Question

I am using jersey client to send POST requests to a third party webservice. Since creating jersey clients is expensive, I am doing this inside the constructor of a service client class which is Spring managed. 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. 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.

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:

<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.

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.

2) Do I need some kind of jersey client pool so that a large number of requests are taken care of more efficiently. 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. 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.

Was it helpful?

Solution

Your thinking is right on track.

1 - Yes, and it is recommended to reuse a client instance:

from 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. The creation of Web resources, the building of requests and receiving of responses are guaranteed to be thread safe. Thus a Client instance and WebResource instances may be shared between multiple threads.

2 - No need, the client itself can handle the requests. 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

4 - You can also consider providing the client as a dependency to MyWebserviceClient, and possibly reuse the same client between multiple classes

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top