Java异步执行多个HTTP请求的例子(需要apache http类库)
2017-06-05 14:47
381 查看
直接上代码
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<HttpResponse>() { @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 ###"); } }
相关文章推荐
- Java异步执行多个HTTP请求的例子(需要apache http类库)
- java模拟http请求上传文件,基于Apache的httpclient
- (一)使用apache的commons-httpclient-3.1.jar之通过Java实现发起HTTP请求【不需要代理】
- java基于ApacheHttpClient发送Http请求实现
- Java利用httpasyncclient进行异步HTTP请求
- java 异步任务队列执行--需要注意的地方
- (二)使用apache的commons-httpclient-3.1.jar之通过Java实现发起HTTP请求【使用代理】
- Java利用httpasyncclient进行异步HTTP请求
- 使用JAVA发送HTTP请求(Http Request),返回HTTP响应(Http Response)内容,代码程序例子及原理说明
- Apache HttpClient Fluent API:在后台线程中异步执行多个请求
- Java利用httpasyncclient进行异步HTTP请求
- Apache HttpClient 4.3开发指南(三个例子)
- Java爬虫走过的坑:org.apache.http.impl.client.HttpClientBuilder.dnsResolver
- Error:(5, 30) java: 程序包org.apache.http.client不存在
- 一心多用多线程-future-java线程中的异步执行
- java中用Http通信的一个小例子
- [CMS]Apache HTTP Server 遇到问题需要关闭
- 关于java异步加载可多选的树形结构(需要导入layer和zTree)(可编辑)
- Android http请求例子、Unicode转UTF-8 java代码
- 一个java执行exe的小例子