0

내가 사용하고 이러한 요청을 들어, 서로 다른 목적을 위해 다른 API를 사용하여 다중 스레드 REST 클라이언트를 쓰고 있어요는 다른 방법 (POST, GET, PUT)효율적인 방법

과 HttpClient를을 스레드 1 :

DefaultHttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(url); 
httpclient.execute(httppost); 


methodThatNeedsHttpClient(httpclient); 


public void methodThatNeedsHttpClient(HttpClient client) { 
//perform other GET/POST/PUT requests 
} 

스레드 2 :

DefaultHttpClient httpclient2 = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(url); 
httpclient2.execute(httppost); 
// Other methods 

나는 httpConnections 관리를 위해 내가 연결 관리자를 사용한다는 것을 읽었다. 클라이언트 4.5 버전을 사용하고 있는데 어떤 연결 관리자를 사용해야합니까? 연결 관리자가 연결이 누출되지 않고 효율적으로 사용되도록하는 방법은 무엇입니까?

나는 다음과 같은 구현하려고 :

PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(); 
     connectionManager.setMaxTotal(5); 
// Perform REST operations 

client.getConnectionManager().shutdown(); 

을하지만 연결 관리자는 모든 스레드에서 초기화 않고, 연결이 멀티 스레드 시스템, 풀에서 관리하는 방법을하지 확신?

답변

0

대부분의 경우 연결 풀은 모든 httpClient 인스턴스에서 액세스 할 수있는 공유 풀이어야합니다.

EntityUtils.consume(httpResponse.getEntity()); 

동안 연결을 닫습니다

세션 객체를 생성,

CloseableHttpClient httpClient = HttpClients.custom() 
       .setConnectionManager(connectionManager) 
       .setConnectionManagerShared(true) 
       .build(); 

그리고이 풀에 다시 연결을 해제합니다,

httpResponse.close(); 
httpClient.close(); 

우리가 setConnectionManagerShared(true)을 가지고 있기 때문에 , httpClient.close()은 연결 풀을 차단하지 않습니다.