CloseableHttpAsyncClient:请求在不同时间返回多个响应的服务器

问题描述

我需要向服务器发出放置请求,该请求在不同时间返回第一个响应(HTTP/1.1 204 无内容)和第二个响应(EVENT/1.0 200 OK)。我的代码基于 https://hc.apache.org/httpcomponents-asyncclient-ga/quickstart.html

private static void getCallback(CloseableHttpAsyncclient client) {
     System.out.println("Start Request");

     final CountDownLatch latch = new CountDownLatch(2);
     final HttpPut putRequest = new HttpPut("http://192.168.178.44:8080/characteristics");
     JsonArray j = new JsonArray();
     j.add("{\"characteristics\":[{\"iid\":571230650403,\"aid\":6623462419719038,\"ev\":true}]}");
     putRequest.setEntity(new StringEntity(j.getAsstring()));       
     HttpAsyncRequestProducer producer = HttpAsyncmethods.create(putRequest);

     AsyncCharConsumer<HttpResponse> consumer = new AsyncCharConsumer<HttpResponse>() {
           HttpResponse response;
           @Override
           protected void onResponseReceived(final HttpResponse response) {
               System.out.printf("onResponseReceived: " +  response);
               this.response = response;
           }

           @Override
           protected void onCharReceived(final CharBuffer buf,final IOControl ioctrl) throws IOException {
               System.out.printf("onCharReceived: %d\n",buf.length());
            }

           @Override
           protected void releaseResources() {
           }

           @Override
           protected HttpResponse buildresult(final HttpContext context) {
                System.out.println("Context: " + context);
                return this.response;
           }
    };

    client.execute(producer,consumer,new FutureCallback<HttpResponse>() {

        public void completed(final HttpResponse response) {
                    latch.countDown();
                     System.out.println("Completed: " + putRequest.getRequestLine() + "->" + response3.getStatusLine());
        }
                
        public void Failed(final Exception ex) {
                    latch.countDown();
                    System.out.println(putRequest.getRequestLine() + "->" + ex);
        }

        public void cancelled() {
                    latch.countDown();
                    System.out.println(putRequest.getRequestLine() + " cancelled");
        }
                
    });
    latch.await();
}

不幸的是,我得到异常

