二十七、CloseableHttpAsyncClient的使用和优化

本文详细介绍异步HTTP请求的优化方法,包括CloseableHttpAsyncClient的使用、配置及异步GET请求的实现。通过设置连接数、超时时间、压缩选项等参数,提升网络请求效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CloseableHttpClient的使用和优化 上篇文章介绍了如何对Http请求进行优化,提到了异步

同步和异步的介绍、使用场景 下篇文章介绍了同步和异步的使用场景

本文就是对异步代码的介绍:

//Basic认证
private static final CredentialsProvider credsProvider = new BasicCredentialsProvider();
//异步HTTP请求配置
private static final RequestConfig reqestConfig;
//异步http请求客户端
private static final CloseableHttpAsyncClient httpClient;
//异步get方法
private static final HttpGet httpGet;

//jackson解析工具
private static final ObjectMapper mapper = new ObjectMapper();

static {
    System.setProperty("http.maxConnections","50");
    System.setProperty("http.keepAlive", "true");
    //初始化BASE认证配置
    credsProvider.setCredentials(
            new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
            new UsernamePasswordCredentials(“”,“”));
    //初始化HTTP请求配置
    reqestConfig = RequestConfig.custom()
            .setContentCompressionEnabled(true)
            .setSocketTimeout(100)
            .setAuthenticationEnabled(true)
            .setConnectionRequestTimeout(100)
            .setConnectTimeout(100).build();
    //关闭httpClient
    httpClient = HttpAsyncClients.custom()
            .useSystemProperties()
            .setMaxConnTotal(50)
            .setDefaultCredentialsProvider(credsProvider).build();
    httpClient.start();
    httpGet = new HttpGet();
    httpGet.setConfig(reqestConfig);
}

/*
 * 功能:返回HTTP GET
 * @author zhangdaquan
 * @date 2019/1/2 下午8:31
 * @param [url]
 * @return org.apache.http.client.methods.HttpGet
 * @exception
 */
public static HttpGet get(String url) throws URISyntaxException {
    httpGet.setURI(new URI(url));
    return httpGet;
}
/*
 * 功能:异步获取结果
 * @author zhangdaquan
 * @date 2019/1/2 下午8:32
 * @param [url]
 * @return void
 * @exception
 */
public static Future<HttpResponse> getUrl(String url) throws IOException, HttpException, URISyntaxException {
    HttpGet get = get(url);
    Future<HttpResponse> httpResponseFuture = httpClient.execute(get, new FutureCallback<HttpResponse>() {
        @Override
        public void completed(HttpResponse httpResponse) {
        }
        @Override
        public void failed(Exception e) {
        }
        @Override
        public void cancelled() {
        }
    });
    return httpResponseFuture;
}
public static void main(String[] args) throws IOException, HttpException, ExecutionException, InterruptedException, URISyntaxException {
   
    Long tm = System.currentTimeMillis();

    for (int i = 0; i < 50 ; i++){
        new Thread(()->{
            try{
                HttpResponse httpResponse = getUrl(getUrl).get();
                HttpEntity entity = httpResponse.getEntity();
                System.out.println(entity);
                String s = EntityUtils.toString(entity);
                //这里没有很好的处理response会导致上层业务要处理httpResponse
                HttpClientUtils.closeQuietly(httpResponse);
                System.out.println(s);
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }
    System.out.println(System.currentTimeMillis()-tm);*/
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值