Commit ee64aadb authored by Benoit Lavenier's avatar Benoit Lavenier

[enh] HttpClients : better logs

parent 2e0284f4
Pipeline #4348 failed with stage
in 1 minute and 14 seconds
......@@ -128,20 +128,12 @@ public class HttpServiceImpl implements HttpService, Closeable, InitializingBean
@Override
public void close() throws IOException {
HttpClients.getThreadHttpClient()
// httpClientCache.clear();
// requestConfigCache.clear();
//
// if (wsEndPoints.size() != 0) {
// for (WebsocketClientEndpoint clientEndPoint: wsEndPoints.values()) {
// clientEndPoint.close();
// }
// wsEndPoints.clear();
// }
//
// if (connectionManager != null) {
// connectionManager.close();
// }
if (wsEndPoints.size() != 0) {
for (WebsocketClientEndpoint clientEndPoint: wsEndPoints.values()) {
clientEndPoint.close();
}
wsEndPoints.clear();
}
}
public <T> T executeRequest(HttpUriRequest request, Class<? extends T> resultClass) {
......@@ -204,97 +196,6 @@ public class HttpServiceImpl implements HttpService, Closeable, InitializingBean
}
}
// protected PoolingHttpClientConnectionManager createConnectionManager(
// int maxTotalConnections,
// int maxConnectionsPerRoute,
// int timeout) {
// PoolingHttpClientConnectionManager connectionManager
// = new PoolingHttpClientConnectionManager();
// connectionManager.setMaxTotal(maxTotalConnections);
// connectionManager.setDefaultMaxPerRoute(maxConnectionsPerRoute);
// connectionManager.setDefaultSocketConfig(SocketConfig.custom()
// .setSoTimeout(timeout).build());
// return connectionManager;
// }
//
// protected HttpClient createHttpClient(int timeout) {
// if (connectionManager == null) {
// Configuration config = Configuration.instance();
// connectionManager = createConnectionManager(
// config.getNetworkMaxTotalConnections(),
// config.getNetworkMaxConnectionsPerRoute(),
// config.getNetworkTimeout());
// }
//
// return HttpClients.custom()
// .setConnectionManager(connectionManager)
// .setDefaultRequestConfig(requestConfigCache.get(timeout))
// .setRetryHandler(httpRetryHandlerCache.get(timeout))
// .build();
// }
//
// protected HttpRequestRetryHandler createRetryHandler(int timeout) {
// if (timeout <= 0) timeout = defaultTimeout;
// final int maxRetryCount = (timeout < defaultTimeout) ? 2 : 3;
// return new HttpRequestRetryHandler() {
// public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
//
// log.warn("Failed request: " + exception.getMessage());
//
// boolean retrying = true;
// if (exception instanceof NoRouteToHostException) {
// // Bad DNS name
// retrying =false;
// }
// else if (exception instanceof InterruptedIOException) {
// // Timeout
// retrying = false;
// }
// else if (exception instanceof UnknownHostException) {
// // Unknown host
// retrying = false;
// }
// else if (exception instanceof SSLException) {
// // SSL handshake exception
// retrying = false;
// }
// else if (exception instanceof HttpHostConnectException) {
// // Host connect error
// retrying = false;
// }
//
// if (retrying && executionCount >= maxRetryCount) {
// // Do not retry if over max retry count
// return false;
// }
//
//
// HttpClientContext clientContext = HttpClientContext.adapt(context);
// HttpRequest request = clientContext.getRequest();
// if (!retrying) {
// if (debug) log.debug("Failed request to " + request.getRequestLine() + ": " + exception.getMessage());
// return false;
// }
//
// boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
// if (idempotent) {
// // Retry if the request is considered idempotent
// if (debug) log.debug("Failed (but will retry) request to " + request.getRequestLine() + ": " + exception.getMessage());
// return true;
// }
// return false;
// }
// };
// }
//
// protected RequestConfig createRequestConfig(int timeout) {
// return RequestConfig.custom()
// .setSocketTimeout(timeout).setConnectTimeout(timeout)
// .setMaxRedirects(1)
// .setCookieSpec(CookieSpecs.IGNORE_COOKIES)
// .build();
// }
protected <T> T executeRequest(HttpClient httpClient, HttpUriRequest request, Class<? extends T> resultClass) {
return executeRequest(httpClient, request, resultClass, Error.class);
}
......
......@@ -29,6 +29,9 @@ public abstract class HttpClients {
private static ThreadLocal<HttpClientConnectionManager> connectionManagerMapper = new ThreadLocal<HttpClientConnectionManager>() {
@Override
public HttpClientConnectionManager initialValue() {
if (log.isDebugEnabled()) log.debug("[HttpClients] Creating new HttpClientConnectionManager, for thread [%s]", Thread.currentThread().getId());
Configuration config = Configuration.instance();
return createConnectionManager(
......@@ -42,8 +45,14 @@ public abstract class HttpClients {
@Override
public HttpClient initialValue() {
HttpClientConnectionManager connectionManager= connectionManagerMapper.get();
if (log.isDebugEnabled()) log.debug("[HttpClients] Creating new HttpClient, for thread [%s]", Thread.currentThread().getId());
return createHttpClient(connectionManager, 0);
}
@Override
public void remove() {
super.remove();
}
};
public static HttpClient getThreadHttpClient(final Integer timeout) {
......@@ -58,14 +67,18 @@ public abstract class HttpClients {
}
/**
* Remlove client from the thread
* Remove client from the thread
*/
public static void remove() {
connectionManagerMapper.remove();
httpClientsMapper.remove();
}
protected static HttpClient createHttpClient(HttpClientConnectionManager connectionManager, int timeout) {
public static HttpClient createHttpClient(int timeout) {
return createHttpClient(null,timeout);
}
public static HttpClient createHttpClient(HttpClientConnectionManager connectionManager, int timeout) {
if (timeout <= 0) {
Configuration config = Configuration.instance();
timeout = config.getNetworkTimeout();
......@@ -79,7 +92,7 @@ public abstract class HttpClients {
}
protected static PoolingHttpClientConnectionManager createConnectionManager(
public static PoolingHttpClientConnectionManager createConnectionManager(
int maxTotalConnections,
int maxConnectionsPerRoute,
int timeout) {
......@@ -92,7 +105,7 @@ public abstract class HttpClients {
return connectionManager;
}
protected static RequestConfig createRequestConfig(int timeout) {
public static RequestConfig createRequestConfig(int timeout) {
return RequestConfig.custom()
.setSocketTimeout(timeout).setConnectTimeout(timeout)
.setMaxRedirects(1)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment