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

查看:40
本文介绍了关闭 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?

根据文档 关于 Client 类:

According to documentation about the Client class:

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

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

WebTarget 类没有任何 invalidate()/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 实现内关闭,但我找不到它.谁能告诉我为什么?我知道 Response 类将实现 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 以外的任何其他内容,您将不必关闭 Response(但无论如何它都是安全的,因为它是幂等的).

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天全站免登陆