httpasyncclient初体验
背景
httpasyncclient使用了nio进行封装。所有请求可以塞入缓存区,不阻塞当前线程。
同时,通过回调机制,产生类似于aio的效果。
体验
pom文件
http异步客户端
<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpasyncclient</artifactId>
	<version>4.1.4</version>
</dependency>
代码
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.concurrent.FutureCallback;
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.impl.nio.reactor.IOReactorConfig;
import org.apache.http.nio.reactor.ConnectingIOReactor;
import org.apache.http.util.EntityUtils;
public class MainTest222 {
	public static void main(String[] args) throws Exception {
		// 配置io线程
		IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
				.setIoThreadCount(Runtime.getRuntime().availableProcessors()).setSoKeepAlive(true).build();
		// 设置连接池大小
		ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
		PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor);
		connManager.setMaxTotal(100);
		connManager.setDefaultMaxPerRoute(100);
		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000)
				.setConnectionRequestTimeout(1000).build();
		final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setConnectionManager(connManager)
				.setDefaultRequestConfig(requestConfig).build();
		// 构造请求
		String url = "https://note.abeffect.com/";
		HttpGet httpGet = new HttpGet(url);
		// start
		client.start();
		// 异步请求
		client.execute(httpGet, new CallBack());
		System.out.println("run 1");
		client.execute(httpGet, new CallBack());
		System.out.println("run 2");
		client.execute(httpGet, new CallBack());
		System.out.println("run 3");
		client.execute(httpGet, new CallBack());
		System.out.println("run 4");
		client.execute(httpGet, new CallBack());
		System.out.println("run 5");
		System.out.println("sleep 60s then exit. ");
		TimeUnit.SECONDS.sleep(60);
	}
	private static class CallBack implements FutureCallback<HttpResponse> {
		private long start = System.currentTimeMillis();
		@Override
		public void completed(HttpResponse httpResponse) {
			try {
				System.out.println("cost " + (System.currentTimeMillis() - start) + " ms, length = "
						+ EntityUtils.toString(httpResponse.getEntity()).length());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		@Override
		public void failed(Exception e) {
			System.err.println(" cost " + (System.currentTimeMillis() - start) + " ms, " + e);
		}
		@Override
		public void cancelled() {
		}
	}
}
运行效果
run 1
run 2
run 3
run 4
run 5
sleep 60s then exit. 
cost 3073 ms, length = 60497
cost 3077 ms, length = 60497
cost 3079 ms, length = 60497
cost 3244 ms, length = 60497
cost 3330 ms, length = 60497
 我的开源
我的开源