fix httpclient not close (#122)

* fix httpclient issue
* @ascrutae As you pr too many commits, I will use `confirm squash and merge`.
This commit is contained in:
Xin,Zhang 2017-04-04 23:01:52 +08:00 committed by 吴晟 Wu Sheng
parent 209791c26d
commit e8a427233c
1 changed files with 6 additions and 6 deletions

View File

@ -30,15 +30,11 @@ import org.apache.http.impl.client.HttpClients;
public class CollectorClient implements Runnable {
private static ILog logger = LogManager.getLogger(CollectorClient.class);
private static long SLEEP_TIME_MILLIS = 500;
private CloseableHttpClient httpclient;
private String[] serverList;
private volatile int selectedServer = -1;
public CollectorClient() {
serverList = Config.Collector.SERVERS.split(",");
httpclient = HttpClients.custom()
.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())
.build();
Random r = new Random();
if (serverList.length > 0) {
selectedServer = r.nextInt(serverList.length);
@ -92,11 +88,13 @@ public class CollectorClient implements Runnable {
.excludeFieldsWithoutExposeAnnotation()
.create();
String messageJson = gson.toJson(message);
CloseableHttpClient httpClient = HttpClients.custom()
.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())
.build();
try {
HttpPost httpPost = ready2Send(messageJson);
if (httpPost != null) {
CloseableHttpResponse httpResponse = httpclient.execute(httpPost);
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (200 != statusCode) {
findBackupServer();
@ -106,6 +104,8 @@ public class CollectorClient implements Runnable {
} catch (IOException e) {
findBackupServer();
throw e;
}finally {
httpClient.close();
}
}