...
Start Request
2021/02/05 19:05:52:552 MEZ [DEBUG] MainClientExec - [exchange: 3] start execution
2021/02/05 19:05:52:556 MEZ [DEBUG] RequestAddCookies - CookieSpec selected: default
2021/02/05 19:05:52:557 MEZ [DEBUG] RequestAuthCache - Auth cache not set in the context
2021/02/05 19:05:52:557 MEZ [DEBUG] InternalHttpAsyncclient - [exchange: 3] Request connection for {}->http://192.168.178.44:8080
2021/02/05 19:05:52:557 MEZ [DEBUG] PoolingNHttpClientConnectionManager - Connection request: [route: {}->http://192.168.178.44:8080][total kept alive: 1; route allocated: 1 of 2; total allocated: 1 of 20]
2021/02/05 19:05:52:557 MEZ [DEBUG] ManagednHttpClientConnectionImpl - http-outgoing-0 http.HAPIOSession@5cc88c2c: Set timeout 0
2021/02/05 19:05:52:557 MEZ [DEBUG] PoolingNHttpClientConnectionManager - Connection leased: [id: http-outgoing-0][route: {}->http://192.168.178.44:8080][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
2021/02/05 19:05:52:557 MEZ [DEBUG] InternalHttpAsyncclient - [exchange: 3] Connection allocated: CPoolProxy{http-outgoing-0 [ACTIVE]}
2021/02/05 19:05:52:559 MEZ [DEBUG] ManagednHttpClientConnectionImpl - http-outgoing-0 http.HAPIOSession@5cc88c2c: Set attribute http.nio.exchange-handler
2021/02/05 19:05:52:559 MEZ [DEBUG] ManagednHttpClientConnectionImpl - http-outgoing-0 http.HAPIOSession@5cc88c2c: Event set [w]
2021/02/05 19:05:52:559 MEZ [DEBUG] InternalIOdispatch - http-outgoing-0 [ACTIVE] Request ready
2021/02/05 19:05:52:559 MEZ [DEBUG] MainClientExec - [exchange: 3] Attempt 1 to execute request
2021/02/05 19:05:52:560 MEZ [DEBUG] MainClientExec - [exchange: 3] Target auth state: UNCHALLENGED
2021/02/05 19:05:52:560 MEZ [DEBUG] MainClientExec - [exchange: 3] Proxy auth state: UNCHALLENGED
2021/02/05 19:05:52:560 MEZ [DEBUG] headers - http-outgoing-0 >> PUT /characteristics HTTP/1.1
2021/02/05 19:05:52:560 MEZ [DEBUG] headers - http-outgoing-0 >> Content-Length: 75
2021/02/05 19:05:52:560 MEZ [DEBUG] headers - http-outgoing-0 >> Content-Type: text/plain; charset=ISO-8859-1
2021/02/05 19:05:52:560 MEZ [DEBUG] headers - http-outgoing-0 >> Host: 192.168.178.44:8080
2021/02/05 19:05:52:560 MEZ [DEBUG] headers - http-outgoing-0 >> Connection: Keep-Alive
2021/02/05 19:05:52:560 MEZ [DEBUG] headers - http-outgoing-0 >> User-Agent: Apache-HttpAsyncclient/4.1.4 (Java/11.0.9.1)
2021/02/05 19:05:52:560 MEZ [DEBUG] ManagednHttpClientConnectionImpl - http-outgoing-0 http.HAPIOSession@5cc88c2c: Event set [w]
2021/02/05 19:05:52:560 MEZ [DEBUG] InternalIOdispatch - http-outgoing-0 [ACTIVE] Output ready
2021/02/05 19:05:52:560 MEZ [DEBUG] MainClientExec - [exchange: 3] produce content
2021/02/05 19:05:52:561 MEZ [DEBUG] MainClientExec - [exchange: 3] Request completed
2021/02/05 19:05:52:561 MEZ [DEBUG] InternalIOdispatch - http-outgoing-0 [ACTIVE] [content length: 75; pos: 75; completed: true]
2021/02/05 19:05:52:562 MEZ [DEBUG] ManagednHttpClientConnectionImpl - http-outgoing-0 http.HAPIOSession@5cc88c2c: 283 bytes written
2021/02/05 19:05:52:563 MEZ [DEBUG] wire - http-outgoing-0 >> "PUT /characteristics HTTP/1.1[\r][\n]"
2021/02/05 19:05:52:563 MEZ [DEBUG] wire - http-outgoing-0 >> "Content-Length: 75[\r][\n]"
2021/02/05 19:05:52:563 MEZ [DEBUG] wire - http-outgoing-0 >> "Content-Type: text/plain; charset=ISO-8859-1[\r][\n]"
2021/02/05 19:05:52:563 MEZ [DEBUG] wire - http-outgoing-0 >> "Host: 192.168.178.44:8080[\r][\n]"
2021/02/05 19:05:52:563 MEZ [DEBUG] wire - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
2021/02/05 19:05:52:563 MEZ [DEBUG] wire - http-outgoing-0 >> "User-Agent: Apache-HttpAsyncclient/4.1.4 (Java/11.0.9.1)[\r][\n]"
2021/02/05 19:05:52:563 MEZ [DEBUG] wire - http-outgoing-0 >> "[\r][\n]"
2021/02/05 19:05:52:563 MEZ [DEBUG] wire - http-outgoing-0 >> "{"characteristics":[{"iid":571230650403,"aid":6623462419719038,"ev":true}]}"
2021/02/05 19:05:52:563 MEZ [DEBUG] InternalIOdispatch - http-outgoing-0 [ACTIVE] Request ready
2021/02/05 19:05:52:564 MEZ [DEBUG] ManagednHttpClientConnectionImpl - http-outgoing-0 http.HAPIOSession@5cc88c2c: Event cleared [w]
2021/02/05 19:05:52:567 MEZ [DEBUG] ManagednHttpClientConnectionImpl - http-outgoing-0 http.HAPIOSession@5cc88c2c: 27 bytes read
2021/02/05 19:05:52:568 MEZ [DEBUG] wire - http-outgoing-0 << "HTTP/1.1 204 No Content[\r][\n]"
2021/02/05 19:05:52:568 MEZ [DEBUG] wire - http-outgoing-0 << "[\r][\n]"
2021/02/05 19:05:52:573 MEZ [DEBUG] headers - http-outgoing-0 << HTTP/1.1 204 No Content
2021/02/05 19:05:52:580 MEZ [DEBUG] InternalIOdispatch - http-outgoing-0 [ACTIVE] Response received
2021/02/05 19:05:52:581 MEZ [DEBUG] MainClientExec - [exchange: 3] Response received HTTP/1.1 204 No Content
2021/02/05 19:05:52:582 MEZ [DEBUG] InternalHttpAsyncclient - [exchange: 3] Connection can be kept alive indefinitely
2021/02/05 19:05:52:582 MEZ [DEBUG] MainClientExec - [exchange: 3] Response processed
2021/02/05 19:05:52:582 MEZ [DEBUG] InternalHttpAsyncclient - [exchange: 3] releasing connection
2021/02/05 19:05:52:583 MEZ [DEBUG] ManagednHttpClientConnectionImpl - http-outgoing-0 http.HAPIOSession@5cc88c2c: Remove attribute http.nio.exchange-handler
2021/02/05 19:05:52:593 MEZ [DEBUG] PoolingNHttpClientConnectionManager - Releasing connection: [id: http-outgoing-0][route: {}->http://192.168.178.44:8080][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
2021/02/05 19:05:52:593 MEZ [DEBUG] PoolingNHttpClientConnectionManager - Connection [id: http-outgoing-0][route: {}->http://192.168.178.44:8080] can be kept alive indefinitely
2021/02/05 19:05:52:594 MEZ [DEBUG] ManagednHttpClientConnectionImpl - http-outgoing-0 http.HAPIOSession@5cc88c2c: Set timeout 0
2021/02/05 19:05:52:594 MEZ [DEBUG] PoolingNHttpClientConnectionManager - Connection released: [id: http-outgoing-0][route: {}->http://192.168.178.44:8080][total kept alive: 1; route allocated: 1 of 2; total allocated: 1 of 20]

Completed: PUT http://192.168.178.44:8080/characteristics HTTP/1.1->HTTP/1.1 204 No Content
2021/02/05 19:05:55:266 MEZ [DEBUG] ManagednHttpClientConnectionImpl - http-outgoing-0 http.HAPIOSession@5cc88c2c: 155 bytes read
2021/02/05 19:05:55:266 MEZ [DEBUG] wire - http-outgoing-0 << "EVENT/1.0 200 OK[\r][\n]"
2021/02/05 19:05:55:266 MEZ [DEBUG] wire - http-outgoing-0 << "Content-Length: 79[\r][\n]"
2021/02/05 19:05:55:266 MEZ [DEBUG] wire - http-outgoing-0 << "Content-Type: application/hap+json[\r][\n]"
2021/02/05 19:05:55:266 MEZ [DEBUG] wire - http-outgoing-0 << "[\r][\n]"
2021/02/05 19:05:55:266 MEZ [DEBUG] wire - http-outgoing-0 << "{"characteristics":[{"aid":6623462419719038,"iid":571230650403,"value":false}]}"
2021/02/05 19:05:55:267 MEZ [DEBUG] headers - http-outgoing-0 << EVENT/1.0 200 OK
2021/02/05 19:05:55:267 MEZ [DEBUG] headers - http-outgoing-0 << Content-Length: 79
2021/02/05 19:05:55:267 MEZ [DEBUG] headers - http-outgoing-0 << Content-Type: application/hap+json
2021/02/05 19:05:55:267 MEZ [DEBUG] InternalIOdispatch - http-outgoing-0 [ACTIVE(79)] Response received
2021/02/05 19:05:55:269 MEZ [DEBUG] InternalIOdispatch - http-outgoing-0 [ACTIVE(79)] Exception <java.lang.NullPointerException: session.getAttribute(name) must not be null>java.lang.NullPointerException: session.getAttribute(name) must not be null
    at proxy.IOSessionProxy.getAttribute(IOSessionProxy.java:60)
    at org.apache.http.impl.nio.conn.LoggingIOSession.getAttribute(LoggingIOSession.java:176)
    at org.apache.http.impl.nio.SessionHttpContext.getAttribute(SessionHttpContext.java:44)
    at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.getHandler(HttpAsyncRequestExecutor.java:420)
    at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.responseReceived(HttpAsyncRequestExecutor.java:264)
    at org.apache.http.impl.nio.client.InternalRequestExecutor.responseReceived(InternalRequestExecutor.java:108)
    at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:255)
    at org.apache.http.impl.nio.client.InternalIOdispatch.onInputReady(InternalIOdispatch.java:81)
    at org.apache.http.impl.nio.client.InternalIOdispatch.onInputReady(InternalIOdispatch.java:39)
    at org.apache.http.impl.nio.reactor.AbstractIOdispatch.inputReady(AbstractIOdispatch.java:114)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:591)
    at java.base/java.lang.Thread.run(Thread.java:834)

2021/02/05 19:05:55:276 MEZ [DEBUG] ManagednHttpClientConnectionImpl - http-outgoing-0 http.HAPIOSession@5cc88c2c: Shutdown
2021/02/05 19:05:55:277 MEZ [DEBUG] InternalIOdispatch - http-outgoing-0 [CLOSED]: disconnected
..

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...