1

您好我正在尝试使用httpcomponents5 beta进行持久连接,我已经尝试了他们网站中给出的示例,代码如下,

final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(45)).setSelectInterval(10000).setSoReuseAddress(true).setSoKeepAlive(true).build();
final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(new TrustAllStrategy()).build();
final PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder.create().setConnectionTimeToLive(TimeValue.of(1, TimeUnit.DAYS)).setTlsStrategy(new H2TlsStrategy(sslContext, NoopHostnameVerifier.INSTANCE)).build();
client = HttpAsyncClients.createMinimal(protocol, H2Config.DEFAULT, null, ioReactorConfig, connectionManager);
client.start();
final org.apache.hc.core5.http.HttpHost target = new org.apache.hc.core5.http.HttpHost("localhost", 8000, "https");
Future<AsyncClientEndpoint> leaseFuture = client.lease(target, null);
AsyncClientEndpoint asyncClientEndpoint = leaseFuture.get(60, TimeUnit.SECONDS);
final CountDownLatch latch = new CountDownLatch(1);
final AsyncRequestProducer requestProducer = AsyncRequestBuilder.post(target.getSchemeName()+"://"+target.getHostName()+":"+target.getPort()+locationposturl).addParameter(new BasicNameValuePair("info", requestData)).setEntity(new StringAsyncEntityProducer("json post data will go here", ContentType.APPLICATION_JSON)).setHeader("Pragma", "no-cache").setHeader("from", "http5").setHeader("Custom", customheaderName).setHeader("Secure", secureHeader).build();
locEndPoint.execute(requestProducer, SimpleResponseConsumer.create(), new FutureCallback<SimpleHttpResponse>() {
    @Override
    public void completed(final SimpleHttpResponse response) {
        if (response != null) {
            if (response.getCode() > -1) {
                try {

                    System.out.println("http5:: COMPLETED : RESPONSE "+response.getBodyText());
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        latch.countDown();
    }

    @Override
    public void failed(final Exception ex) {
        System.out.println("http5:: FAILED : "+target+locationposturl);
        LoggerUtil.printStackTrace(ex);
        System.out.println("http5::Exception Request failed "+LoggerUtil.getStackTrace(ex));
        latch.countDown();
    }

    @Override
    public void cancelled() {
        System.out.println("http5:: CANCELLED : "+target+locationposturl);
        System.out.println(http5::Exception Request cancelled");
        latch.countDown();
    }
});
latch.await();

这段代码第一次没有问题,但是当我发送后续请求时,它会抛出如下异常,

http5:: 发生异常 java.lang.IllegalStateException: Endpoint is not connected at org.apache.hc.core5.util.Asserts.check(Asserts.java:38) at org.apache.hc.client5.http.impl.nio .PoolingAsyncClientConnectionManager$InternalConnectionEndpoint.getValidatedPoolEntry(PoolingAsyncClientConnectionManager.java:497) at org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager$InternalConnectionEndpoint.execute(PoolingAsyncClientConnectionManager.java:552) at org.apache.hc.client5.http .impl.async.MinimalHttpAsyncClient$InternalAsyncClientEndpoint.execute(MinimalHttpAsyncClient.java:405) at org.apache.hc.core5.http.nio.AsyncClientEndpoint.execute(AsyncClientEndpoint.java:81) at org.apache.hc.core5.http .nio.AsyncClientEndpoint.execute(AsyncClientEndpoint.java:114)

端点可能是什么问题,我正在强制端点存活一天,请对此有所了解

4

0 回答 0