Skip to content

Commit ea6c88a

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 8434f82 + 3aa3ac2 commit ea6c88a

File tree

4 files changed

+499
-6
lines changed

4 files changed

+499
-6
lines changed
Lines changed: 350 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,373 @@
11
package org.baeldung.httpclient;
22

3+
import static org.junit.Assert.assertTrue;
4+
35
import java.io.IOException;
46
import java.util.concurrent.ExecutionException;
7+
import java.util.concurrent.TimeUnit;
58

9+
import org.apache.http.HeaderElement;
10+
import org.apache.http.HeaderElementIterator;
11+
import org.apache.http.HttpClientConnection;
612
import org.apache.http.HttpException;
713
import org.apache.http.HttpHost;
14+
import org.apache.http.HttpResponse;
15+
import org.apache.http.client.ClientProtocolException;
16+
import org.apache.http.client.config.RequestConfig;
17+
import org.apache.http.client.methods.CloseableHttpResponse;
18+
import org.apache.http.client.methods.HttpGet;
819
import org.apache.http.client.protocol.HttpClientContext;
20+
import org.apache.http.config.SocketConfig;
21+
import org.apache.http.conn.ConnectionKeepAliveStrategy;
922
import org.apache.http.conn.ConnectionRequest;
1023
import org.apache.http.conn.routing.HttpRoute;
24+
import org.apache.http.impl.client.CloseableHttpClient;
25+
import org.apache.http.impl.client.HttpClients;
1126
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
27+
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
28+
import org.apache.http.message.BasicHeaderElementIterator;
29+
import org.apache.http.protocol.HTTP;
30+
import org.apache.http.protocol.HttpContext;
31+
import org.apache.http.protocol.HttpCoreContext;
32+
import org.apache.http.protocol.HttpRequestExecutor;
33+
import org.apache.http.util.EntityUtils;
34+
import org.junit.After;
35+
import org.junit.Before;
36+
import org.junit.Ignore;
1237
import org.junit.Test;
1338

