gpt4 book ai didi

java - RESTEasy 线程中的 ProxyFactory 替代品是否安全?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:14:57 25 4
gpt4 key购买 nike

我使用 ProxyFactory 和 ClientExecutor 在 RESTEasy 中开发了一个服务,如下所示:

PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 5000);
ClientExecutor clientExecutor = new ApacheHttpClient4Executor(httpClient);
MyClass client = ProxyFactory.create(MyClass.class, "http://www.example.com", clientExecutor);

它始终运行良好。 RESTEasy deprecated both ClientExecutor 和 ProxyFactory 后,他们提供了一个新的 ResteasyClient 用于外部连接,但我不知道这个新的 ResteasyClient 是否是线程安全的。这是文档中的新示例代码:

ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("http://example.com/base/uri");

SimpleClient simple = target.proxy(SimpleClient.class);

更新:我将代码与 ResteasyClient 一起使用,但我遇到了很多这样的错误:

javax.ws.rs.ProcessingException: Unable to invoke request

造成

java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated. Make sure to release the connection before allocating another one.

最佳答案

我们使用这个:

    final ResteasyClient client = new ResteasyClientBuilder()
.connectionPoolSize(10)
.maxPooledPerRoute(5)
.build();

调试后我发现(至少在我们的情况下)RESTEasy 客户端默认使用 ThreadSafeClientConnManager,所以我认为没有必要指定一个不同的,尽管根据 JavaDoc,它已被弃用,取而代之的是 PoolingHttpClientConnectionManager(注意额外的 Http)。但这已在 RESTEasy 客户端 3.0.5 中修复。最终:https://issues.jboss.org/browse/RESTEASY-948

那里是 HTTP 连接管理器的丛林..

关于java - RESTEasy 线程中的 ProxyFactory 替代品是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21717130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com