java 使用 jersey 客户端的连接池
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25424302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connection pooling using jersey client
提问by lambodar
I am very new to Jersey and I did a search but unable to figure out whether Is there a way in jersey clientto use connection poolinginstead 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.
整个想法是重用池中的一组连接,这将节省大量或资源。仅供参考,我不是在寻找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.
任何帮助都是非常可观的,期待代码片段。提前致谢。
回答by Chris H.
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.xmentioned in the comments.
您可以将 Jersey 客户端配置为将 Apache HttpClient 与连接池一起使用。有关如何执行此操作的详细信息,请参阅此博客文章。请注意,该帖子本身涵盖了 Jersey 2.x,但在评论中提到了 Jersey 1.x的要点。