14-
@SuppressWarnings("unused")
39+
1540
public class HttpClientConnectionManagementTest {
41+
private BasicHttpClientConnectionManager basicConnManager;
42+
private HttpClientContext context;
43+
private HttpRoute route;
44+
private static final String SERVER1 = "http://echo.200please.com";
45+
private static final String SERVER7 = "http://localhost";
46+
private HttpGet get1;
47+
private HttpGet get2;
48+
private static CloseableHttpResponse response;
49+
private HttpClientConnection conn1;
50+
private HttpClientConnection conn;
51+
private HttpClientConnection conn2;
52+
private PoolingHttpClientConnectionManager poolingConnManager;
53+
private CloseableHttpClient client;
54+
55+
@Before
56+
public final void before() {
57+
get1 = new HttpGet(SERVER1);
58+
get2 = new HttpGet(SERVER7);
59+
route = new HttpRoute(new HttpHost("localhost", 80));
60+
}
61+
62+
@After
63+
public final void after() throws IllegalStateException, IOException {
64+
if (conn != null)
65+
conn.close();
66+
if (conn1 != null)
67+
conn1.close();
68+
if (conn2 != null)
69+
conn2.close();
70+
if (poolingConnManager != null)
71+
poolingConnManager.shutdown();
72+
if (basicConnManager != null)
73+
basicConnManager.shutdown();
74+
if (client != null)
75+
client.close();
76+
if (response != null)
77+
response.close();
78+
79+
}
1680

1781
// tests
1882

1983
@Test
84+
@Ignore
85+
// 2.1 IN ARTCLE
2086
public final void whenLowLevelConnectionIsEstablished_thenNoExceptions() throws IOException, HttpException, InterruptedException, ExecutionException {
21-
final HttpClientContext context = HttpClientContext.create();
22-
final BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager();
23-
final HttpRoute route = new HttpRoute(new HttpHost("localhost", 80));
24-
final ConnectionRequest connRequest = connManager.requestConnection(route, null);
87+
basicConnManager = new BasicHttpClientConnectionManager();
88+
final ConnectionRequest connRequest = basicConnManager.requestConnection(route, null);
89+
assertTrue(connRequest.get(1000, TimeUnit.SECONDS) != null);
90+
}
91+
92+
@Test
93+
@Ignore
94+
// 2.2 IN ARTICLE
95+
public final void whenOpeningLowLevelConnectionWithSocketTimeout_thenNoExceptions() throws InterruptedException, ExecutionException, IOException, HttpException {
96+
basicConnManager = new BasicHttpClientConnectionManager();
97+
context = HttpClientContext.create();
98+
final ConnectionRequest connRequest = basicConnManager.requestConnection(route, null);
99+
conn = connRequest.get(1000, TimeUnit.SECONDS);
100+
if (!conn.isOpen())
101+
basicConnManager.connect(conn, route, 1000, context);
102+
conn.setSocketTimeout(30000);
103+
104+
assertTrue(conn.getSocketTimeout() == 30000);
105+
assertTrue(conn.isOpen());
106+
}
107+
108+
@Test
109+
@Ignore
110+
// Example 3.1. TESTER VERSION
111+
public final void WhenTwoConnectionsForTwoRequests_ThenLeaseTwoConnectionsNoExceptions() throws InterruptedException {
112+
get1 = new HttpGet("http://localhost");
113+
get2 = new HttpGet("http://google.com");
114+
poolingConnManager = new PoolingHttpClientConnectionManager();
115+
final CloseableHttpClient client1 = HttpClients.custom().setConnectionManager(poolingConnManager).build();
116+
final CloseableHttpClient client2 = HttpClients.custom().setConnectionManager(poolingConnManager).build();
117+
final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client1, get1, poolingConnManager);
118+
final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client2, get2, poolingConnManager);
119+
thread1.start();
120+
thread1.join();
121+
thread2.start();
122+
assertTrue(poolingConnManager.getTotalStats().getLeased() == 1);
123+
thread2.join(1000);
124+
assertTrue(poolingConnManager.getTotalStats().getLeased() == 2);
125+
}
126+
127+
@Test
128+
@Ignore
129+
// Example 3.1.ARTICLE VERSION
130+
public final void WhenTwoConnectionsForTwoRequests_ThensNoExceptions() throws InterruptedException {
131+
get1 = new HttpGet("http://localhost");
132+
get2 = new HttpGet("http://google.com");
133+
poolingConnManager = new PoolingHttpClientConnectionManager();
134+
final CloseableHttpClient client1 = HttpClients.custom().setConnectionManager(poolingConnManager).build();
135+
final CloseableHttpClient client2 = HttpClients.custom().setConnectionManager(poolingConnManager).build();
136+
final MultiHttpClientConnThread thread1 = new MultiHttpClientConnThread(client1, get1);
137+
final MultiHttpClientConnThread thread2 = new MultiHttpClientConnThread(client2, get2);
138+
thread1.start();
139+
thread1.join();
140+
thread2.start();
141+
thread2.join();
142+
}
143+
144+
@Test
145+
@Ignore
146+
// 3.3
147+
public final void whenIncreasingConnectionPool_thenNoEceptions() {
148+
149+
poolingConnManager = new PoolingHttpClientConnectionManager();
150+
poolingConnManager.setMaxTotal(5);
151+
poolingConnManager.setDefaultMaxPerRoute(4);
152+
final HttpHost localhost = new HttpHost("locahost", 80);
153+
poolingConnManager.setMaxPerRoute(new HttpRoute(localhost), 5);
154+
}
155+
156+
@Test
157+
@Ignore
158+
// 3.4 Tester Version
159+
public final void whenExecutingSameRequestsInDifferentThreads_thenUseDefaultConnLimitNoExceptions() throws InterruptedException, IOException {
160+
final HttpGet get = new HttpGet("http://google.com");
161+
poolingConnManager = new PoolingHttpClientConnectionManager();
162+
client = HttpClients.custom().setConnectionManager(poolingConnManager).build();
163+
final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager);
164+
final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager);
165+
final TesterVersion_MultiHttpClientConnThread thread3 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager);
166+
thread1.start();
167+
thread1.join(1000);
168+
assertTrue(poolingConnManager.getTotalStats().getLeased() == 1);
169+
thread2.start();
170+
thread2.join(1000);
171+
assertTrue(poolingConnManager.getTotalStats().getLeased() == 2);
172+
thread3.start();
173+
thread3.join(1000);
174+
}
175+
176+
@Test
177+
@Ignore
178+
// 3.4 Article version
179+
public final void whenExecutingSameRequestsInDifferentThreads_thenExxecuteReuqesttNoExceptions() throws InterruptedException {
180+
final HttpGet get = new HttpGet("http://localhost");
181+
poolingConnManager = new PoolingHttpClientConnectionManager();
182+
client = HttpClients.custom().setConnectionManager(poolingConnManager).build();
183+
final MultiHttpClientConnThread thread1 = new MultiHttpClientConnThread(client, get);
184+
final MultiHttpClientConnThread thread2 = new MultiHttpClientConnThread(client, get);
185+
final MultiHttpClientConnThread thread3 = new MultiHttpClientConnThread(client, get);
186+
thread1.start();
187+
thread2.start();
188+
thread3.start();
189+
thread1.join();
190+
thread2.join();
191+
thread3.join();
192+
}
193+
194+
@Test
195+
@Ignore
196+
// 4.1
197+
public final void whenCustomizingKeepAliveStrategy_thenNoExceptions() throws ClientProtocolException, IOException {
198+
final ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
199+
@Override
200+
public long getKeepAliveDuration(final HttpResponse myResponse, final HttpContext myContext) {
201+
final HeaderElementIterator it = new BasicHeaderElementIterator(myResponse.headerIterator(HTTP.CONN_KEEP_ALIVE));
202+
while (it.hasNext()) {
203+
final HeaderElement he = it.nextElement();
204+
final String param = he.getName();
205+
final String value = he.getValue();
206+
if (value != null && param.equalsIgnoreCase("timeout")) {
207+
return Long.parseLong(value) * 1000;
208+
}
209+
}
210+
final HttpHost target = (HttpHost) myContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
211+
if ("localhost".equalsIgnoreCase(target.getHostName())) {
212+
return 10 * 1000;
213+
} else {
214+
return 5 * 1000;
215+
}
216+
}
217+
218+
};
219+
client = HttpClients.custom().setKeepAliveStrategy(myStrategy).setConnectionManager(poolingConnManager).build();
220+
client.execute(get1);
221+
client.execute(get2);
222+
}
223+
224+
@Test
225+
@Ignore
226+
// 5.1
227+
public final void GivenBasicHttpClientConnManager_whenConnectionReuse_thenNoExceptions() throws InterruptedException, ExecutionException, IOException, HttpException {
228+
basicConnManager = new BasicHttpClientConnectionManager();
229+
context = HttpClientContext.create();
230+
final HttpGet get = new HttpGet("http://localhost");
231+
HttpResponse thisResponse = null;
232+
final ConnectionRequest connRequest = basicConnManager.requestConnection(route, null);
233+
client = HttpClients.custom().setConnectionManager(basicConnManager).build();
234+
boolean respAvail = false;
235+
conn = connRequest.get(10, TimeUnit.SECONDS);
236+
if (!conn.isOpen()) {
237+
basicConnManager.connect(conn, route, 1000, context);
238+
basicConnManager.routeComplete(conn, route, context);
239+
final HttpRequestExecutor exeRequest = new HttpRequestExecutor();
240+
context.setTargetHost((new HttpHost("localhost", 80)));
241+
thisResponse = exeRequest.execute(get, conn, context);
242+
respAvail = conn.isResponseAvailable(1000);
243+
}
244+
basicConnManager.releaseConnection(conn, null, 1, TimeUnit.SECONDS);
245+
if (respAvail) {
246+
client.execute(get);
247+
}
248+
}
249+
250+
@Test
251+
@Ignore
252+
// 5.2 TESTER VERSION
253+
public final void WhenConnectionsNeededGreaterThanMaxTotal_thenReuseConnectionsNoExceptions() throws InterruptedException {
254+
poolingConnManager = new PoolingHttpClientConnectionManager();
255+
poolingConnManager.setDefaultMaxPerRoute(5);
256+
poolingConnManager.setMaxTotal(5);
257+
client = HttpClients.custom().setConnectionManager(poolingConnManager).build();
258+
final MultiHttpClientConnThread[] threads = new MultiHttpClientConnThread[10];
259+
int countConnMade = 0;
260+
for (int i = 0; i < threads.length; i++) {
261+
threads[i] = new MultiHttpClientConnThread(client, get1, poolingConnManager);
262+
}
263+
for (final MultiHttpClientConnThread thread : threads) {
264+
thread.start();
265+
}
266+
for (final MultiHttpClientConnThread thread : threads) {
267+
thread.join(10000);
268+
countConnMade++;
269+
if (countConnMade == 0)
270+
assertTrue(thread.getLeasedConn() == 5);
271+
}
272+
}
273+
274+
@Test
275+
// 5.2 ARTICLE VERSION
276+
@Ignore
277+
public final void WhenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuseNoExceptions() throws InterruptedException {
278+
final HttpGet get = new HttpGet("http://echo.200please.com");
279+
poolingConnManager = new PoolingHttpClientConnectionManager();
280+
poolingConnManager.setDefaultMaxPerRoute(5);
281+
poolingConnManager.setMaxTotal(5);
282+
client = HttpClients.custom().setConnectionManager(poolingConnManager).build();
283+
final MultiHttpClientConnThread[] threads = new MultiHttpClientConnThread[10];
284+
for (int i = 0; i < threads.length; i++) {
285+
threads[i] = new MultiHttpClientConnThread(client, get, poolingConnManager);
286+
}
287+
for (final MultiHttpClientConnThread thread : threads) {
288+
thread.start();
289+
}
290+
for (final MultiHttpClientConnThread thread : threads) {
291+
thread.join(10000);
292+
}
293+
}
25294

