0

Ниже приведен код для запроса NHttpClientConnection из PoolingNHttpClientConnectionManager. Вызов connFuture.get() не возвращается. Кто-нибудь знает почему? Я использую библиотеку HttpAsyncClient httpasyncclient-4.0.1.jarHttpAsyncClient PoolingNHttpClientConnectionManager.requestConnection не удалось вернуться?

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"); 
} 

ответ

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(); 
}