我使用 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.