users@jersey.java.net

[Jersey] Jersey client proper way to execute parallel POST requests

From: Gabriel Ciuloaica <gciuloaica_at_gmail.com>
Date: Fri, 31 Aug 2012 14:01:54 +0300

Hi,

I'm looking for the proper way of executing POST requests using jersey client (with apache http client 4.x)

Since Client instance and WebResource instance are thread-safe, the natural way of coding the method will be:

public AuthorizedAccount createAuthTokenFromUserPass(Credentials credentials)
        throws AuthenticationServiceClientException {
    ClientResponse response = resource.accept("application/x-protobuf").post(
                              ClientResponse.class, credentials);
    return getAuthorizedAccountFromResponse(response);
}
ClientResponse instance is properly released.

When this method is executed from multiple threads, it ends-up by POSTing same credential instance to the server, for each thread.

A solution will be to synchronise the method, but will end-up by executing the POSTs in sequence.

Is there a different way to do it? (avoiding the re-creation of Client instance)


Thanks
Gabi