【问题标题】:httpAsyncClient.execute() callback method not invoked未调用 httpAsyncClient.execute() 回调方法
【发布时间】:2018-03-19 17:36:45
【问题描述】:
package com.lt.uadb.app.resource.test;

import java.util.concurrent.Future;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.nio.client.DefaultHttpAsyncClient;
import org.apache.http.nio.client.HttpAsyncClient;
import org.junit.Test;

public class AsyncSampleResourceTest {
    private static final String PATH = "http://192.168.1.112:8080/uadb.app/rest/sample/async/get";

    @Test
    public void testHttpAsyncClientFutureCallBack() throws Exception {
        final HttpAsyncClient httpAsyncClient = new DefaultHttpAsyncClient();
        httpAsyncClient.start();
        final HttpGet request = new HttpGet(PATH);
        try {
            for (int i = 0; i < 5; i++) {
                System.out.println("*****get--------------");
                Future<HttpResponse> future = httpAsyncClient.execute(request,
                        new FutureCallback<HttpResponse>() {
                            @Override
                            public void completed(HttpResponse httpResponse) {
                                System.out
                                        .println("*****completed--------------");
                            }

                            @Override
                            public void failed(Exception e) {
                                System.out.println("*****failed--------------");
                            }

                            @Override
                            public void cancelled() {
                                System.out
                                        .println("*****cancelled--------------");
                            }
                        });
                // if run code in try catch,the rest service will invoke
                // try {
                // HttpResponse result = future.get();
                // if (result != null) {
                // System.out.println("Request successfully executed");
                // } else {
                // System.out.println("Request failed");
                // }
                // } catch (InterruptedException | ExecutionException e1) {
                // e1.printStackTrace();
                // }
            }
            System.out.println("*****loop--------------");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("shutting down");
            httpAsyncClient.getConnectionManager().shutdown();
        }

    }
}

上面的代码没有调用rest服务,下面是日志-

    *****get--------------
    2015-06-04 16:51:53,372 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 1] start execution
      2015-06-04 16:51:53,385 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 1] Request connection for {}->http://192.168.1.112:8080
      2015-06-04 16:51:53,389 [main] [org.apache.http.impl.nio.conn.PoolingClientAsyncConnectionManager] [DEBUG] - Connection request: [route: {}->http://192.168.1.112:8080][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
      *****get--------------
    2015-06-04 16:51:53,393 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 2] start execution
      2015-06-04 16:51:53,394 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 2] Request connection for {}->http://192.168.1.112:8080
      2015-06-04 16:51:53,394 [main] [org.apache.http.impl.nio.conn.PoolingClientAsyncConnectionManager] [DEBUG] - Connection request: [route: {}->http://192.168.1.112:8080][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
      *****get--------------
    2015-06-04 16:51:53,394 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 3] start execution
      2015-06-04 16:51:53,396 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 3] Request connection for {}->http://192.168.1.112:8080
      2015-06-04 16:51:53,396 [main] [org.apache.http.impl.nio.conn.PoolingClientAsyncConnectionManager] [DEBUG] - Connection request: [route: {}->http://192.168.1.112:8080][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
      *****get--------------
    2015-06-04 16:51:53,396 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 4] start execution
      2015-06-04 16:51:53,397 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 4] Request connection for {}->http://192.168.1.112:8080
      2015-06-04 16:51:53,397 [main] [org.apache.http.impl.nio.conn.PoolingClientAsyncConnectionManager] [DEBUG] - Connection request: [route: {}->http://192.168.1.112:8080][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
      *****get--------------
    2015-06-04 16:51:53,397 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 5] start execution
      2015-06-04 16:51:53,397 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 5] Request connection for {}->http://192.168.1.112:8080
      2015-06-04 16:51:53,397 [main] [org.apache.http.impl.nio.conn.PoolingClientAsyncConnectionManager] [DEBUG] - Connection request: [route: {}->http://192.168.1.112:8080][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
      *****loop--------------
    shutting down
    2015-06-04 16:51:53,398 [main] [org.apache.http.impl.nio.conn.PoolingClientAsyncConnectionManager] [DEBUG] - Connection manager is shutting down
      2015-06-04 16:51:53,401 [Thread-0] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 1] Cancelled
      *****cancelled--------------
    2015-06-04 16:51:53,402 [main] [org.apache.http.impl.nio.conn.PoolingClientAsyncConnectionManager] [DEBUG] - Connection manager shut down
      2015-06-04 16:51:53,402 [Thread-0] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 2] Cancelled
      *****cancelled--------------
    2015-06-04 16:51:53,402 [Thread-0] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 3] Cancelled
      *****cancelled--------------
    2015-06-04 16:51:53,402 [Thread-0] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 4] Cancelled
      *****cancelled--------------
    2015-06-04 16:51:53,403 [Thread-0] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 5] Cancelled
      *****cancelled--------------

completed 没有被记录。

如果我如下所示切换代码的​​注释 -

    try {
  HttpResponse result = future.get();
  if (result != null) {
    System.out.println("Request successfully executed");
  } else {
    System.out.println("Request failed");
  }
} catch (InterruptedException | ExecutionException e1) {
  e1.printStackTrace();
}

然后会调用剩下的服务,如下图-

      *****get-------------- 
      *****completed--------------
    *****loop--------------

    *****get-------------- 
      *****completed--------------
    *****loop--------------

    *****get-------------- 
      *****completed--------------
    *****loop--------------

    shutting down 

似乎 async 没有被调用,如果它调用 async,那么它将像这样记录 -

      *****get-------------- 
    *****loop--------------

    *****get-------------- 
    *****loop--------------

    *****get-------------- 
    *****loop--------------


      *****completed--------------
      *****completed--------------
      *****completed--------------

    shutting down 

【问题讨论】:

  • 我试过jersey客户端,遇到同样的问题,请求被阻塞,直到服务器响应!
  • 如果 execut() 没有调用 .get() ,服务器没有响应,并且抛出取消像 [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange : 5] 取消

标签: java


【解决方案1】:

future.get() 将等到得到响应。所以调用它会使http请求同步。只要让你的程序稍等片刻,就可以得到你期望的输出了。

        final CountDownLatch latch = new CountDownLatch(requests.length);  
        for (final HttpGet request : requests) {  
            httpclient.execute(request, new FutureCallback<HttpResponse>() {  

                @Override  
                public void completed(final HttpResponse response) {  
                    latch.countDown();  
                    System.out.println("******completed");  
                }  
                @Override  
                public void failed(final Exception ex) {  
                    latch.countDown();  
                    System.out.println("*******failed");  
                }  
                @Override  
                public void cancelled() {  
                    latch.countDown();  
                    System.out.println("********canceled");  
                }  
            });  
        }  
        latch.await();  
        System.out.println("Shutting down");  

【讨论】:

  • 谢谢,这帮助我解决了响应同步问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-03
  • 1970-01-01
相关资源
最近更新 更多