• 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

  • 相关阅读:
    Eureka 系列(04)客户端源码分析
    Eureka 系列(03)Spring Cloud 自动装配原理
    Eureka 系列(02)Eureka 一致性协议
    Eureka 系列(01)最简使用姿态
    Feign 系列(05)Spring Cloud OpenFeign 源码解析
    python 线程,进程与协程
    Python IO多路复用
    python 作用域
    python 网络编程:socket(二)
    python 网络编程:socket
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/14463700.html
Copyright © 2020-2023  润新知