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

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

Introduction

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

Prototype

public DefaultClientIOEventDispatch(final NHttpClientHandler handler, final HttpParams params) 

Source Link

Document

Creates a new instance of this class to be used for dispatching I/O event notifications to the given protocol handler.

Usage

From source file:wh.contrib.RewardOptimizerHttpClient.java

public static void main(String[] args) throws Exception {
    HttpParams params = new BasicHttpParams();
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 30000)
            .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000)
            .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 64 * 1024)
            .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
            .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
            .setParameter(CoreProtocolPNames.USER_AGENT,
                    "HttpComponents/1.1 (RewardOptimizer - karlthepagan@gmail.com)");

    final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(2, params);

    BasicHttpProcessor httpproc = new BasicHttpProcessor();
    httpproc.addInterceptor(new RequestContent());
    httpproc.addInterceptor(new RequestTargetHost());
    httpproc.addInterceptor(new RequestConnControl());
    httpproc.addInterceptor(new RequestUserAgent());
    httpproc.addInterceptor(new RequestExpectContinue());

    // We are going to use this object to synchronize between the 
    // I/O event and main threads
    RequestCount requestCount = new RequestCount(1);

    BufferingHttpClientHandler handler = new BufferingHttpClientHandler(httpproc,
            new MyHttpRequestExecutionHandler(requestCount), new DefaultConnectionReuseStrategy(), params);

    handler.setEventListener(new EventLogger());

    final IOEventDispatch ioEventDispatch = new DefaultClientIOEventDispatch(handler, params);

    Thread t = new Thread(new Runnable() {

        public void run() {
            try {
                ioReactor.execute(ioEventDispatch);
            } catch (InterruptedIOException ex) {
                System.err.println("Interrupted");
            } catch (IOException e) {
                System.err.println("I/O error: " + e.getMessage());
            }//from ww w . j a  v  a  2 s . co  m
            System.out.println("Shutdown");
        }

    });
    t.start();

    List<SessionRequest> reqs = new ArrayList<SessionRequest>();
    //        reqs.add(ioReactor.connect(
    //                new InetSocketAddress("www.yahoo.com", 80), 
    //                null, 
    //                new HttpHost("www.yahoo.com"),
    //                null));
    //        reqs.add(ioReactor.connect(
    //                new InetSocketAddress("www.google.com", 80), 
    //                null,
    //                new HttpHost("www.google.ch"),
    //                null));
    //        reqs.add(ioReactor.connect(
    //                new InetSocketAddress("www.apache.org", 80), 
    //                null,
    //                new HttpHost("www.apache.org"),
    //                null));

    reqs.add(ioReactor.connect(new InetSocketAddress("www.wowhead.com", 80), null,
            new HttpHost("www.wowhead.com"), null));

    // Block until all connections signal
    // completion of the request execution
    synchronized (requestCount) {
        while (requestCount.getValue() > 0) {
            requestCount.wait();
        }
    }

    System.out.println("Shutting down I/O reactor");

    ioReactor.shutdown();

    System.out.println("Done");
}

From source file:org.eclipse.ecf.provider.filetransfer.httpcore.NHttpClient.java

public static void main(String[] args) throws Exception {
    HttpParams params = new BasicHttpParams();
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
            .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000)
            .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
            .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
            .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
            .setParameter(CoreProtocolPNames.USER_AGENT, "HttpComponents/1.1");

    final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(2, params);

    BasicHttpProcessor httpproc = new BasicHttpProcessor();
    httpproc.addInterceptor(new RequestContent());
    httpproc.addInterceptor(new RequestTargetHost());
    httpproc.addInterceptor(new RequestConnControl());
    httpproc.addInterceptor(new RequestUserAgent());
    httpproc.addInterceptor(new RequestExpectContinue());

    // We are going to use this object to synchronize between the 
    // I/O event and main threads
    //CountDownLatch requestCount = new CountDownLatch(3);
    CountDownLatch requestCount = new CountDownLatch(1);

    BufferingHttpClientHandler handler = new MyBufferingHttpClientHandler(httpproc,
            new MyHttpRequestExecutionHandler(requestCount), new DefaultConnectionReuseStrategy(), params);

    handler.setEventListener(new EventLogger());

    final IOEventDispatch ioEventDispatch = new DefaultClientIOEventDispatch(handler, params);

    Thread t = new Thread(new Runnable() {

        public void run() {
            try {
                ioReactor.execute(ioEventDispatch);
            } catch (InterruptedIOException ex) {
                System.err.println("Interrupted");
            } catch (IOException e) {
                System.err.println("I/O error: " + e.getMessage());
            }/*from   w  w  w .j ava2 s  .  c o  m*/
            System.out.println("Shutdown");
        }

    });
    t.start();

    SessionRequest[] reqs = new SessionRequest[1];
    reqs[0] = ioReactor.connect(new InetSocketAddress("ftp.osuosl.org", 80), null,
            new HttpHost("ftp.osuosl.org"), new MySessionRequestCallback(requestCount));
    // Block until all connections signal
    // completion of the request execution
    requestCount.await();

    System.out.println("Shutting down I/O reactor");

    ioReactor.shutdown();

    System.out.println("Done");
}

