HttpTest.java
3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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();
}
}
}