아파치 httpclient 4.3.4를 사용했는데, 작업 스레드에서 httpclient 인스턴스를 재사용하고 싶지만, httpclient가 두 번째로 데이터를 게시 할 때 스레드가 차단되었습니다.아파치를 재사용 할 때 스레드가 차단되었습니다. httpclient
class SinglePostConnectionThread extends Thread {
void processResponse(CloseableHttpResponse response) throws Exception{
try{
StatusLine sLine = response.getStatusLine();
switch (sLine.getStatusCode()){
case 200:{
break;
}
case 204:{
//HttpEntity entity = response.getEntity();
//entity.getContent().close();
System.out.println("No Flight");
break;
}
default:
System.out.println("Bad response");
}
} catch (Exception e){
System.out.println(e.getMessage());
}finally {
System.out.println("Close response");
response.close();
}
}
@Override
public void run() {
BasicHttpClientConnectionManager basicConnManager = new BasicHttpClientConnectionManager();
HttpClientContext context = HttpClientContext.create();
try {
CloseableHttpClient client = HttpClients.custom().setConnectionManager(basicConnManager).build();
int tmpLoop = loopNum;
while (tmpLoop > 0) {
HttpPost post = new HttpPost(host);
StringEntity se = new StringEntity(toJson(bid), "utf-8");
post.setHeader(HTTP.CONTENT_TYPE, "application/json");
post.setEntity(se);
processResponse(client.execute(post, context));//blocked when running at second time
tmpLoop--;
if (loopNum ==0){
tmpLoop = 1;
}
post.releaseConnection();
//basicConnManager.;
}
} catch (Exception e){
e.printStackTrace();
}
}
}
연결이 끊긴 것 같지만 사실 모든 리소스를 닫았습니다.
좀 걸릴 수 있습니다, 그래서 BasicHttpClientConnectionManager를 사용하려고합니다. 스레드가 데이터를 게시하는 데 계속해서 사용할 수 있도록 연결을 해제하는 방법을 얻길 바랍니다. – tank1920
차단의 원인을 발견했습니다. 서버의 NOCONTENT 응답이 본문을 가져 왔기 때문에 연결을 해제 할 수 없습니다. NOCONTENT 응답을 테스트 할 환경이 있기 때문에 테스트하는 것은 큰 실수입니다. – tank1920