From source file:NHttpClient.java

public static void main(String[] args) throws Exception {
    HttpParams params = new BasicHttpParams();
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
            .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000)
            .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
            .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
            .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
            .setParameter(CoreProtocolPNames.USER_AGENT, "HttpComponents/1.1");

    final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(2, params);

    BasicHttpProcessor httpproc = new BasicHttpProcessor();
    httpproc.addInterceptor(new RequestContent());
    httpproc.addInterceptor(new RequestTargetHost());
    httpproc.addInterceptor(new RequestConnControl());
    httpproc.addInterceptor(new RequestUserAgent());
    httpproc.addInterceptor(new RequestExpectContinue());

    // We are going to use this object to synchronize between the 
    // I/O event and main threads
    CountDownLatch requestCount = new CountDownLatch(3);

    BufferingHttpClientHandler handler = new BufferingHttpClientHandler(httpproc,
            new MyHttpRequestExecutionHandler(requestCount), new DefaultConnectionReuseStrategy(), params);

    handler.setEventListener(new EventLogger());

    final IOEventDispatch ioEventDispatch = new DefaultClientIOEventDispatch(handler, params);

    Thread t = new Thread(new Runnable() {

        public void run() {
            try {
                ioReactor.execute(ioEventDispatch);
            } catch (InterruptedIOException ex) {
                System.err.println("Interrupted");
            } catch (IOException e) {
                System.err.println("I/O error: " + e.getMessage());
            }//from   w ww .  j a v  a 2 s .co m
            System.out.println("Shutdown");
        }

    });
    t.start();

    SessionRequest[] reqs = new SessionRequest[3];
    reqs[0] = ioReactor.connect(new InetSocketAddress("www.yahoo.com", 80), null, new HttpHost("www.yahoo.com"),
            new MySessionRequestCallback(requestCount));
    reqs[1] = ioReactor.connect(new InetSocketAddress("www.google.com", 80), null,
            new HttpHost("www.google.ch"), new MySessionRequestCallback(requestCount));
    reqs[2] = ioReactor.connect(new InetSocketAddress("www.apache.org", 80), null,
            new HttpHost("www.apache.org"), new MySessionRequestCallback(requestCount));

    // Block until all connections signal
    // completion of the request execution
    requestCount.await();

    System.out.println("Shutting down I/O reactor");

    ioReactor.shutdown();

    System.out.println("Done");
}

From source file:org.frameworkset.spi.remote.http.HttpClient.java

private void execute(final NHttpClientHandler clientHandler) throws IOException {
    IOEventDispatch ioEventDispatch = new DefaultClientIOEventDispatch(clientHandler, this.params);

    this.ioReactor.execute(ioEventDispatch);
}

From source file:com.couchbase.client.http.AsyncConnectionManager.java

public void execute() throws IOException {
    IOEventDispatch dispatch = new DefaultClientIOEventDispatch(new ManagedClientHandler(this.handler, this),
            this.params);
    this.ioreactor.execute(dispatch);
}

From source file:net.kungfoo.grizzly.proxy.impl.Activator.java

private static void setupClient() throws IOReactorException {
    HttpParams params = new BasicHttpParams();
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 30000)
            .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
            .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
            .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true);

    connectingIOReactor = new DefaultConnectingIOReactor(1, params);

    BasicHttpProcessor originServerProc = new BasicHttpProcessor();
    originServerProc.addInterceptor(new RequestContent());
    originServerProc.addInterceptor(new RequestTargetHost());
    originServerProc.addInterceptor(new RequestConnControl());
    originServerProc.addInterceptor(new RequestUserAgent());
    originServerProc.addInterceptor(new RequestExpectContinue());

    NHttpClientHandler connectingHandler = new ConnectingHandler(originServerProc,
            new DefaultConnectionReuseStrategy(), params);

    connectingEventDispatch = new DefaultClientIOEventDispatch(connectingHandler, params);
}

From source file:org.jclouds.http.httpnio.pool.NioHttpCommandConnectionPool.java

public static IOEventDispatch provideClientEventDispatch(AsyncNHttpClientHandler handler, HttpParams params) {
    return new DefaultClientIOEventDispatch(handler, params);
}

From source file:NioHttpClient.java

