HttpAsyncClient 简单实例
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
import org.apache.http.nio.reactor.ConnectingIOReactor;
import org.apache.http.nio.reactor.IOReactorException;
/**
- 异步http客户端
-
*/
public class AsyncClient {
private static int MAX_CONN_COUNT = 20000;
private static CloseableHttpAsyncClient asyncClient;
static {
ConnectingIOReactor ioReactor;
try {
ioReactor = new DefaultConnectingIOReactor();
} catch (IOReactorException e) {
throw new RuntimeException(e);
}
PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(ioReactor);
cm.setMaxTotal(MAX_CONN_COUNT);
cm.setDefaultMaxPerRoute(MAX_CONN_COUNT);
asyncClient = HttpAsyncClients.custom().setConnectionManager(cm).build();
asyncClient.start();
}
public static CloseableHttpAsyncClient getInstance() {
return asyncClient;
}
}