关闭JAX RS客户端/响应 [英] Closing JAX RS Client/Response

查看:209
本文介绍了关闭JAX RS客户端/响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不清楚是否必须关闭JAX RS客户端/响应实例。如果我必须,总是或不?

It's not clear must I close JAX RS Client/Response instances or not. And if I must, always or not?

根据文档


调用此方法会有效地使客户端实例生成的所有资源目标
无效。

Calling this method effectively invalidates all resource targets produced by the client instance.

WebTarget类没有任何无效()/ close()方法,但Response类确实如此。
根据文档

The WebTarget class does not have any invalidate()/close() method, but the Response class does. According to documentation:


关闭基础消息实体输入流(如果可用且
) open)以及释放与
响应相关的任何其他资源(例如缓冲的消息实体数据)。

Close the underlying message entity input stream (if available and open) as well as releases any other resources associated with the response (e.g. buffered message entity data).

...应该在包含未消耗实体
输入流的所有实例上调用close()方法
以确保资源与实例关联的是
正确清理并防止潜在的内存泄漏。对于客户端方案,这是
,其中应用程序层代码
仅处理响应头并忽略响应实体。

... The close() method should be invoked on all instances that contain an un-consumed entity input stream to ensure the resources associated with the instance are properly cleaned-up and prevent potential memory leaks. This is typical for client-side scenarios where application layer code processes only the response headers and ignores the response entity.

最后一段对我来说并不清楚。 未消耗的实体输入流是什么意思?如果我从响应中获取InputSteam或String,是否应该明确关闭响应?

The last paragraph is not clear to me. What does "un-consumed entity input stream" mean? If I get an InputSteam or a String from response, should I close the response explicitly?

我们可以在无法访问Response实例的情况下获得响应结果:

We can get a response result without getting access to Response instance:

Client client = ...;
WebTarget webTarget = ...;
Invocation.Builder builder = webTarget.request(MediaType.APPLICATION_JSON_TYPE);
Invocation invocation = builder.buildGet();
InputStream reso = invocation.invoke(InputStream.class);

我正在使用RESTeasy实现,我期望在resteasy实现中关闭响应,但我找不到它。谁能告诉我为什么?
我知道回应class将实现Closeable接口
但即使知道,也会使用Response,而不关闭它。

I'm working with RESTeasy implementation, and I expected that response will be closed inside of resteasy implementation, but I could not find it. Could anyone tell me why? I know that the Response class will implement Closeable interface But even know, the Response is used, without closing it.

推荐答案

根据文件 close() 是幂等的。

According to the documentation close() is idempotent.


此操作是幂等的,即它可以多次调用并具有相同的效果,这也意味着在已经关闭的消息实例上调用close()方法是合法的并且没有进一步的效果。

This operation is idempotent, i.e. it can be invoked multiple times with the same effect which also means that calling the close() method on an already closed message instance is legal and has no further effect.

所以你可以自己安全地关闭 InputStream

So you can safely close the InputStream yourself and should.

据说我风格明智不会做 invocation.invoke(InputStream.class)作为 invoker(Class)用于进行实体转换。相反,如果你想要InputStream,你可能只需要调用 invocation.invoke()并直接处理 Response 对象在阅读流之前可能需要一些标题信息。
处理响应时你想要标题的原因 InputStream 是典型的,因为你不关心身体或身体需要特殊的处理和尺寸考虑因素是文档提到的(例如 HEAD 请求ping服务器)。

That being said I style wise would not do invocation.invoke(InputStream.class) as the invoker(Class) is made for doing entity transformation. Instead if you want InputStream you should probably just call invocation.invoke() and deal with the Response object directly as you may want some header info before reading the stream. The reason you want headers when dealing with a response InputStream is typical because you either don't care about the body or the body requires special processing and size considerations which is what the documentation is alluding to (e.g. HEAD request to ping server).

参见 link


从此方法返回的消息实例将被缓存,以便通过getEntity()进行后续检索。除非提供的实体类型是输入流,否则此方法会在打开时自动关闭未使用的原始响应实体数据流。如果实体数据已被缓冲,则缓冲区将在消耗缓冲数据之前被重置,以便在此响应上启用readEntity(...)方法的后续调用。

A message instance returned from this method will be cached for subsequent retrievals via getEntity(). Unless the supplied entity type is an input stream, this method automatically closes the an unconsumed original response entity data stream if open. In case the entity data has been buffered, the buffer will be reset prior consuming the buffered data to enable subsequent invocations of readEntity(...) methods on this response.

因此,如果你选择 InputStream 以外的任何东西,你将不必关闭响应 (但无论如何它都是安全的,因为它是幂等的。)

So if you choose anything other than InputStream you will not have to close the Response (but regardless its safe to do it anyways as its idempotent).

这篇关于关闭JAX RS客户端/响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