如何正确共享JAX-RS 2.0客户端 [英] How to correctly share JAX-RS 2.0 client

查看:191
本文介绍了如何正确共享JAX-RS 2.0客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为我的问题提供一些上下文...



我有一个Java EE Web应用程序(作为UI /客户端)访问数据/业务服务逻辑通过REST接口使用JAX-RS 2.0客户端API(Resteasy实现)。



目前,我使用RequestScoped CDI托管bean为每个请求注入一个新的JAXRS客户端实例,我们的想法是客户端应用程序可以为每个请求调用多个后端资源,并且我为整个请求重用相同的JAXRS客户端(尽管我在某处读到这可能不正确,因为我可能会更改每次调用的URI)



JAXRS客户端的文档似乎表明客户端是一个可能很昂贵的操作,应用程序应该限制它创建的连接数量。它似乎也自相矛盾,并建议在完成对特定WebTarget的所有请求后关闭客户端。



客户端应用程序可能会支持数千个并发用户创建和销毁成千上万的昂贵的客户似乎不是正确的方法,因此我认为共享客户端池更合适,但似乎没有关于如何实现这一点的任何信息。



所有示例似乎都显示为请求创建了一个新客户端,a)在关闭之后关闭它,或者b)没有关闭它,但没有真正解释第二个请求发生了什么。



您能否提供一些答案,说明您认为如何解决这个问题,或者提供有关此方法最佳实践的信息。



谢谢。

解决方案

唯一的最佳实践建议我看到,以避免使用JAX-RS 2.0客户端的性能不佳或内存使用不良模式与Jersey的Jax-RS实现相关,因此它可能对RestEasy无效。但是,我怀疑这两个实现是相似的,建议是可移植的。



基本上,我的理解是




  • 使用ClientBuilder创建少量完全配置的客户端实例 - 在不同情况下,您可能需要具有不同配置的不同客户端(例如,序列化/反序列化提供程序)。这种事情应该围绕应用程序初始化或类似的罕见事件发生。

  • 在具有相同配置要求的类之间共享每个完全配置的客户端实例。例如,您可以告诉您的DI框架将每个客户端实例的范围限定为 @Singleton

  • 避免调用任何方法修改基础配置的客户端实例。例如 register(Class< T> componentClass) - 几乎所有关于 javax.ws.rs.core.Configurable interface。

  • 使用共享客户端的每个实例对象都可以(并且应该?)创建自己的私有和非共享WebTargets。实际上,它的WebTargets应该是 @RequestScoped 而不是客户。

  • 但是,与客户端一样,任何使用WebTarget的东西都应该避免做通过 javax.ws.rs.core.Configurable 接口方法进行的任何操作。



<之后,它几乎一帆风顺。


To give a little context to my issue...

I have a Java EE web application (as a UI / client) that accesses services for data / business logic via a REST interface using the JAX-RS 2.0 client API (Resteasy implementation).

Currently I inject a new JAXRS Client instance per request using a RequestScoped CDI managed bean, the thinking being that the client app may call multiple backend resources per request and I reuse the same JAXRS Client for the whole request (although I read somewhere this may not be correct as I am potentially changing the URI for each invocation)

The documentation for JAXRS Client seems to suggest that the client is a potentially expensive operation and the app should limit the amount of connections it creates. It also seems to contradict itself and suggest the client should be closed once all the requests to a particular WebTarget are finished.

The client application could potentially support thousands of simultaneous users so creating and destroying thousands of 'expensive clients' does not seem to be the correct approach so am thinking a shared client pool is more appropriate but there doesn't seem to be any information on how this should be achieved.

All examples appear to show creating a new client for the request and a) closing it after or b) not closing it but not really explaining what happens on a second request.

Can you help provide some answers on how you think this would be solved or information on what the best practice for this approach is.

Thanks.

解决方案

The only "best-practice" advice I've seen for avoiding either poor performance or poor memory usage patterns with a JAX-RS 2.0 client relates to the Jersey implementation of Jax-RS and so it may not be valid for RestEasy. However, I suspect that the two implementations are similar enough that the advice is portable.

Basically, my understanding is

  • use ClientBuilder to create a small number of fully-configured Client instances - you may need different clients with different configurations (e.g. Serialisation/Deserialisation providers) in different situations. This sort of thing should probably happen around application initialisation, or similar kind of 'rare' events.
  • share each fully-configured Client instance among classes with the same configuration requirements. You might tell your DI framework, for example, to scope each of those Client instances as @Singleton.
  • avoid calling any methods on Client instances which modify the underlying configuration. Examples are things like register(Class<T> componentClass) - pretty much anything on the javax.ws.rs.core.Configurable interface.
  • Each instance object using a shared Client can (and should?) create its own, private and non-shared, WebTargets. Effectively, it's WebTargets which should be @RequestScoped and not Clients.
  • However, as with Client, anything using a WebTarget should avoid doing any diddling via the javax.ws.rs.core.Configurable interface methods.

After that it's pretty much plain sailing.

这篇关于如何正确共享JAX-RS 2.0客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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