Search in sources :

Example 1 with HashedWheelTimeoutController

use of com.biasedbit.http.client.timeout.HashedWheelTimeoutController in project http-client by biasedbit.

the class DefaultHttpClient method init.

// HttpClient -----------------------------------------------------------------------------------------------------
@Override
public boolean init() {
    if (timeoutController == null) {
        // prefer lower resources consumption over precision
        timeoutController = new HashedWheelTimeoutController();
        timeoutController.init();
        internalTimeoutManager = true;
    }
    if (connectionFactory == null)
        connectionFactory = new DefaultConnectionFactory();
    if ((sslContextFactory == null) && isHttps())
        sslContextFactory = BogusSslContextFactory.getInstance();
    eventConsumerLatch = new CountDownLatch(1);
    eventQueue = new LinkedBlockingQueue<>();
    // TODO instead of fixed size thread pool, use a cached thread pool with size limit (limited growth cached pool)
    executor = Executors.newFixedThreadPool(maxHelperThreads, new NamedThreadFactory("httpHelpers"));
    Executor workerPool = Executors.newFixedThreadPool(maxIoWorkerThreads, new NamedThreadFactory("httpWorkers"));
    if (useNio) {
        // It's only going to create 1 thread, so no harm done here.
        Executor bossPool = Executors.newCachedThreadPool();
        channelFactory = new NioClientSocketChannelFactory(bossPool, workerPool);
    } else {
        channelFactory = new OioClientSocketChannelFactory(workerPool);
    }
    channelGroup = new CleanupChannelGroup(toString());
    // Create a pipeline without the last handler (it will be added right before connecting).
    pipelineFactory = new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = Channels.pipeline();
            if (useSsl) {
                SSLEngine engine = sslContextFactory.getClientContext().createSSLEngine();
                engine.setUseClientMode(true);
                pipeline.addLast("ssl", new SslHandler(engine));
            }
            pipeline.addLast("codec", new HttpClientCodec());
            if (autoDecompress)
                pipeline.addLast("decompressor", new HttpContentDecompressor());
            return pipeline;
        }
    };
    executor.execute(new Runnable() {

        @Override
        public void run() {
            eventHandlingLoop();
        }
    });
    return true;
}
Also used : NioClientSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory) SSLEngine(javax.net.ssl.SSLEngine) HashedWheelTimeoutController(com.biasedbit.http.client.timeout.HashedWheelTimeoutController) SslHandler(org.jboss.netty.handler.ssl.SslHandler) OioClientSocketChannelFactory(org.jboss.netty.channel.socket.oio.OioClientSocketChannelFactory)

Aggregations

HashedWheelTimeoutController (com.biasedbit.http.client.timeout.HashedWheelTimeoutController)1 SSLEngine (javax.net.ssl.SSLEngine)1 NioClientSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory)1 OioClientSocketChannelFactory (org.jboss.netty.channel.socket.oio.OioClientSocketChannelFactory)1 SslHandler (org.jboss.netty.handler.ssl.SslHandler)1