简体   繁体   English

在Spring托管bean的构造函数中初始化jersey客户端

[英]Initializing jersey client in constructor of spring managed bean

I am using jersey client to send POST requests to a third party webservice. 我正在使用jersey客户端向第三方Web服务发送POST请求。 Since creating jersey clients is expensive, I am doing this inside the constructor of a service client class which is Spring managed. 由于创建jersey客户端很昂贵,我在Spring管理的服务客户端类的构造函数中执行此操作。 My thinking is that when my server starts up, Spring will create my service client bean, which in turn will cause the constructor to be invoked and so my jersey client will be created once. 我的想法是,当我的服务器启动时,Spring将创建我的服务客户端bean,这反过来将导致调用构造函数,因此我的jersey客户端将被创建一次。 As long as the server is up, this jersey client will be responsible for sending out the requests and no further client initializations are required. 只要服务器启动,此球衣客户端将负责发送请求,并且不需要进一步的客户端初始化。 However, I will be creating a new webresource for each call as creating jersey webresources is much cheaper. 但是,我将为每个调用创建一个新的webresource,因为创建jersey webresources要便宜得多。

package com.mypackage;
//Bunch of imports

class MyWebserviceClient {

    //jersey client member variable
    private Client jClient;

    public MyWebserviceClient(){

        //Create jersey client
        jClient = Client.create();

        //Other stuff
    }

    public void sendRequest(){

        WebResource wr = jClient.resource(someUrl);
        //Use the webresource to make webservice call
    }
}

MyWebserviceClient is spring managed as such in the Spring config xml: MyWebserviceClient在Spring config xml中是弹簧管理的:

<bean id="myClient" class="com.mypackage,MyWebserviceClient"></bean>

The bean myClient will then be injected into the appropriate place where the service call needs to be made. 然后将bean myClient注入需要进行服务调用的适当位置。

My questions 我的问题

1) If my application is dealing with thousands of requests per hour is it efficient enough to handle all the requests with just one jersey client. 1)如果我的应用程序每小时处理数千个请求,那么仅使用一个球衣客户端来处理所有请求就足够了。

2) Do I need some kind of jersey client pool so that a large number of requests are taken care of more efficiently. 2)我是否需要某种球衣客户端池,以便更有效地处理大量请求。 If so, is there a way to do it? 如果是这样,有办法吗?

3) I would like to know in general how multiple requests coming in from the end users are handled on the server side. 3)我想知道如何在服务器端处理来自最终用户的多个请求。 Each request is a separate thread of execution on the server and all of them have access to the same jersey client object. 每个请求都是服务器上的单独执行线程,并且所有请求都可以访问相同的泽西客户端对象。 If the jersey client object is busy with one such request, are the other requests from different end users going to wait till a response is received for the ongoing request? 如果泽西客户端对象忙于一个这样的请求,来自不同终端用户的其他请求是否会等到收到正在进行的请求的响应?

4) Any better alternative to the one I am using. 4)任何比我正在使用的更好的替代品。

Your thinking is right on track. 你的想法是正确的。

1 - Yes, and it is recommended to reuse a client instance: 1 - 是,建议重用客户端实例:

from https://jersey.java.net/documentation/1.18/client-api.html#d4e623 : 来自https://jersey.java.net/documentation/1.18/client-api.html#d4e623

Client instances are expensive resources. 客户端实例是昂贵的资源。 It is recommended a configured instance is reused for the creation of Web resources. 建议重新使用已配置的实例来创建Web资源。 The creation of Web resources, the building of requests and receiving of responses are guaranteed to be thread safe. Web资源的创建,请求的构建和响应的接收都保证是线程安全的。 Thus a Client instance and WebResource instances may be shared between multiple threads. 因此,可以在多个线程之间共享Client实例和WebResource实例。

2 - No need, the client itself can handle the requests. 2 - 不需要,客户端本身可以处理请求。 In the case of asynchronous requests, it internally uses a thread pool that is configurable. 在异步请求的情况下,它在内部使用可配置的线程池。

3 - The Jersey client is thread safe, so threads will not block each other 3 - Jersey客户端是线程安全的,因此线程不会相互阻塞

4 - You can also consider providing the client as a dependency to MyWebserviceClient, and possibly reuse the same client between multiple classes 4 - 您还可以考虑将客户端作为MyWebserviceClient的依赖项,并可能在多个类之间重用相同的客户端

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM