Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

java - What is the right way to use HttpClientConnectionManager with CloseableHttpClient in Apache?

Context

I have an instance of org.apache.hc.client5.http.impl.classic.CloseableHttpClient and a singleton instance of org.apache.hc.client5.http.io.HttpClientConnectionManager to communicate with some remote API.

I have created a singleton pool of HTTP connections to create an instance of the HTTP client.

What is the right way to use HttpClientConnectionManager?

Should I really use try-with-resource (or old-style try-finally) to work with CloseableHttpClient?

If I close the HTTP client, a connection will be closed also from this pool. And then I can't use this pool to communicate with a remote API.

Of course, I've read the documentation https://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html

Maybe something I missed. Could anyone explain me?

question from:https://stackoverflow.com/questions/65831212/what-is-the-right-way-to-use-httpclientconnectionmanager-with-closeablehttpclien

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should also be using CloseableHttpClient as a singleton (on a per distinct service basis).

If you want to continue creating short lived instances of CloseableHttpClient with the same HttpClientConnectionManager please make sure to mark it as shared when creating CloseableHttpClient instances with HttpClientBuilder.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...