java-RESTEasy线程中的ProxyFactory替换是否安全?
作者:互联网
我使用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不推荐使用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连接管理器的丛林.
标签:multithreading,client,resteasy,java,proxyfactory 来源: https://codeday.me/bug/20191029/1963509.html