26-
connManager.shutdown();
295+
@Test
296+
@Ignore
297+
// 6.2.1
298+
public final void whenConfiguringTimeOut_thenNoExceptions() {
299+
route = new HttpRoute(new HttpHost("localhost", 80));
300+
poolingConnManager = new PoolingHttpClientConnectionManager();
301+
poolingConnManager.setSocketConfig(route.getTargetHost(), SocketConfig.custom().setSoTimeout(5000).build());
302+
assertTrue(poolingConnManager.getSocketConfig(route.getTargetHost()).getSoTimeout() == 5000);
27303
}
28304

305+
@Test
306+
@Ignore
307+
// 7.1
308+
public final void whenHttpClientChecksStaleConns_thenNoExceptions() {
309+
poolingConnManager = new PoolingHttpClientConnectionManager();
310+
client = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().setStaleConnectionCheckEnabled(true).build()).setConnectionManager(poolingConnManager).build();
311+
}
312+
313+
@Test
314+
@Ignore
315+
// 7.2 TESTER VERSION
316+
public final void whenCustomizedIdleConnMonitor_thenEliminateIdleConnsNoExceptions() throws InterruptedException, IOException {
317+
poolingConnManager = new PoolingHttpClientConnectionManager();
318+
client = HttpClients.custom().setConnectionManager(poolingConnManager).build();
319+
final IdleConnectionMonitorThread staleMonitor = new IdleConnectionMonitorThread(poolingConnManager);
320+
final HttpGet get = new HttpGet("http://google.com");
321+
// test this with new HttpGet("http://iotechperu.com")----First test will fail b/c there is redirect connection at that site
322+
final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager);
323+
final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager);
324+
final TesterVersion_MultiHttpClientConnThread thread3 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager);
325+
staleMonitor.start();
326+
thread1.start();
327+
thread1.join();
328+
thread2.start();
329+
thread2.join();
330+
thread3.start();
331+
assertTrue(poolingConnManager.getTotalStats().getAvailable() == 1);
332+
thread3.join(32000);
333+
assertTrue(poolingConnManager.getTotalStats().getAvailable() == 0);
334+
}
335+
336+
@Test
337+
@Ignore
338+
// 7.2 ARTICLE VERSION
339+
public final void whenCustomizedIdleConnMonitor_thenNoExceptions() throws InterruptedException, IOException {
340+
final HttpGet get = new HttpGet("http://google.com");
341+
poolingConnManager = new PoolingHttpClientConnectionManager();
342+
client = HttpClients.custom().setConnectionManager(poolingConnManager).build();
343+
final IdleConnectionMonitorThread staleMonitor = new IdleConnectionMonitorThread(poolingConnManager);
344+
staleMonitor.start();
345+
staleMonitor.join(1000);
346+
}
347+
348+
@Test(expected = IllegalStateException.class)
349+
@Ignore
350+
// 8.1
351+
public final void whenClosingConnectionsandManager_thenCloseWithNoExceptions() throws InterruptedException, ExecutionException, IOException, HttpException {
352+
route = new HttpRoute(new HttpHost("google.com", 80));
353+
final HttpGet get = new HttpGet("http://google.com");
354+
poolingConnManager = new PoolingHttpClientConnectionManager();
355+
final ConnectionRequest connRequest = poolingConnManager.requestConnection(route, null);
356+
context = HttpClientContext.create();
357+
conn = connRequest.get(10, TimeUnit.SECONDS);
358+
poolingConnManager.connect(conn, route, 10000, context);
359+
client = HttpClients.custom().setConnectionManager(poolingConnManager).build();
360+
response = client.execute(get);
361+
EntityUtils.consume(response.getEntity());
362+
client.close();
363+
conn.close();
364+
response.close();
365+
poolingConnManager.close();
366+
poolingConnManager.shutdown();
367+
client.execute(get);
368+
conn.sendRequestHeader(get);
369+
assertTrue(!conn.isOpen());
370+
assertTrue(conn.isOpen());
371+
assertTrue(response.getEntity() == null);
372+
}
29373
}

0 commit comments

Comments
 (0)