1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package groovyx.net.http;
23
24 import java.io.IOException;
25 import java.net.URISyntaxException;
26 import java.util.Map;
27 import java.util.concurrent.Callable;
28 import java.util.concurrent.Executors;
29 import java.util.concurrent.Future;
30 import java.util.concurrent.ThreadPoolExecutor;
31
32 import org.apache.http.HttpVersion;
33 import org.apache.http.client.ClientProtocolException;
34 import org.apache.http.conn.ClientConnectionManager;
35 import org.apache.http.conn.params.ConnManagerParams;
36 import org.apache.http.conn.params.ConnPerRouteBean;
37 import org.apache.http.conn.scheme.PlainSocketFactory;
38 import org.apache.http.conn.scheme.Scheme;
39 import org.apache.http.conn.scheme.SchemeRegistry;
40 import org.apache.http.conn.ssl.SSLSocketFactory;
41 import org.apache.http.impl.client.DefaultHttpClient;
42 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
43 import org.apache.http.params.BasicHttpParams;
44 import org.apache.http.params.HttpParams;
45 import org.apache.http.params.HttpProtocolParams;
46
47
48
49
50
51
52
53
54
55
56 public class AsyncHTTPBuilder extends HTTPBuilder {
57
58
59
60
61 public static final int DEFAULT_POOL_SIZE = 4;
62
63 protected final ThreadPoolExecutor threadPool =
64 (ThreadPoolExecutor)Executors.newCachedThreadPool();
65
66
67
68
69
70
71
72
73
74 public AsyncHTTPBuilder( Map<String, ?> args ) throws URISyntaxException {
75 super();
76 Object poolSize = args.get("poolSize");
77 if ( poolSize == null ) poolSize = DEFAULT_POOL_SIZE;
78 this.initThreadPools( (Integer)poolSize );
79
80 Object defaultURL = args.get("url");
81 if ( defaultURL != null ) super.setURL(defaultURL);
82
83 Object defaultContentType = args.get("contentType");
84 if ( defaultContentType != null )
85 super.setContentType(defaultContentType);
86 }
87
88
89
90
91
92
93
94
95
96
97 @Override
98 protected Future<?> doRequest( final SendDelegate delegate ) {
99 return threadPool.submit( new Callable<Object>() {
100
101 try {
102 return doRequestSuper(delegate);
103 }
104 catch( Exception ex ) {
105 log.error( "Exception thrown from request delegate: " +
106 delegate, ex );
107 throw ex;
108 }
109 }
110 });
111 }
112
113
114
115
116
117 private Object doRequestSuper( SendDelegate delegate ) throws IOException {
118 return super.doRequest(delegate);
119 }
120
121
122
123
124
125 protected void initThreadPools( final int poolSize ) {
126 if (poolSize < 1) throw new IllegalArgumentException("poolSize may not be < 1");
127
128 HttpParams params = client != null ? client.getParams()
129 : new BasicHttpParams();
130 ConnManagerParams.setMaxTotalConnections(params, poolSize);
131 ConnManagerParams.setMaxConnectionsPerRoute(params,
132 new ConnPerRouteBean(poolSize));
133
134 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
135
136
137 SchemeRegistry schemeRegistry = new SchemeRegistry();
138 schemeRegistry.register( new Scheme( "http",
139 PlainSocketFactory.getSocketFactory(), 80 ) );
140 schemeRegistry.register( new Scheme( "https",
141 SSLSocketFactory.getSocketFactory(), 443));
142
143 ClientConnectionManager cm = new ThreadSafeClientConnManager(
144 params, schemeRegistry );
145 super.client = new DefaultHttpClient( cm, params );
146
147
148
149
150
151
152 this.threadPool.setMaximumPoolSize(poolSize);
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 public ThreadPoolExecutor getThreadPoolExecutor() {
170 return this.threadPool;
171 }
172
173
174
175
176 @Override public void shutdown() {
177 super.shutdown();
178 this.threadPool.shutdown();
179 }
180
181
182
183
184
185 @Override protected void finalize() throws Throwable {
186 this.shutdown();
187 super.finalize();
188 }
189 }