Skip to content

Instantly share code, notes, and snippets.

@renatoathaydes
Created April 10, 2014 09:08
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renatoathaydes/10359780 to your computer and use it in GitHub Desktop.
Save renatoathaydes/10359780 to your computer and use it in GitHub Desktop.
Creating and configuring a HttpAsyncClient
// 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