zoukankan      html  css  js  c++  java
  • Spring 线程池实战

    应用场景

    老王上线了一个电商平台,最近需要开发一个功能,使用户能够直接点击物流编号查询到物流信息。通过对比,选择了一个快递聚合查询平台作为接入方,但是由于使用的是免费账号,快递聚合平台只允许最多2个并发,否则查询失败。我们要如何帮助老王去解决这个问题呢?

    聪明的程序员小张想了一下,很快就整理出来解决问题的思路:

    1. 首先用 Httpclient 实现与物流聚合平台的对接
    2. 创建一个物流查询 Service 类,基于线程接口
    3. 创建一个 Sping 固定大小线程池
    4. Controller 接口通过线程池访问物流接口,异步返回

    POM.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.suoron.springmvc</groupId>
        <artifactId>myShop-threads</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <dependencies>
    
            <!-- Apache Http Begin -->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.5</version>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>fluent-hc</artifactId>
                <version>4.5.5</version>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpmime</artifactId>
                <version>4.5.5</version>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.47</version>
            </dependency>
            <!-- Apache Http End -->
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.2.5.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.freemarker</groupId>
                <artifactId>freemarker</artifactId>
                <version>2.3.20</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>4.2.5.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>3.0-alpha-1</version>
            </dependency>
    
    
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins </groupId>
                    <artifactId>maven-compiler-plugin </artifactId>
                    <version>2.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    

    配置文件

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
    
        <!-- 字符集过滤器 -->
        <filter>
            <filter-name>encodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>encodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <!-- spring mvc 前端控制器 -->
        <servlet>
            <servlet-name>springServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath*:spring-mvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-context.xml</param-value>
        </context-param>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    </web-app>
    

    spring-context.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    
        <context:annotation-config />
        <context:component-scan base-package="com.suoron.springmvc.service.impl"/>
    
        <!-- 线程池配置 -->
        <bean id="myThreadPool" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
            <!-- 核心线程数  -->
            <property name="corePoolSize" value="2" />
            <!-- 最大线程数 -->
            <property name="maxPoolSize" value="2" />
            <!-- 队列最大长度 -->
            <property name="queueCapacity" value="1000" />
            <!-- 线程池维护线程所允许的空闲时间 -->
            <property name="keepAliveSeconds" value="300" />
            <!-- 线程池对拒绝任务(无线程可用)的处理策略 -->
            <property name="rejectedExecutionHandler">
                <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
            </property>
        </bean>
    
    </beans>
    

    spring-mvc.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <description>Spring MVC Configuration</description>
    
        <!-- 加载配置属性文件 -->
        <!--  <context:property-placeholder ignore-unresolvable="true" location="classpath:config.properties"/> -->
    
        <!-- 使用 Annotation 自动注册 Bean,只扫描 @Controller -->
        <context:component-scan base-package="com.suoron.springmvc.controller"/>
    
        <!-- 配置注解驱动 可以将request参数与绑定到controller参数上 -->
        <mvc:annotation-driven />
    
    
        <!-- html视图解析器 必须先配置freemarkerConfig,注意html是没有prefix前缀属性的-->
        <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
            <property name="templateLoaderPath">
                <value>/WEB-INF/views/html</value>
            </property>
            <property name="defaultEncoding" value="UTF-8" />
        </bean>
        <bean id="htmlviewResolver"
              class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
            <property name="suffix" value=".html" />
            <property name="order" value="0"></property>
            <property name="contentType" value="text/html;charset=UTF-8"></property>
        </bean>
    
        <!-- 定义视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
            <property name="prefix" value="/WEB-INF/views/jsp/" />
            <property name="suffix" value=".jsp" />
            <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView"/> <!-- 不能用jstl的那个 -->
            <property name="contentType" value="text/html;charset=UTF-8"/>
            <property name="order" value="1"></property>
        </bean>
    
        <!-- 静态资源映射 -->
        <mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/>
        <mvc:resources mapping="/json/**" location="/json/" cache-period="31536000"/>
    </beans>
    

    访问代码

    HttpUtils.java -- 对httpclient进行了封装

    public class HttpUtils {
    
        public static String post(String url, Map<String,String> map){
            // 创建 HttpClient 客户端
            CloseableHttpClient httpClient = HttpClients.createDefault();
    
            // 创建 HttpPost 请求
            HttpPost httpPost = new HttpPost(url);
            // 设置长连接
            httpPost.setHeader("Connection", "keep-alive");
            // 设置代理(模拟浏览器版本)
            httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
            // 设置 Cookie
            //httpPost.setHeader("Cookie", "UM_distinctid=16442706a09352-0376059833914f-3c604504-1fa400-16442706a0b345; CNZZDATA1262458286=1603637673-1530123020-%7C1530123020; JSESSIONID=805587506F1594AE02DC45845A7216A4");
    
            // 创建 HttpPost 参数
            List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
            for(String key:map.keySet()){
                params.add(new BasicNameValuePair(key, map.get(key)));
            }
    /*
            params.add(new BasicNameValuePair("draw", "1"));
            params.add(new BasicNameValuePair("start", "0"));
            params.add(new BasicNameValuePair("length", "10"));
    */
    
            CloseableHttpResponse httpResponse = null;
            try {
                // 设置 HttpPost 参数
                httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
                httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                // 输出请求结果
                //System.out.println(EntityUtils.toString(httpEntity));
                return EntityUtils.toString(httpEntity);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            // 无论如何必须关闭连接
            finally {
                try {
                    if (httpResponse != null) {
                        httpResponse.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                try {
                    if (httpClient != null) {
                        httpClient.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
        public static String get(String url){
            // 创建 HttpClient 客户端
            CloseableHttpClient httpClient = HttpClients.createDefault();
    
            // 创建 HttpGet 请求
            HttpGet httpGet = new HttpGet(url);
            // 设置长连接
            httpGet.setHeader("Connection", "keep-alive");
            // 设置代理(模拟浏览器版本)
            httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
            // 设置 Cookie
            //httpGet.setHeader("Cookie", "UM_distinctid=16442706a09352-0376059833914f-3c604504-1fa400-16442706a0b345; CNZZDATA1262458286=1603637673-1530123020-%7C1530123020; JSESSIONID=805587506F1594AE02DC45845A7216A4");
    
            CloseableHttpResponse httpResponse = null;
            try {
                // 请求并获得响应结果
                httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                // 输出请求结果
                //System.out.println(EntityUtils.toString(httpEntity));
                return EntityUtils.toString(httpEntity);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            // 无论如何必须关闭连接
            finally {
                if (httpResponse != null) {
                    try {
                        httpResponse.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
                if (httpClient != null) {
                    try {
                        httpClient.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return null;
        }
    }
    
    

    LogisticsUtil.java -- 物流聚合平台查询工具类

    /**
     *
     * 快递鸟物流轨迹即时查询接口
     *
     * @技术QQ群: 456320272
     * @see: http://www.kdniao.com/YundanChaxunAPI.aspx
     * @copyright: 深圳市快金数据技术服务有限公司
     *
     * DEMO中的电商ID与私钥仅限测试使用,正式环境请单独注册账号
     * 单日超过500单查询量,建议接入我方物流轨迹订阅推送接口
     *
     * ID和Key请到官网申请:http://www.kdniao.com/ServiceApply.aspx
     */
    
    public class LogisticsUtil {
    
        public static byte[] decodeBase64(String input) throws Exception{
            Class clazz=Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
            Method mainMethod= clazz.getMethod("decode", String.class);
            mainMethod.setAccessible(true);
            Object retObj=mainMethod.invoke(input);
            return (byte[])retObj;
        }
        //DEMO
        public static void main(String[] args) {
    /*
    
            try {
                String base64encodedString = Base64.getEncoder().encodeToString("小黄人。。。".getBytes("utf-8"));
                System.out.println("Base64 编码字符串 (基本) :" + base64encodedString);
                //System.out.println(new String(decodeBase64("5bCP6buE5Lq6Li4uLi4u")));
                // 解码
                byte[] base64decodedBytes = Base64.getDecoder().decode("5bCP6buE5Lq644CC44CC44CC");
    
                System.out.println("原始字符串: " + new String(base64decodedBytes, "utf-8"));
            } catch (Exception e) {
                e.printStackTrace();
            }
    
    */
    
            LogisticsUtil api = new LogisticsUtil();
            try {
                String result = api.getOrderTracesByJson("ZTO", "75125101747966");
                System.out.print(result);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        //电商ID
        private String EBusinessID="1436451";
        //电商加密私钥,快递鸟提供,注意保管,不要泄漏
        private String AppKey="9b4c6b10-84c6-4223-b1fe-6be2f0277f85";
        //请求url
        private String ReqURL="http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx";
    
        /**
         * Json方式 查询订单物流轨迹
         * @throws Exception
         */
        public String getOrderTracesByJson(String expCode, String expNo) throws Exception{
            String requestData= "{'OrderCode':'','ShipperCode':'" + expCode + "','LogisticCode':'" + expNo + "'}";
    
            Map<String, String> params = new HashMap<String, String>();
            params.put("RequestData", urlEncoder(requestData, "UTF-8"));
            params.put("EBusinessID", EBusinessID);
            params.put("RequestType", "1002");
            String dataSign=encrypt(requestData, AppKey, "UTF-8");
            params.put("DataSign", urlEncoder(dataSign, "UTF-8"));
            params.put("DataType", "2");
    
            //String result=sendPost(ReqURL, params);
            String result = HttpUtils.post(ReqURL,params);
    
            //根据公司业务处理返回的信息......
    
            return result;
        }
        /**
         * base64编码
         * @param str 内容
         * @param charset 编码方式
         * @throws UnsupportedEncodingException
         */
        private String base64(String str, String charset) throws UnsupportedEncodingException{
            //String encoded = base64Encode(str.getBytes(charset));
            String encoded = Base64.getEncoder().encodeToString(str.getBytes(charset));
            return encoded;
        }
    
        

    MD5Utils.java -- MD5加密工具类,物流接口数据加解密

    public class MD5Utils {
        /**
         * MD5加密
         * @param str 内容
         * @param charset 编码方式
         * @throws Exception
         */
        //@SuppressWarnings("unused")
        public static String MD5(String str, String charset) throws Exception {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes(charset));
            byte[] result = md.digest();
            StringBuffer sb = new StringBuffer(32);
            for (int i = 0; i < result.length; i++) {
                int val = result[i] & 0xff;
                if (val <= 0xf) {
                    sb.append("0");
                }
                sb.append(Integer.toHexString(val));
            }
            return sb.toString().toLowerCase();
        }
    }
    

    LogisticsService.java -- 物流查询service类

    public interface LogisticsService extends Callable<String>, Serializable {
        public void setExpInfo(String expCode,String expNo);
    }
    

    LogisticsServiceImpl.java -- 物流查询实现类

    @Service
    @Scope("prototype")
    public class LogisticsServiceImpl implements LogisticsService, Callable<String>, Serializable {
    
        private  String expCode;
        private  String expNo;
    
        public void setExpInfo(String expCode, String expNo) {
            this.expCode = expCode;
            this.expNo = expNo;
        }
    
        @Override
        public String call() throws Exception {
            LogisticsUtil api = new LogisticsUtil();
            try {
                Thread.sleep(2000);
                String result = api.getOrderTracesByJson(expCode, expNo);
                //System.out.print(result);
                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    LogisticsController.java -- 访问controller



    作者:索伦x
    链接:https://www.jianshu.com/p/e961916e28bd
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    第二次冲刺个人工作总结06
    第二次冲刺个人工作总结05
    第十三周学习进度
    第二次冲刺工作总结04
    软件工程课堂作业——寻找水王2
    第二次冲刺个人工作总结03
    第二次冲刺周期个人工作总结02
    第二次冲刺个人工作总结01
    第二次冲刺会议
    git 的更新代码的取消
  • 原文地址:https://www.cnblogs.com/telwanggs/p/13079783.html
Copyright © 2011-2022 走看看