HttpTest.java 3.04 KB
package cn.runsa.crmapp.base;

import java.nio.charset.CodingErrorAction;
import java.util.concurrent.ArrayBlockingQueue;

import javax.net.ssl.SSLContext;

import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
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.conn.NoopIOSessionStrategy;
import org.apache.http.nio.conn.SchemeIOSessionStrategy;
import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
import org.apache.http.nio.reactor.ConnectingIOReactor;
import org.apache.http.ssl.SSLContexts;

import cn.runsa.crmapp.base.http.TrustAllStrategy;

/**
 * @author Stephen
 */
public class HttpTest {
	
	private static PoolingNHttpClientConnectionManager pool;
	
	private static final ArrayBlockingQueue<Boolean> blockQueue = new ArrayBlockingQueue<Boolean>(100);

	public static void main(String[] args) throws Exception {
		
		CloseableHttpAsyncClient asyncClient;
		
		ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
		
		SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustAllStrategy()).build();
		
		SSLIOSessionStrategy sslStrategy = new SSLIOSessionStrategy(sslContext);
		
		Registry<SchemeIOSessionStrategy> registry = RegistryBuilder.<SchemeIOSessionStrategy> create().register("http", NoopIOSessionStrategy.INSTANCE).register("https", sslStrategy).build();
		
		ConnectionConfig connectionConfig = ConnectionConfig.custom().setMalformedInputAction(CodingErrorAction.IGNORE).setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8).build();
		
		pool = new PoolingNHttpClientConnectionManager(ioReactor, registry);
		pool.setDefaultConnectionConfig(connectionConfig);
		pool.setMaxTotal(5);
		pool.setDefaultMaxPerRoute(5);
		
		asyncClient = HttpAsyncClients.custom().setConnectionManager(pool).build();
		
		asyncClient.start();
		
		for(int i=0;i<10000000;i++){
			HttpUriRequest  request = new HttpGet("https://www.baidu.com");
			blockQueue.put(true);
			asyncClient.execute(request, new MyResponseCallback());
			System.out.println(""+i);
		}
		Thread.sleep(100000);
		asyncClient.close();
		pool.shutdown();
		
	}
	
	static class MyResponseCallback implements FutureCallback<HttpResponse>{
		
		@Override
		public void completed(HttpResponse result) {
			System.out.println("==="+result.getStatusLine().getStatusCode());
			OK();
		}

		@Override
		public void failed(Exception ex) {
			System.out.println("EXCEPTION...");
			OK();
		}

		@Override
		public void cancelled() {
			System.out.println("CANCELLED...");
			OK();
		}
		
		private void OK(){
			blockQueue.poll();
		}
		
	}

}