【发布时间】:2017-08-30 21:11:08
【问题描述】:
我们目前在生产服务器上遇到了一些问题,因为它消耗了太多内存。其中一个泄漏可能来自球衣客户。我发现了以下两个其他问题以及如何:
- How to correctly share JAX-RS 2.0 client
- Closing JAX RS Client/Response
- https://blogs.oracle.com/japod/entry/how_to_use_jersey_client
我从中得到什么,我应该重用客户端并可能还重用 WebTargets? 还建议关闭响应,但是如何使用 .request() 执行此操作?
代码示例,每小时使用不同的路径调用大约 1000 次:
public byte[] getDocument(String path) {
Client client = ClientBuilder.newClient();
WebTarget target = client.target(config.getPublishHost() + path);
try {
byte[] bytes = target.request().get(byte[].class);
LOGGER.debug("Document size in bytes: " + bytes.length);
return bytes;
} catch (ProcessingException e) {
LOGGER.error(Constants.PROCESSING_ERROR, e);
throw new FailureException(Constants.PROCESSING_ERROR, e);
} catch (WebApplicationException e) {
LOGGER.error(Constants.RESPONSE_ERROR, e);
throw new FailureException(Constants.RESPONSE_ERROR, e);
} finally {
client.close();
}
}
那么我的问题是如何正确使用 API 来防止上述示例的泄漏?
【问题讨论】:
标签: java performance rest jersey java-memory-leaks