import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.apache.http.nio.reactor.ConnectingIOReactor;
import org.apache.http.nio.reactor.IOReactorException;
import org.springframework.util.Assert;
@Slf4j
public class HttpAsyncClient {
// 異步httpclient
private volatile static CloseableHttpAsyncClient asyncHttpClient;
private static final Object LOCK = new Object();
static {
asyncHttpClient = createAsyncClient();
}
public static CloseableHttpAsyncClient getAsyncHttpClient(){
if (null == asyncHttpClient){
synchronized (LOCK){
if (null == asyncHttpClient){
asyncHttpClient = createAsyncClient();
}
}
}
return asyncHttpClient;
}
private static CloseableHttpAsyncClient createAsyncClient(){
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(1000)
.setSocketTimeout(1000)
.setConnectionRequestTimeout(1000)
.build();
//配置io線程
IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
// .setIoThreadCount(10)
.setIoThreadCount(Runtime.getRuntime().availableProcessors())
.setSoKeepAlive(true)
.build();
//設定連接配接池大小
ConnectingIOReactor ioReactor = null;
try {
ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
} catch (IOReactorException e) {
e.printStackTrace();
}
Assert.notNull(ioReactor, "ioReactor init error");
PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor);
connManager.setMaxTotal(1000);
connManager.setDefaultMaxPerRoute(100);
final CloseableHttpAsyncClient client = HttpAsyncClients.custom()
.setConnectionManager(connManager)
.setDefaultRequestConfig(requestConfig)
.build();
client.start();
return client;
}
}
import com.sendinfo.datamanager.scheduler.exception.HttpException;
import com.sendinfo.trace.core.util.LoggerFormatUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.nio.client.util.HttpAsyncClientUtils;
import org.apache.http.util.EntityUtils;
import org.springframework.util.CollectionUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author
*/
@Slf4j
public class HttpAsyncUtils {
public static void doGet(String url) throws HttpException {
try {
// 通過httpget方式來實作我們的get請求
HttpGet httpGet = new HttpGet(url);
CloseableHttpAsyncClient client = HttpAsyncClient.getAsyncHttpClient();
// CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
// client.start();
//異步請求
client.execute(httpGet, new Back());
// client.close();
} catch (Exception e) {
throw new HttpException(e);
}
}
public static void doPost(String url, Map<String, String> params) throws HttpException {
try {
// 通過HttpPost來發送post請求
HttpPost httpPost = new HttpPost(url);
/*
* post帶參數開始
*/
// 第三步:構造list集合,往裡面丢資料
List<NameValuePair> list = new ArrayList<NameValuePair>();
if (!CollectionUtils.isEmpty(params)){
for (Map.Entry<String, String> param : params.entrySet()){
BasicNameValuePair basicNameValuePair = new BasicNameValuePair(param.getKey(), param.getValue());
list.add(basicNameValuePair);
}
}
// 第二步:我們發現Entity是一個接口,是以隻能找實作類,發現實作類又需要一個集合,集合的泛型是NameValuePair類型
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list);
// 第一步:通過setEntity 将我們的entity對象傳遞過去
httpPost.setEntity(formEntity);
CloseableHttpAsyncClient client = HttpAsyncClient.getAsyncHttpClient();
// CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
// client.start();
//異步請求
client.execute(httpPost, new Back());
// client.close();
} catch (Exception e) {
LoggerFormatUtil.error(e, log, "do post error");
throw new HttpException(e);
}
}
static class Back implements FutureCallback<HttpResponse> {
private long start = System.currentTimeMillis();
Back() {
}
@Override
public void completed(HttpResponse httpResponse) {
try {
System.out.println("cost is:" + (System.currentTimeMillis() - start) + ":" + EntityUtils.toString(httpResponse.getEntity()));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void failed(Exception e) {
System.err.println(" cost is:" + (System.currentTimeMillis() - start) + ":" + e);
}
@Override
public void cancelled() {
}
}
}
<!-- async Http Client -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.1.3</version>
</dependency>