httpclient5-beta

时间:2018-05-17 06:00:05

标签: apache-httpcomponents apache-httpclient-5.x

您好我正在尝试使用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:端点是   没有连接   org.apache.hc.core5.util.Asserts.check(Asserts.java:38)at   org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager $ InternalConnectionEndpoint.getValidatedPoolEntry(PoolingAsyncClientConnectionManager.java:497)     在   org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager $ InternalConnectionEndpoint.execute(PoolingAsyncClientConnectionManager.java:552)     在   org.apache.hc.client5.http.impl.async.MinimalHttpAsyncClient $ InternalAsyncClientEndpoint.execute(MinimalHttpAsyncClient.java:405)     在   org.apache.hc.core5.http.nio.AsyncClientEndpoint.execute(AsyncClientEndpoint.java:81)     在   org.apache.hc.core5.http.nio.AsyncClientEndpoint.execute(AsyncClientEndpoint.java:114)

端点可能有什么问题,我强迫终端保持活力一天,请对此有所了解

0 个答案:

没有答案