Last active
October 11, 2022 07:57
-
-
Save leewin12/e384271ed42fd4337eb6 to your computer and use it in GitHub Desktop.
AsyncHttpClient based on Apache HttpClient4.3.5 and Apache Commons-IO 2.4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util; | |
import org.apache.commons.io.IOUtils; | |
import org.apache.http.client.ResponseHandler; | |
import org.apache.http.client.config.CookieSpecs; | |
import org.apache.http.client.config.RequestConfig; | |
import org.apache.http.client.methods.HttpUriRequest; | |
import org.apache.http.impl.client.CloseableHttpClient; | |
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler; | |
import org.apache.http.impl.client.HttpClients; | |
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | |
import play.Logger; | |
import java.util.concurrent.*; | |
public class HttpUtil { | |
public static final int MAX_CONNECTION = 1000; | |
public static final int TIMEOUT = 5000; | |
private static PoolingHttpClientConnectionManager cm = null; | |
private static RequestConfig requestConfig = null; | |
public static CloseableHttpClient httpclient = null; | |
private static ExecutorService pool = Executors.newFixedThreadPool(MAX_CONNECTION); | |
static { | |
cm = new PoolingHttpClientConnectionManager(); | |
cm.setMaxTotal(MAX_CONNECTION); | |
cm.setDefaultMaxPerRoute(MAX_CONNECTION); | |
requestConfig = RequestConfig.custom() | |
.setCookieSpec(CookieSpecs.IGNORE_COOKIES) | |
.setConnectTimeout(TIMEOUT) | |
.setConnectionRequestTimeout(TIMEOUT) | |
.setSocketTimeout(TIMEOUT) | |
.build(); | |
httpclient = HttpClients.custom() | |
.setConnectionManager(cm) | |
.setDefaultRequestConfig(requestConfig) | |
.setRetryHandler(new DefaultHttpRequestRetryHandler()) | |
.build(); | |
} | |
/** | |
* | |
* @param req | |
* @param handler | |
* @param <T> | |
* @return | |
* @throws InterruptedException | |
* @throws java.util.concurrent.ExecutionException | |
* @throws java.util.concurrent.TimeoutException | |
*/ | |
public static <T> Future<T> run(final HttpUriRequest req, final ResponseHandler<T> handler) { | |
return pool.submit(new Callable<T>() { | |
@Override | |
public T call() throws Exception { | |
try { | |
return httpclient.execute(req, handler); | |
} catch (Exception e) { | |
Logger.error("[HttpUtil.run] uri:" + req.getURI().toString(), e); | |
return null; | |
} | |
} | |
}); | |
} | |
public static void shutdown() { | |
pool.shutdown(); | |
try { | |
if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { | |
pool.shutdownNow(); | |
} | |
if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { | |
Logger.error("[HttpUtil.shutdown] pool_termination_fail"); | |
} | |
} catch (InterruptedException e) { | |
pool.shutdownNow(); | |
} | |
IOUtils.closeQuietly(httpclient); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment