我使用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);
UPDATE:我在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.
答案 0 :(得分:4)
我们用这个:
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连接管理器的丛林......
答案 1 :(得分:0)
这对我有用。只需要找到设置Apache HTTP引擎的钩子。主要基于RestEasy 3.0.5.Final 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);
}