/** * Tests that if a socket fails to connect, the allocated connection is properly released back to * the connection manager. */ @Test public void testSocketConnectFailureReleasesConnection() throws Exception { final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class); final ConnectionRequest connrequest = Mockito.mock(ConnectionRequest.class); Mockito.when(connrequest.get(Mockito.anyInt(), Mockito.any(TimeUnit.class))).thenReturn(conn); final HttpClientConnectionManager connmgr = Mockito.mock(HttpClientConnectionManager.class); Mockito.doThrow(new ConnectException()) .when(connmgr) .connect( Mockito.any(HttpClientConnection.class), Mockito.any(HttpRoute.class), Mockito.anyInt(), Mockito.any(HttpContext.class)); Mockito.when(connmgr.requestConnection(Mockito.any(HttpRoute.class), Mockito.any())) .thenReturn(connrequest); final HttpClient client = HttpClients.custom().setConnectionManager(connmgr).build(); final HttpContext context = new BasicHttpContext(); final HttpGet httpget = new HttpGet("http://www.example.com/a"); try { client.execute(httpget, context); Assert.fail("expected IOException"); } catch (final IOException expected) { } Mockito.verify(connmgr).releaseConnection(conn, null, 0, TimeUnit.MILLISECONDS); }
public void request2(String uri, int reqNum, int reqTerm) throws Exception { HttpClientContext context = HttpClientContext.create(); HttpClientConnectionManager connManager = new BasicHttpClientConnectionManager(); HttpRoute route = new HttpRoute(new HttpHost(uri, 80)); // Request new connection. This can be a long process if (reqNum == 0) { System.out.println( String.format("request to %s infinite times with term '%d' ms", uri, reqTerm)); } else { System.out.println( String.format("request to %s '%d' times with term '%d' ms", uri, reqNum, reqTerm)); } int i = 0, tick = 0; while (true) { usleep(reqTerm); tick = (int) (Math.random() * 10) % 2; if (tick == 0) { continue; } System.out.println("111111111"); ConnectionRequest connRequest = connManager.requestConnection(route, null); // Wait for connection up to 10 sec HttpClientConnection conn = connRequest.get(10, TimeUnit.SECONDS); System.out.println("222222222"); try { if (!conn.isOpen()) // If not open { System.out.println("333333333"); // establish connection based on its route info connManager.connect(conn, route, 1000, context); // and mark it as route complete connManager.routeComplete(conn, route, context); } // Do useful things with the connection. System.out.println("request " + uri); System.out.println( "--> response status = " + conn.receiveResponseHeader().getStatusLine()); } finally { connManager.releaseConnection(conn, null, 1, TimeUnit.MINUTES); } System.out.println("----------------------------------------"); if (reqNum != 0 && reqNum < ++i) { break; } } }
@Override public void releaseConnection( final HttpClientConnection conn, final Object newState, final long validDuration, final TimeUnit timeUnit) { connectionManager.releaseConnection(conn, newState, validDuration, timeUnit); }
@Override public void connect( final HttpClientConnection conn, final HttpRoute route, final int connectTimeout, final HttpContext context) throws IOException { connectionManager.connect(conn, route, connectTimeout, context); }
@Override public void run() { Stopwatch start = executionTimeStats.start(); try { HttpClientConnectionManager cm = (HttpClientConnectionManager) apacheHttpClient .getConfiguration() .getProperty(ApacheClientProperties.CONNECTION_MANAGER); cm.closeIdleConnections(connectionIdleTimeout, TimeUnit.SECONDS); } catch (Throwable e) { s_logger.error("Cannot clean connections", e); cleanupFailed.increment(); } finally { if (null != start) { start.stop(); } } }
@Before public void setup() throws Exception { MockitoAnnotations.initMocks(this); mainClientExec = new MainClientExec( requestExecutor, connManager, reuseStrategy, keepAliveStrategy, proxyHttpProcessor, targetAuthStrategy, proxyAuthStrategy, userTokenHandler); target = new HttpHost("foo", 80); proxy = new HttpHost("bar", 8888); Mockito.when(connManager.requestConnection(Mockito.<HttpRoute>any(), Mockito.any())) .thenReturn(connRequest); Mockito.when(connRequest.get(Mockito.anyLong(), Mockito.<TimeUnit>any())) .thenReturn(managedConn); final Map<String, Header> challenges = new HashMap<String, Header>(); challenges.put("basic", new BasicHeader(AUTH.WWW_AUTH, "Basic realm=test")); final AuthOption authOption = new AuthOption(new BasicScheme(), new UsernamePasswordCredentials("user:pass")); Mockito.when( targetAuthStrategy.getChallenges( Mockito.eq(target), Mockito.<HttpResponse>any(), Mockito.<HttpClientContext>any())) .thenReturn(challenges); Mockito.when( targetAuthStrategy.getChallenges( Mockito.eq(target), Mockito.<HttpResponse>any(), Mockito.<HttpClientContext>any())) .thenReturn(challenges); Mockito.when( targetAuthStrategy.select( Mockito.same(challenges), Mockito.eq(target), Mockito.<HttpResponse>any(), Mockito.<HttpClientContext>any())) .thenReturn(new LinkedList<AuthOption>(Arrays.asList(authOption))); Mockito.when( proxyAuthStrategy.getChallenges( Mockito.eq(proxy), Mockito.<HttpResponse>any(), Mockito.<HttpClientContext>any())) .thenReturn(challenges); Mockito.when( proxyAuthStrategy.getChallenges( Mockito.eq(proxy), Mockito.<HttpResponse>any(), Mockito.<HttpClientContext>any())) .thenReturn(challenges); Mockito.when( proxyAuthStrategy.select( Mockito.same(challenges), Mockito.eq(proxy), Mockito.<HttpResponse>any(), Mockito.<HttpClientContext>any())) .thenReturn(new LinkedList<AuthOption>(Arrays.asList(authOption))); }
@Override public void run() { try { while (!shutdown) { if (!isblock.get()) { synchronized (this) { isblock.set(true); wait(); if (connMgrs.size() > 0) { for (int i = 0; i < connMgrs.size(); i++) { HttpClientConnectionManager connMgr = connMgrs.get(i); connMgr.closeExpiredConnections(); connMgr.closeIdleConnections(30, TimeUnit.SECONDS); } } } } } } catch (InterruptedException ex) { // terminate } }
public void reallyShutdown() { connectionManager.shutdown(); }
@Override public void closeExpiredConnections() { connectionManager.closeExpiredConnections(); }
@Override public void closeIdleConnections(final long idletime, final TimeUnit tunit) { connectionManager.closeIdleConnections(idletime, tunit); }
@Override public void routeComplete( final HttpClientConnection conn, final HttpRoute route, final HttpContext context) throws IOException { connectionManager.routeComplete(conn, route, context); }
@Override public ConnectionRequest requestConnection(final HttpRoute route, final Object state) { return connectionManager.requestConnection(route, state); }