我在RESTEasy中使用ProxyFactory和ClientExecutor开发了一个服务,如下所示:
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.发布于 2014-12-08 16:08:13
我们用这个:
final ResteasyClient client = new ResteasyClientBuilder()
.connectionPoolSize(10)
.maxPooledPerRoute(5)
.build();调试之后,我发现(至少在我们的情况下) RESTEasy客户端默认使用ThreadSafeClientConnManager,因此我认为没有必要指定不同的ThreadSafeClientConnManager,尽管根据JavaDoc,它被反对为支持PoolingHttpClientConnectionManager (注意额外的Http)。但是在RESTEasy客户机3.0.5中已经修复了这个问题。fixed:https://issues.jboss.org/browse/RESTEASY-948
这是一片HTTP连接管理器的丛林。
发布于 2014-08-07 17:30:08
这对我有用。只需要找到用于设置Apache引擎的钩子。主要基于RestEasy 3.0.5.最终API
public static Object setupServiceProxy(@NotNull Class responseClass) {
ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance();
ResteasyClientBuilder builder = new ResteasyClientBuilder().providerFactory(factory);
ResteasyClient client = builder.httpEngine(setupHttpDefaults()).build();
ResteasyWebTarget target = client.target(url);
return target.proxy(responseClass);
}
public static ClientHttpEngine setupHttpDefaults() {
PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 30000);
HttpConnectionParams.setSoTimeout(params, 30000);
BasicHttpContext localContext = new BasicHttpContext();
return new ApacheHttpClient4Engine(httpClient, localContext);
}https://stackoverflow.com/questions/21717130
复制相似问题