[英]Jersey client creation in a REST service
i am creating a http REST service that consumes other http REST services. 我正在创建使用其他HTTP REST服务的http REST服务。
I am using Jersey Client to call other services and i have many doubt about which creation pattern of the http client is the best. 我正在使用Jersey客户端调用其他服务,并且我对http客户端的最佳创建模式有很多疑问。
Currently i am using EJB with injection of the client that is a Singleton shared by every methods, but i would like to remove java ee dependency and use Jetty as embedded application server. 目前,我正在使用EJB注入客户端,该客户端是每个方法共享的Singleton,但是我想删除java ee依赖性并将Jetty用作嵌入式应用程序服务器。
I see from the doc that Client creation is an expensive operation so i cannot create one every time i need it. 我从文档中看到,客户端创建是一项昂贵的操作,因此我无法在每次需要时创建一个。
I think about creating 1 in the constructor of every Servlet/Rest class is the simpler solution but i am not sure about the lifecycle of the servlet (if an instance is created for every request, this method is quite the same as the previous) 我认为在每个Servlet / Rest类的构造函数中创建1是更简单的解决方案,但是我不确定Servlet的生命周期(如果为每个请求创建一个实例,则此方法与前面的方法完全相同)
Or maybe is better to create a Singleton shared by every Servlet/Rest class 或者创建每个Servlet / Rest类共享的Singleton更好
Or maybe better a pool of N client. 或更好的N客户池。
About this last two solution i need some advice... What do you think it's the better solution? 关于最后两个解决方案,我需要一些建议...您认为这是更好的解决方案吗?
Thanks 谢谢
According to you, there is a REST Service
deployed in some environment and there is one application, a client
or consumer
, which wants to access that service. 根据您的说法,在某个环境中部署了REST Service
,并且有一个应用程序( client
或consumer
)想要访问该服务。
If i am writing a normal Java class
as client using Jersey API, then i will write something lime this : 如果我使用Jersey API将普通的Java class
作为客户端编写,那么我会写一些类似的东西:
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
Client client = Client.create();
WebResource webResource = client
.resource("http://localhost:8080/rest/example/employees");
ClientResponse response = webResource.accept("application/json")
.get(ClientResponse.class);
String result = response.getEntity(String.class);
Now say you are writing a servlet
, which does some defined job in you application, also it makes a call to the REST Service
using client block of code, everytime you access the servlet
it will create a instance of com.sun.jersey.api.client.Client
each time. 现在说您正在编写一个servlet
,它在您的应用程序中完成一些定义的工作,它还会使用客户端代码块调用REST Service
,每次访问该servlet
,它将创建com.sun.jersey.api.client.Client
的实例。每次com.sun.jersey.api.client.Client
。
If you want to avoid this then you can create a initial class that will create an instance of com.sun.jersey.api.client.Client
, one and only, and make it static and use the same reference where ever you want. 如果要避免这种情况,则可以创建一个初始类,该初始类将创建com.sun.jersey.api.client.Client
一个实例,并且是唯一的,并将其设为静态,并在需要的地方使用相同的引用。 WebResource
should be created as and when required, because you might be interested to call different URIs
. WebResource
应该在需要时创建,因为您可能有兴趣调用不同的URIs
。
I would have followed this approach, if i were in your situation. 如果我处在您的情况下,我会采用这种方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.