Jersey 客户端关闭 InputStream 响应 - 它真的有效吗?

Jersey Client closing InputStream response - does it really work?

我正在使用 Jersey Client v2.16(Dropwizard 0.8.0 的传递依赖,我也在使用它)。

当实体被读取为 InputStream 时,我对响应的关闭机制感到困惑。 documentation 状态:

Also if the entity is read into an InputStream (by response.readEntity(InputStream.class)), the connection stays open until you finish reading from the InputStream. In that case, the InputStream or the Response should be closed manually at the end of reading from InputStream.

但是,当我使用 Response.readEntity(InputStream.class) 获得响应实体时,我最终得到的是 org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream 的实例,顾名思义,当 close() 方法被调用(打破 InputStream 合同,我可能会说)。这是 close() 方法:

@Override
public void close() throws IOException {
    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.log(Level.FINE, LocalizationMessages.MBR_TRYING_TO_CLOSE_STREAM(reader.getClass()));
    }
} 

因此,我的池中的 HTTP 连接最终未释放并慢慢填满池。

鉴于获取对响应的引用可能不是很容易,而且官方文档声明 the InputStream **or** the Response should be closed manually,我如何设法真正释放物理资源?

看起来这是 Jersey Client 的一个已知错误,根据 https://java.net/jira/browse/JERSEY-2878 and https://github.com/jersey/jersey/releases/tag/2.21

,它似乎已在 2.21 中修复