public NioHttpClient(String user_agent, HttpRequestExecutionHandler request_handler,
        EventListener connection_listener) throws Exception {

    // Construct the long-lived HTTP parameters.
    HttpParams parameters = new BasicHttpParams();
    parameters//from ww w.  j  a va  2  s .c  o  m
            // Socket data timeout is 5,000 milliseconds (5 seconds).
            .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
            // Maximum time allowed for connection establishment is 10,00 milliseconds (10 seconds).
            .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000)
            // Socket buffer size is 8 kB.
            .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
            // Don't bother to check for stale TCP connections.
            .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
            // Don't use Nagle's algorithm (in other words minimize latency).
            .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
            // Set the user agent string that the client sends to the server.
            .setParameter(CoreProtocolPNames.USER_AGENT, user_agent);

    // Construct the core HTTP request processor.
    BasicHttpProcessor http_processor = new BasicHttpProcessor();
    // Add Content-Length header to request where appropriate.
    http_processor.addInterceptor(new RequestContent());
    // Always include Host header in requests.
    http_processor.addInterceptor(new RequestTargetHost());
    // Maintain connection keep-alive by default.
    http_processor.addInterceptor(new RequestConnControl());
    // Include user agent information in each request.
    http_processor.addInterceptor(new RequestUserAgent());

    // Allocate an HTTP client handler.
    BufferingHttpClientHandler client_handler = new BufferingHttpClientHandler(http_processor, // Basic HTTP Processor.
            request_handler, new DefaultConnectionReuseStrategy(), parameters);
    client_handler.setEventListener(connection_listener);

    // Use two worker threads for the IO reactor.
    io_reactor = new DefaultConnectingIOReactor(2, parameters);
    io_event_dispatch = new DefaultClientIOEventDispatch(client_handler, parameters);
}

From source file:NIO_HTTP_Client.java

public NIO_HTTP_Client(String user_agent, HttpRequestExecutionHandler request_handler,
        EventListener connection_listener) throws Exception {

    // Construct the long-lived HTTP parameters.
    HttpParams parameters = new BasicHttpParams();
    parameters/*from   ww  w.j  ava2 s .  c om*/
            // Socket data timeout is 5,000 milliseconds (5 seconds).
            .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
            // Maximum time allowed for connection establishment is 10,00 milliseconds (10 seconds).
            .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000)
            // Socket buffer size is 8 kB.
            .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
            // Don't bother to check for stale TCP connections.
            .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
            // Don't use Nagle's algorithm (in other words minimize latency).
            .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
            // Set the user agent string that the client sends to the server.
            .setParameter(CoreProtocolPNames.USER_AGENT, user_agent);

    // Construct the core HTTP request processor.
    BasicHttpProcessor http_processor = new BasicHttpProcessor();
    // Add Content-Length header to request where appropriate.
    http_processor.addInterceptor(new RequestContent());
    // Always include Host header in requests.
    http_processor.addInterceptor(new RequestTargetHost());
    // Maintain connection keep-alive by default.
    http_processor.addInterceptor(new RequestConnControl());
    // Include user agent information in each request.
    http_processor.addInterceptor(new RequestUserAgent());

    // Allocate an HTTP client handler.
    BufferingHttpClientHandler client_handler = new BufferingHttpClientHandler(http_processor, // Basic HTTP Processor.
            request_handler, new DefaultConnectionReuseStrategy(), parameters);
    client_handler.setEventListener(connection_listener);

    // Use two worker threads for the IO reactor.
    io_reactor = new DefaultConnectingIOReactor(2, parameters);
    io_event_dispatch = new DefaultClientIOEventDispatch(client_handler, parameters);

    //    t = new Thread(new Runnable() {
    //             public void run() {
    //                 try {
    //                     io_reactor.execute(io_event_dispatch);
    //                 } catch (InterruptedIOException ex) {
    //                     System.err.println("Interrupted");
    //                 } catch (IOException e) {
    //                     System.err.println("I/O error: " + e.getMessage());
    //           Throwable c = e.getCause();
    //           if (c != null) {
    //          System.err.println("Cause: " + c.toString());
    //           }else {
    //          System.err.println("Cause: unknown");
    //           }
    //                 }
    //                 System.out.println("Shutdown");
    //             }
    //    });
    //    t.start();
}

From source file:org.apache.axis2.transport.nhttp.HttpCoreNIOSender.java

/**
 * Return the IOEventDispatch implementation to be used. This is overridden by the
 * SSL sender/*from  ww  w  .  j  a v  a 2  s . c  o  m*/
 * @param handler
 * @param sslContext
 * @param params
 * @return
 */
protected IOEventDispatch getEventDispatch(NHttpClientHandler handler, SSLContext sslContext,
        HttpParams params) {
    return new DefaultClientIOEventDispatch(handler, params);
}