Created
July 14, 2018 16:08
-
-
Save duqicauc/60df9d0d6491b038c1b448b35885dea9 to your computer and use it in GitHub Desktop.
异步http客户端构建工厂
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考:https://www.jianshu.com/p/1ed5f234d57e