Connection pooling using jersey client

2019-04-07 00:36发布

问题:

I am very new to Jersey and I did a search but unable to figure out whether Is there a way in jersey client to use connection pooling instead of creating a connection each and every time we are sending a new request.

The whole idea is to reuse set of connection from the pool, which will save lots or resource. FYI I'm not looking for Connection: keep-alive.

This is what I'm doing now

public void postData()
 {
     Client client  =   new Client();
     WebResource webResource = client.resource("http://SomeService.com/..");
     ClientResponse response = webResource.accept("text/plain").get(ClientResponse.class);
     System.out.println(response.getStatus());
     System.out.println(response.getEntity(String.class));
 }

Any help is highly appreciable,Expecting code snippet. Thanks in advance.

回答1:

You can configure Jersey client to use Apache HttpClient with connection pooling. Details of how to do so can be found on this blog post. Note that the post itself covers Jersey 2.x, but there is a gist for Jersey 1.x mentioned in the comments.