zoukankan      html  css  js  c++  java
  • Java HttpClient线程池Demo

    Java HttpClient线程池Demo

    package com.how2java.http;
    
    import org.apache.hc.client5.http.ClientProtocolException;
    import org.apache.hc.client5.http.ConnectionKeepAliveStrategy;
    import org.apache.hc.client5.http.classic.methods.HttpGet;
    import org.apache.hc.client5.http.classic.methods.HttpPost;
    import org.apache.hc.client5.http.impl.DefaultConnectionKeepAliveStrategy;
    import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
    import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
    import org.apache.hc.client5.http.impl.classic.HttpClients;
    import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
    import org.apache.hc.client5.http.protocol.HttpClientContext;
    import org.apache.hc.core5.http.HttpEntity;
    import org.apache.hc.core5.http.io.entity.EntityUtils;
    import org.apache.hc.core5.http.protocol.HttpContext;
    
    import java.io.IOException;
    
    public class BaseHttpClinet {
        /**
         * 多线程-HttpClient连接池管理HTTP请求实例
         */
        public static void main(String[] args) {
            //连接池对象
            PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
            //将最大连接数增加到200
            connectionManager.setMaxTotal(200);
            //将每个路由的默认最大连接数增加到20
            connectionManager.setDefaultMaxPerRoute(10);
    
    //        ConnectionKeepAliveStrategy kaStrategy = new DefaultConnectionKeepAliveStrategy()
    //        {
    //            @Override
    //            public TimeValue getKeepAliveDuration(HttpResponse response, HttpContext context)
    //            {
    //                TimeValue keepAlive = super.getKeepAliveDuration(response, context);
    //                if (keepAlive.toSeconds() == -1)
    //                {
    //                    keepAlive = TimeValue.ofSeconds(10000);
    //                }
    //                return keepAlive;
    //            }
    //
    //        };
    
            //HttpClient对象
            CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();
            //为每一个URI创建一个线程
    //        GetThread[] threads = new GetThread[urisToGet.length];
            String url = "https://www.baidu.com/s?word=java";
            long startTime = System.currentTimeMillis();
            int questCount = 100;
            while(questCount > 0){
                questCount--;
                HttpGet httpGet = new HttpGet(url);
                // 复用线程 可提高效率
                new GetThread(httpClient, httpGet).start();
            }
    
            System.out.println((System.currentTimeMillis() - startTime)/1000);
    
        }
    
        /**
         * 执行Get请求线程
         */
        public static class GetThread extends Thread{
            private final CloseableHttpClient httpClient;
            private final HttpContext context;
            private final HttpGet httpGet;
            public GetThread(CloseableHttpClient httpClient, HttpGet httpGet) {
                this.httpClient = httpClient;
                this.context = HttpClientContext.create();
                this.httpGet = httpGet;
            }
            @Override
            public void run() {
                try {
                    CloseableHttpResponse response = httpClient.execute(httpGet, context);
                    try {
                        HttpEntity entity = response.getEntity();
                        System.out.println(entity.getContent());
                    }finally {
                        EntityUtils.consume(response.getEntity());
                        response.close();
                    }
                }catch (ClientProtocolException ex){
                    //处理客户端协议异常
                }catch (IOException ex){
                    //处理客户端IO异常
                    ex.printStackTrace();
                }
            }
        }
    
        /**
         * 执Post请求线程
         */
        public static class PostThread extends Thread{
            private final CloseableHttpClient httpClient;
            private final HttpContext context;
            private final HttpPost httpPost;
            public PostThread(CloseableHttpClient httpClient, HttpPost httpPost) {
                this.httpClient = httpClient;
                this.context = HttpClientContext.create();
                this.httpPost = httpPost;
            }
            @Override
            public void run() {
                try {
                    CloseableHttpResponse response = httpClient.execute(httpPost, context);
                    try {
                        HttpEntity entity = response.getEntity();
                        System.out.println(entity.getContent());
                    }finally {
                        EntityUtils.consume(response.getEntity());
                        response.close();
                    }
                }catch (ClientProtocolException ex){
                    //处理客户端协议异常
                }catch (IOException ex){
                    //处理客户端IO异常
                    ex.printStackTrace();
                }
            }
        }
    }

    参考:

    https://zhuanlan.zhihu.com/p/90855160

  • 相关阅读:
    C#实现RSA加密与解密、签名与认证
    RSA公钥加密,私钥解密的程序示例
    C#中自定义属性的例子
    HTTPS简单原理介绍
    深入浅出HTTPS基本原理
    WebClient请求帮助类
    WebApi安全性 使用TOKEN+签名验证
    jsvascript === 和==的区别
    UML中的图的出现顺序
    UML从需求到实现----用例
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/14463700.html
Copyright © 2011-2022 走看看