Created
April 10, 2014 09:08
-
-
Save renatoathaydes/10359780 to your computer and use it in GitHub Desktop.
Creating and configuring a HttpAsyncClient
This file contains 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
// more config options at http://hc.apache.org/httpcomponents-asyncclient-4.0.x/httpasyncclient/examples/org/apache/http/examples/nio/client/AsyncClientConfiguration.java | |
public RequestRunner provideRequestRunner( ComponentContext context, URI pageUri, Iterable<URI> assetUris ) | |
throws IOException | |
{ | |
try | |
{ | |
IOReactorConfig ioReactorConfig = IOReactorConfig.custom() | |
.setIoThreadCount( Runtime.getRuntime().availableProcessors() ) | |
.setConnectTimeout( 30_000 ) | |
.setSoTimeout( 30_000 ) | |
.build(); | |
ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor( ioReactorConfig ); | |
Registry<SchemeIOSessionStrategy> sessionStrategyRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create() | |
.register( "http", NoopIOSessionStrategy.INSTANCE ) | |
.register( "https", new SSLIOSessionStrategy( | |
socketFactoryProvider.newSSLContext(), | |
socketFactoryProvider.getHostnameVerifier() ) ) | |
.build(); | |
CloseableHttpAsyncClient client = HttpAsyncClients.custom() | |
.setMaxConnTotal( 5_000 ) | |
.setMaxConnPerRoute( 1_000 ) | |
.setConnectionManager( new PoolingNHttpClientConnectionManager( ioReactor, sessionStrategyRegistry ) ) | |
.build(); | |
return new RequestRunner( clock, client, pageUri, assetUris, createStatsSenderIfNecessary( context ) ); | |
} | |
catch( Exception e ) | |
{ | |
throw new IOException( "Could not create a request runner", e ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment