Example usage for org.apache.http.impl.nio DefaultHttpClientIODispatch DefaultHttpClientIODispatch

List of usage examples for org.apache.http.impl.nio DefaultHttpClientIODispatch DefaultHttpClientIODispatch

Introduction

In this page you can find the example usage for org.apache.http.impl.nio DefaultHttpClientIODispatch DefaultHttpClientIODispatch.

Prototype

public DefaultHttpClientIODispatch(final NHttpClientEventHandler handler, final SSLContext sslcontext,
        final ConnectionConfig config) 

Source Link

Usage

From source file:com.googlecode.jsonrpc4j.JsonRpcHttpAsyncClient.java

private void initialize() {
    if (initialized.getAndSet(true)) {
        return;/*from   ww w. j ava  2s .  co m*/
    }

    // HTTP parameters for the client
    final HttpParams params = new BasicHttpParams();
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT,
            Integer.getInteger("com.googlecode.jsonrpc4j.async.socket.timeout", 30000));
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
            Integer.getInteger("com.googlecode.jsonrpc4j.async.connect.timeout", 30000));
    params.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE,
            Integer.getInteger("com.googlecode.jsonrpc4j.async.socket.buffer", 8 * 1024));
    params.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY,
            Boolean.valueOf(System.getProperty("com.googlecode.jsonrpc4j.async.tcp.nodelay", "true")));
    params.setParameter(CoreProtocolPNames.USER_AGENT, "jsonrpc4j/1.0");

    // Create client-side I/O reactor
    final ConnectingIOReactor ioReactor;
    try {
        IOReactorConfig config = new IOReactorConfig();
        config.setIoThreadCount(Integer.getInteger("com.googlecode.jsonrpc4j.async.reactor.threads", 1));
        ioReactor = new DefaultConnectingIOReactor(config);
    } catch (IOReactorException e) {
        throw new RuntimeException("Exception initializing asynchronous Apache HTTP Client", e);
    }

    // Create a default SSLSetupHandler that accepts any certificate
    if (sslContext == null) {
        try {
            sslContext = SSLContext.getDefault();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // Create HTTP connection pool
    BasicNIOConnFactory nioConnFactory = new BasicNIOConnFactory(sslContext, null, params);
    pool = new BasicNIOConnPool(ioReactor, nioConnFactory, params);
    // Limit total number of connections to 500 by default
    pool.setDefaultMaxPerRoute(Integer.getInteger("com.googlecode.jsonrpc4j.async.max.inflight.route", 500));
    pool.setMaxTotal(Integer.getInteger("com.googlecode.jsonrpc4j.async.max.inflight.total", 500));

    // Run the I/O reactor in a separate thread
    Thread t = new Thread(new Runnable() {
        public void run() {
            try {
                // Create client-side HTTP protocol handler
                HttpAsyncRequestExecutor protocolHandler = new HttpAsyncRequestExecutor();
                // Create client-side I/O event dispatch
                IOEventDispatch ioEventDispatch = new DefaultHttpClientIODispatch(protocolHandler, sslContext,
                        params);
                // Ready to go!
                ioReactor.execute(ioEventDispatch);
            } catch (InterruptedIOException ex) {
                System.err.println("Interrupted");
            } catch (IOException e) {
                System.err.println("I/O error: " + e.getMessage());
            }
        }

    }, "jsonrpc4j HTTP IOReactor");
    // Start the client thread
    t.setDaemon(true);
    t.start();

    // Create HTTP protocol processing chain
    HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] {
            // Use standard client-side protocol interceptors
            new RequestContent(), new RequestTargetHost(), new RequestConnControl(), new RequestUserAgent(),
            new RequestExpectContinue() });

    // Create HTTP requester
    requester = new HttpAsyncRequester(httpproc, new DefaultConnectionReuseStrategy(), params);
}