博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java异步执行多个HTTP请求的例子(需要apache http类库)
阅读量:6909 次
发布时间:2019-06-27

本文共 3371 字,大约阅读时间需要 11 分钟。

直接上代码

 

package org.jivesoftware.spark.util;import java.io.IOException;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import org.apache.http.HttpResponse;import org.apache.http.client.config.RequestConfig;import org.apache.http.client.methods.HttpGet;import org.apache.http.concurrent.FutureCallback;import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;import org.apache.http.impl.nio.client.HttpAsyncClients;import org.jivesoftware.DebugPrint;import org.jivesoftware.spark.SparkManager;//异步埋点数据采集工具类public class HotClickAsync {    static ExecutorService service = Executors.newSingleThreadExecutor(); //单一线程    // 调用http请求。不阻塞主线程    public static void SendRequest(final String event)            throws InterruptedException, IOException {                Runnable run = new Runnable() {            @Override            public void run() {                try {                    SendRequestAsync(event,SparkManager.getSessionManager().getUsername());                } catch (InterruptedException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        };        service.execute(run);    }    // 阻塞HTTP调用    private static void SendRequestAsync(String event,String username)            throws InterruptedException, IOException {        RequestConfig requestConfig = RequestConfig.custom()                .setSocketTimeout(1000) // http超时                .setConnectTimeout(1000).build(); // 连接超时        CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()                .setDefaultRequestConfig(requestConfig).build();        try {            httpclient.start();            final HttpGet[] requests = new HttpGet[] { new HttpGet(                    "http://XXXXXX.cn:81/HotClick.aspx?event="+ event +"&username="+username) // 第一个采集地址            // , new HttpGet("http://mta.qq.com")//第二个采集地址, http://mta.qq.com/            };            // 同步计数            final CountDownLatch latch = new CountDownLatch(requests.length);            for (final HttpGet request : requests) {                httpclient.execute(request, new FutureCallback
() { @Override public void completed(final HttpResponse response) { latch.countDown(); DebugPrint.outStirng(request.getRequestLine() + "####->" + response.getStatusLine()); } @Override public void failed(final Exception ex) { latch.countDown(); DebugPrint.outStirng(request.getRequestLine() + "####->" + ex); } @Override public void cancelled() { latch.countDown(); //DebugPrint.outStirng(request.getRequestLine() // + " cancelled"); } }); } latch.await(); } finally { httpclient.close(); } DebugPrint.outStirng(" ### HotClickAsync Done ###"); }}

 

转载地址:http://orycl.baihongyu.com/

你可能感兴趣的文章
Mysql第一周
查看>>
深入理解 Android 消息机制原理
查看>>
1.一个WEB应用的开发流程
查看>>
centos7 安装mysql5.6.32
查看>>
前端JavaScript实现跨域的方式(转)
查看>>
原来你是这样的http2......
查看>>
WaitAll 和 WhenAll 的使用及区别
查看>>
给信息安全爱好者的一封信
查看>>
学习ASP.NET Core Razor 编程系列八——并发处理
查看>>
CSS动画的性能分析和浏览器GPU加速
查看>>
Nginx在线服务状态下平滑升级及ab压力测试【转】
查看>>
poj-2411 Mondriaan's Dream ***
查看>>
(转)as3中的资源管理与GC
查看>>
项目经验总结分享:图书馆维护系统总结
查看>>
HOJ-10513 Allocation Scheme[简单DFS]
查看>>
大数乘法 zju 1217
查看>>
c# 邮件发送 发送人带昵称
查看>>
wxWidgets事件处理(手机播放器连载系列2)
查看>>
c#中转义符总结
查看>>
Android学习笔记之AndroidManifest.xml文件解析(转载)
查看>>