Java异步执行多个HTTP请求的例子(需要apache http类库)
直接上代码
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使用多线程异步执行批量更新操作
import com.google.common.collect.Lists; import org.apache.commons.collections.CollectionUtils; impor ...
- Paip.Php Java 异步编程。推模型与拉模型。响应式(Reactive)”编程FutureData总结... 1
Paip.Php Java 异步编程.推模型与拉模型.响应式(Reactive)"编程FutureData总结... 1.1.1 异步调用的实现以及角色(:调用者 提货单) F ...
- Android Studio利用异步任务AsyncTask发送post请求获取json数据
syncTask,是Android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以通过接口实现UI进度更新),最后反馈执行的结果给UI主 ...
- java 异步机制与同步机制的区别
所谓异步输入输出机制,是指在进行输入输出处理时,不必等到输入输出处理完毕才返回.所以异步的同义语是非阻塞(None Blocking). 网上有很多网友用很通俗的比喻 把同步和异步讲解的很透彻 转过 ...
- Adobe AIR中使用Flex连接Sqlite数据库(1)(创建数据库和表,以及同步和异步执行模式)
系列文章导航 Adobe AIR中使用Flex连接Sqlite数据库(1)(创建数据库和表) Adobe AIR中使用Flex连接Sqlite数据库(2)(添加,删除,修改以及语句参数) Adobe ...
- 异步执行任务SimpleAsyncTaskExecutor详解
SimpleAsyncTaskExecutor 异步执行用户任务的SimpleAsyncTaskExecutor.每次执行客户提交给它的任务时,它会启动新的线程,并允许开发者控制并发线程的上限(con ...
- Spring Boot 之异步执行方法
前言: 最近的时候遇到一个需求,就是当服务器接到请求并不需要任务执行完成才返回结果,可以立即返回结果,让任务异步的去执行.开始考虑是直接启一个新的线程去执行任务或者把任务提交到一个线程池去执行,这两种 ...
- Java异步记录日志-2022新项目
一.业务场景 web项目开发中,经常会有的一个操作是记录请求日志,比如记录请求的IP地址,记录请求的路径,记录请求的参数等等. 每个系统都会根据自己的需要来记录一些请求相关的日志.一般会将记录的日志信 ...
- thread.join 从异步执行变成同步
Java的线程模型为我们提供了更好的解决方案,这就是join方法.在前面已经讨论过,join的功能就是使用线程 从异步执行变成同步执行 当线程变成同步执行后,就和从普通的方法中得到返回数据没有什么区别 ...
随机推荐
- Match-----Correlation-----find_ncc_model_exposure
* This example program shows how to use HALCON's correlation-based* matching. In particular it demon ...
- pytorch入门之安装和配置
pytorch是一种python接口的深度学习框架,其他的框架还有caffe,tensorflow等等. 1,pytorch目前支持linux和OSX两种系统.支持的Python版本有2.7,3.5, ...
- .net like模糊查询参数化
List<SqlParameter> paras = new List<SqlParameter>(); if (!string.IsNullOrEmpty(ciName)) ...
- [leetcode]38. Count and Say数数
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- Redhat学习(此为草稿,正式笔记之后存放)
系统组成 版本XX.XX.XX XX主版本,XX副版本(奇数为测试版,偶数为稳定版),修订版本号 , 表示修改的次数(debug) linux主流分支 硬盘分区 /dev/ 硬件设备所在分区 hd 表 ...
- 微擎开发------day03
微擎系统的路由 首先附上一张完整的项目目录图,可下载图片后查看详情 详细的路由介绍 (1)后台系统路由 例如:http://local.drp.com/web/index.php?c=home& ...
- Myisam 和 Innodb 区别
MySQL默认采用的是MyISAM. MyISAM不支持事务,而InnoDB支持.InnoDB的AUTOCOMMIT默认是打开的,即每条SQL语句会默认被封装成一个事务,自动提交,这样会影响速度,所以 ...
- Centos7下安装Docker[z]
[z]https://www.cnblogs.com/qgc1995/p/9553572.html https://yq.aliyun.com/articles/691610?spm=a2c4e.11 ...
- flask 未完待续
Flask - 一个短小精悍.可扩展的一个Web框架很多可用的第三方组件:http://flask.pocoo.org/extensions/blogs:https://www.cnblogs.com ...
- 3U - 算菜价
妈妈每天都要出去买菜,但是回来后,兜里的钱也懒得数一数,到底花了多少钱真是一笔糊涂帐.现在好了,作为好儿子(女儿)的你可以给她用程序算一下了,呵呵. Input 输入含有一些数据组,每组数据包括菜种( ...