我目前遇到的问题是我的RestEasyClient在关闭响应对象后没有释放连接。在我的本地环境中它可以工作,但是当我部署到CentOS Tomcat 7实例时,连接在一段时间之后才会被释放。
以下是我如何初始化客户端的示例:
ResteasyClient client = new ResteasyClientBuilder().connectionPoolSize(20)
.connectionTTL(2, TimeUnit.SECONDS)
.establishConnectionTimeout(500, TimeUnit.MILLISECONDS)
.maxPooledPerRoute(20)
.build();
以下是使用客户端/消费响应的示例:
final ResteasyWebTarget target = client.target(url);
Response response = null;
try {
response = target.request().post(Entity.json(postBody));
final String jsonMsg = response.readEntity(String.class);
return mapper.readValue(jsonMsg, new TypeReference<List<String>>() {});
} catch (final Exception e) {
log.error("error", e);
} finally {
if (null != response) {
response.close();
}
}
这是堆栈跟踪:
javax.ws.rs.ProcessingException: Unable to invoke request
at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:287)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:407)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.post(ClientInvocationBuilder.java:195)
引起:
Caused by: org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection
at org.apache.http.impl.conn.PoolingClientConnectionManager.leaseConnection(PoolingClientConnectionManager.java:232)
at org.apache.http.impl.conn.PoolingClientConnectionManager$1.getConnection(PoolingClientConnectionManager.java:199)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:455)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:283)
任何帮助都会非常感激 - 我已经在这个问题上花了好几天。