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:
parent
209791c26d
commit
e8a427233c
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue