Skip to content

Instantly share code, notes, and snippets.

@duqicauc
Created July 14, 2018 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duqicauc/60df9d0d6491b038c1b448b35885dea9 to your computer and use it in GitHub Desktop.
Save duqicauc/60df9d0d6491b038c1b448b35885dea9 to your computer and use it in GitHub Desktop.
异步http客户端构建工厂
/**
* Created by IntelliJ IDEA.
* User: duqi
* Date: 2017/2/9
* Time: 15:06
*/
public class AsyncHttpClientFactoryBean implements FactoryBean<CloseableHttpAsyncClient> {
private static final int DEFAULT_MAX_TOTAL = 512;
private static final int DEFAULT_MAX_PER_ROUTE = 64;
private static final int DEFAULT_CONNECTION_TIMEOUT = 5000;
private static final int DEFAULT_SOCKET_TIMEOUT = 3000;
private static final int DEFAULT_TIMEOUT = 1000;
@Override
public CloseableHttpAsyncClient getObject() throws Exception {
DefaultConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(IOReactorConfig.custom()
.setSoKeepAlive(true).build());
PoolingNHttpClientConnectionManager pcm = new PoolingNHttpClientConnectionManager(ioReactor);
pcm.setMaxTotal(DEFAULT_MAX_TOTAL);
pcm.setDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE);
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT)
.setSocketTimeout(DEFAULT_SOCKET_TIMEOUT)
.setConnectionRequestTimeout(DEFAULT_TIMEOUT)
.build();
return HttpAsyncClients.custom()
.setThreadFactory(new BasicThreadFactory.Builder().namingPattern("AysncHttpThread-%d").build())
.setConnectionManager(pcm)
.setDefaultRequestConfig(defaultRequestConfig)
.build();
}
@Override
public Class<?> getObjectType() {
return CloseableHttpAsyncClient.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
@duqicauc
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment