Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpUtil类加载的时候就要初始化client #165

Merged
merged 2 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,45 +30,46 @@
import org.joyqueue.toolkit.time.SystemClock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@Component
public class AsyncHttpClient {

private static final int ASYNC_TIMEOUT = 2000;
private static final Logger logger= LoggerFactory.getLogger(AsyncHttpClient.class);
private static CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
.setDefaultRequestConfig(RequestConfig.custom()
.setConnectTimeout(ASYNC_TIMEOUT)
.setSocketTimeout(ASYNC_TIMEOUT)
.setConnectionRequestTimeout(ASYNC_TIMEOUT).build()).build();
private static final Logger logger = LoggerFactory.getLogger(AsyncHttpClient.class);
private static CloseableHttpAsyncClient httpclient;

static {
httpclient = HttpAsyncClients.custom()
.setDefaultRequestConfig(RequestConfig.custom()
.setConnectTimeout(ASYNC_TIMEOUT)
.setSocketTimeout(ASYNC_TIMEOUT)
.setConnectionRequestTimeout(ASYNC_TIMEOUT).build()).build();

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
if (null != httpclient) {
try {
httpclient.close();
logger.info("close async http client success.");
} catch (IOException e) {
logger.error("close async http client error.", e);
httpclient = null;
}
}
}));
}

public static void AsyncRequest(HttpUriRequest request, FutureCallback<HttpResponse> asyncCallBack){
httpclient.start();
request.setHeader("Content-Type", "application/json;charset=utf-8");
httpclient.execute(request,asyncCallBack);
}

@PreDestroy
public static void destroy() {
if (null != httpclient) {
try {
httpclient.close();
} catch (IOException e) {
logger.error("close async http client error.", e);
httpclient = null;
}
}
}

@Deprecated
public static class ConcurrentResponseHandler implements FutureCallback<HttpResponse>{
private Logger logger = LoggerFactory.getLogger(ConcurrentResponseHandler.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
import org.joyqueue.toolkit.time.SystemClock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -43,7 +41,6 @@
* HTTP服务 公共方法
* Created by wangxiaofei1 on 2018/10/17.
*/
@Component
public class HttpUtil {
private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
protected static final long DEFAULT_HTTP_CONN_TIME_TO_LIVE = 60;
Expand All @@ -69,8 +66,7 @@ public class HttpUtil {
private static RequestConfig requestConfig;
private static IdleConnectionMonitorThread monitorThread;

@PostConstruct
public void init() {
static {
clientConnManager = new PoolingHttpClientConnectionManager(DEFAULT_HTTP_CONN_TIME_TO_LIVE, TimeUnit.SECONDS);
clientConnManager.setMaxTotal(DEFAULT_HTTP_CONN_MAX_TOTAL);
clientConnManager.setDefaultMaxPerRoute(DEFAULT_HTTP_CONN_MAX_PER_ROUTE);
Expand All @@ -83,9 +79,22 @@ public void init() {
httpClientBuilder = HttpClientBuilder.create()
.setConnectionManager(clientConnManager);
client = httpClientBuilder.build();

monitorThread = new IdleConnectionMonitorThread(clientConnManager);
monitorThread.setName("HTTP-IO-MONITOR-THREAD");
monitorThread.setDaemon(true);
monitorThread.start();

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
client.close();
monitorThread.shutdown();
clientConnManager.close();
logger.info("close http client success. ");
} catch (IOException e) {
logger.error("close http client error.", e);
}
}));
}

public static String createUrl(String url, String uri) {
Expand Down