我可以使用一个 PoolingNHttpClientConnectionManager 运行多个 HttpAsyncClient 吗?

问题描述 投票:0回答:1

我想使用单个连接管理器创建多个

HttpAsyncClient
(类似于 Baeldung 文章中的示例)。

问题是这似乎不适用于

HttpAsyncClient
(仅适用于
HttpClient
)。应用程序失败并在日志中显示以下错误:

14:00:56.443 [pool-2-thread-1] ERROR org.apache.http.impl.nio.client.InternalHttpAsyncClient - I/O reactor terminated abnormally
java.lang.IllegalStateException: Illegal state ACTIVE
    at org.apache.http.util.Asserts.check(Asserts.java:46)
    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:316)
    at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.execute(PoolingNHttpClientConnectionManager.java:221)
    at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase$1.run(CloseableHttpAsyncClientBase.java:64)
    at java.base/java.lang.Thread.run(Thread.java:833)

Exception in thread "main" java.util.concurrent.ExecutionException: java.util.concurrent.CancellationException: Request execution cancelled
    at org.apache.http.concurrent.BasicFuture.getResult(BasicFuture.java:71)
    at org.apache.http.concurrent.BasicFuture.get(BasicFuture.java:84)
    at org.apache.http.impl.nio.client.FutureWrapper.get(FutureWrapper.java:70)
    at com.example.Test.main(Test.java:32)
Caused by: java.util.concurrent.CancellationException: Request execution cancelled
    at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase.execute(CloseableHttpAsyncClientBase.java:114)
    at org.apache.http.impl.nio.client.InternalHttpAsyncClient.execute(InternalHttpAsyncClient.java:138)
    at org.apache.http.impl.nio.client.CloseableHttpAsyncClient.execute(CloseableHttpAsyncClient.java:75)
    at org.apache.http.impl.nio.client.CloseableHttpAsyncClient.execute(CloseableHttpAsyncClient.java:108)
    at org.apache.http.impl.nio.client.CloseableHttpAsyncClient.execute(CloseableHttpAsyncClient.java:92)
    at com.example.Test.main(Test.java:31)

这是我正在运行的代码:

package com.example;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.apache.http.nio.reactor.ConnectingIOReactor;
import org.apache.http.nio.reactor.IOReactorException;

import java.util.concurrent.ExecutionException;

public class Test {
    public static void main(String[] args) throws IOReactorException, ExecutionException, InterruptedException {
        ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(IOReactorConfig.custom().build());
        PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor);
        CloseableHttpAsyncClient client1 = HttpAsyncClientBuilder.create()
                .setConnectionManager(connManager)
                .setRedirectStrategy(new LaxRedirectStrategy())
                .build();
        client1.start();
        CloseableHttpAsyncClient client2 = HttpAsyncClientBuilder.create()
                .setConnectionManager(connManager)
                .build();
        client2.start();

        var res1 = client1.execute(new HttpGet("https://www.google.com"), null);
        res1.get();
        var res2 = client2.execute(new HttpGet("https://www.google.com"), null);
        res2.get();
    }
}

我使用的是https://hc.apache.org/

的4.4.15版本
java httpclient
1个回答
0
投票

告诉两个构建器连接管理器是共享的:

CloseableHttpAsyncClient client1 = HttpAsyncClientBuilder.create()
        .setConnectionManager(connManager)
        .setConnectionManagerShared(true)
        .setRedirectStrategy(new LaxRedirectStrategy())
        .build();
client1.start();
CloseableHttpAsyncClient client2 = HttpAsyncClientBuilder.create()
        .setConnectionManager(connManager)
        .setConnectionManagerShared(true)
        .build();
client2.start();
© www.soinside.com 2019 - 2024. All rights reserved.