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

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

直接上代码

 

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://wpvtx.baihongyu.com/

你可能感兴趣的文章
运行自己的shell脚本
查看>>
C语言之基本算法26—佩尔方程求解
查看>>
jqMobi(App Framework)入门学习(一)
查看>>
委托的N种写法
查看>>
如何让linux加载当前目录的动态库
查看>>
如何查看dede版本信息
查看>>
基于SIP和RTP协议的开源VOIP之QuteCom简单介绍
查看>>
Leetcode--easy系列9
查看>>
.NET Core程序中使用User Secrets存储敏感数据
查看>>
freeradius 启动报错Refusing to start with libssl version OpenSSL 1.0.1
查看>>
python 操作redis之——有序集合(sorted set) (七)
查看>>
Python 爬虫实例(1)—— 爬取百度图片
查看>>
setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key
查看>>
Linux环境变量设置中配置文件分析(/etc/profile,~/.bashrc等)(转)
查看>>
详解执行计划
查看>>
petri网
查看>>
删除节点
查看>>
Objective-C Classes Are also Objects
查看>>
idea搭建javaweb项目 Artifacts生成
查看>>
python matplot 绘图
查看>>