Example usage for org.apache.http.nio.protocol BufferingHttpClientHandler BufferingHttpClientHandler

List of usage examples for org.apache.http.nio.protocol BufferingHttpClientHandler BufferingHttpClientHandler

Introduction

In this page you can find the example usage for org.apache.http.nio.protocol BufferingHttpClientHandler BufferingHttpClientHandler.

Prototype

public BufferingHttpClientHandler(final HttpProcessor httpProcessor,
            final HttpRequestExecutionHandler execHandler, final ConnectionReuseStrategy connStrategy,
            final HttpParams params) 

Source Link

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());
            }/*  ww  w.ja  v  a 2  s.c om*/
            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: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   www . j  av  a 2 s  . c o  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: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//w ww.  j av a2  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 www  .ja  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);

    //    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:com.google.acre.script.NHttpClient.java

public NHttpClient(int max_connections) {
    _max_connections = max_connections;//from www.j  a  va  2  s.c  o  m
    _costCollector = CostCollector.getInstance();

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

    BufferingHttpClientHandler handler = new BufferingHttpClientHandler(httpproc,
            new NHttpRequestExecutionHandler(), new DefaultConnectionReuseStrategy(), DEFAULT_HTTP_PARAMS);

    handler.setEventListener(new EventListener() {
        private final static String REQUEST_CLOSURE = "request-closure";

        public void connectionClosed(NHttpConnection conn) {
            // pass (should we be logging this?)
        }

        public void connectionOpen(NHttpConnection conn) {
            // pass (should we be logging this?)
        }

        public void connectionTimeout(NHttpConnection conn) {
            noteException(null, conn);
        }

        void noteException(Exception e, NHttpConnection conn) {
            HttpContext context = conn.getContext();
            NHttpClientClosure closure = (NHttpClientClosure) context.getAttribute(REQUEST_CLOSURE);
            if (closure != null)
                closure.exceptions().add(e);
        }

        public void fatalIOException(IOException e, NHttpConnection conn) {
            noteException(e, conn);
        }

        public void fatalProtocolException(HttpException e, NHttpConnection conn) {
            noteException(e, conn);
        }
    });

    try {
        SSLContext sctx = SSLContext.getInstance("SSL");
        sctx.init(null, null, null);
        _dispatch = new NHttpAdaptableSensibleAndLogicalIOEventDispatch(handler, sctx, DEFAULT_HTTP_PARAMS);
    } catch (java.security.KeyManagementException e) {
        throw new RuntimeException(e);
    } catch (java.security.NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }

    _requests = new ArrayList<NHttpClientClosure>();
    _connection_lock = new Semaphore(_max_connections);

}