0

以下是从 PoolingNHttpClientConnectionManager 请求 NHttpClientConnection 的代码。调用 connFuture.get() 无法返回。有谁知道为什么?我正在使用 HttpAsyncClient 库 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");
}
4

1 回答 1

0

我知道了。ioReactor 需要启动。这是有效的代码。

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();
}
于 2015-08-14T22:05:24.310 回答