HttpAsyncClient PoolingNHttpClientConnectionManager.requestConnection failed to return?

1.6k Views Asked by At

Following is the code to request NHttpClientConnection from PoolingNHttpClientConnectionManager. The call connFuture.get(), fails to return. Anyone knows why? I am using HttpAsyncClient library httpasyncclient-4.0.1.jar

static NHttpClientConnection httpConn = null;
public static void testOne() throws Exception {
    ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
    PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor);
    connManager.setMaxTotal(100);

    long connectTimeout=1;
    long leaseTimeout=4;
    TimeUnit timeUnit = TimeUnit.SECONDS;
    Object state = null;

    HttpRoute route = new HttpRoute(new HttpHost("www.google.com", 80));
    Future<NHttpClientConnection> connFuture = connManager.requestConnection(route, state, connectTimeout, leaseTimeout, timeUnit, 
            new FutureCallback<NHttpClientConnection>() {
        public void completed(final NHttpClientConnection c) {
            System.out.println("completed");
            httpConn = c;
        }
        public void failed(final Exception ex) {
            System.out.println("failed");
        }
        public void cancelled() {
            System.out.println("cancelled");
        }
    } );
    System.out.println("Step3");
    connFuture.get(); // Failed to return
    System.out.println("Done");
}
1

There are 1 best solutions below

0
On

I got it. ioReactor needs to be started. Here is the code that works.

static NHttpClientConnection httpConn = null;
public static void testOne() throws Exception {

    HttpAsyncRequestExecutor protocolHandler = new HttpAsyncRequestExecutor();
    // Create client-side I/O event dispatch
    final IOEventDispatch ioEventDispatch = new DefaultHttpClientIODispatch(protocolHandler, ConnectionConfig.DEFAULT);

    final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
    PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor);
    connManager.setMaxTotal(100);

    long connectTimeout=1;
    long leaseTimeout=4;
    TimeUnit timeUnit = TimeUnit.SECONDS;
    Object state = null;

    //HttpRoute route = new HttpRoute(new HttpHost("www.google.com", 80));
    HttpRoute route = new HttpRoute(new HttpHost("www.google.com"));

     // Run the I/O reactor in a separate thread
    Thread t = new Thread(new Runnable() {
        public void run() {
            try {
                // Ready to go!
                ioReactor.execute(ioEventDispatch); 
            } catch (InterruptedIOException ex) {
                System.err.println("Interrupted");
            } catch (IOException e) {
                System.err.println("I/O error: " + e.getMessage());
            }
            System.out.println("Shutdown");
        }

    });
    t.start();

    Future<NHttpClientConnection> connFuture = connManager.requestConnection(route, state, connectTimeout, leaseTimeout, timeUnit, 
            new FutureCallback<NHttpClientConnection>() {
        public void completed(final NHttpClientConnection c) {
            System.out.println("completed");
            httpConn = c;
        }
        public void failed(final Exception ex) {
            System.out.println("failed");
        }
        public void cancelled() {
            System.out.println("cancelled");
        }
    } );
    System.out.println("Step3");
    connFuture.get();
    System.out.println("Done");
    ioReactor.shutdown();
}