Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey client services - closing resources

I have a service class that has a number of methods that make REST calls to a Spring REST service. Each of the methods looks like this:

public void getUser() {

        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        WebResource service = client.resource(RESOURCE_URL);


        // Get response as String
        String response = service.path("/addUser").accept(MediaType.TEXT_PLAIN)
                .get(String.class);

        return response;
    }

The above works fine but i am slightly worried that every time the method is called, new instances of ClientConfig, Client and WebResource are created. What would the side effects be of me changing the above and making ClientConfig, Client and WebResource as class level instance variables? i.e. change to this:

ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(RESOURCE_URL);

public void getUser() {         

        // Get response as String
        String response = service.path("/addUser").accept(MediaType.TEXT_PLAIN)
                .get(String.class);

        return response;
    }

 public void getUserAccount() {         

        // Get response as String
        String response = service.path("/getUserAccount").accept(MediaType.TEXT_PLAIN)
                .get(String.class);

        return response;
    }

Is the above likely to fail if multiple users call different methods at the same time? What is the best way to structure the above?

If the Jersey client methods had close() methods, i could have left them the way they were and just close the resources inside the methods.

like image 372
ziggy Avatar asked Jan 13 '13 16:01

ziggy


People also ask

How do I close my jersey client?

For this situation, you need to invoke Response#close() to close the connection. Or invoke Response#readEntity(Class<T>) to make Jersey close the connection for you.

What is Jersey client?

Jersey is an open source framework for developing RESTFul Web Services. It also has great inbuilt client capabilities. In this quick tutorial, we will explore the creation of JAX-RS client using Jersey 2.

Is Jersey client thread safe?

Yes, the Jersey 2.1 client is thread safe and it should be thread safe even in the future Jersey version. You can create many WebTarget from one Client instance and invoke many requests on these WebTargets and even more requests on one WebTarget instance in the same time.


1 Answers

From Jersey documentation:

For Client:

Methods to create instances of WebResource are thread-safe. Methods that modify configuration and or filters are not guaranteed to be thread-safe.

The creation of a Client instance is an expensive operation and the instance may make use of and retain many resources. It is therefore recommended that a Client instance is reused for the creation of WebResource instances that require the same configuration settings

For WebResource:

Methods to create a request and return a response are thread-safe. Methods that modify filters are not guaranteed to be thread-safe

While there is no explicit concurrency documentation for ClientConfig, it's clear from its source code that its safe to use in a multithreaded environment. The Client class is also thread safe, leaving just the WebResource to consider. Based on its documentation I would dedicate a new WebResource to each thread, meaning your code should look more like this:

ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);

public void getUser() {         
    WebResource service = client.resource(RESOURCE_URL);

    // Get response as String
    String response = service
        .path("/addUser")
        .accept(MediaType.TEXT_PLAIN)
        .get(String.class);

        return response;
}
like image 173
Perception Avatar answered Oct 07 '22 20:10

Perception