/** Tests the MultiThreadedHttpConnectionManager's ability to reclaim unused connections. */ public void testConnectionReclaiming() { MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); connectionManager.getParams().setDefaultMaxConnectionsPerHost(1); connectionManager.getParams().setMaxTotalConnections(1); HostConfiguration host1 = new HostConfiguration(); host1.setHost("host1", -1, "http"); HostConfiguration host2 = new HostConfiguration(); host2.setHost("host2", -1, "http"); HttpConnection connection = connectionManager.getConnection(host1); // now release this connection connection.releaseConnection(); connection = null; try { // the connection from host1 should be reclaimed connection = connectionManager.getConnectionWithTimeout(host2, 100); } catch (ConnectTimeoutException e) { e.printStackTrace(); fail("a httpConnection should have been available: " + e); } }
/** * Tests the MultiThreadedHttpConnectionManager's ability to restrict the maximum number of * connections. */ public void testMaxConnections() { MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); connectionManager.getParams().setDefaultMaxConnectionsPerHost(1); connectionManager.getParams().setMaxTotalConnections(2); HostConfiguration host1 = new HostConfiguration(); host1.setHost("host1", -1, "http"); HostConfiguration host2 = new HostConfiguration(); host2.setHost("host2", -1, "http"); HttpConnection connection1 = connectionManager.getConnection(host1); HttpConnection connection2 = connectionManager.getConnection(host2); try { // this should fail quickly since the connection has not been released connectionManager.getConnectionWithTimeout(host2, 100); fail("ConnectionPoolTimeoutException should not be available"); } catch (ConnectionPoolTimeoutException e) { // this should throw an exception } // release one of the connections connection2.releaseConnection(); connection2 = null; try { // there should be a connection available now connection2 = connectionManager.getConnectionWithTimeout(host2, 100); } catch (ConnectionPoolTimeoutException e) { e.printStackTrace(); fail("a httpConnection should have been available: " + e); } }
public void testHttpPost() throws URISyntaxException, HttpException, IOException { URI uri = new URI(HTTP_LOCALHOST_60198); PostMethod postMethod = new PostMethod(uri.toString()); postMethod.setRequestEntity(new StringRequestEntity(TEST_MESSAGE)); HttpConnection cnn = new HttpConnection(uri.getHost(), uri.getPort()); cnn.open(); postMethod.execute(new HttpState(), cnn); System.out.println("PostResponse: " + postMethod.getResponseBodyAsString()); }
protected String doHttpGet(String param) throws URISyntaxException, HttpException, IOException { URI uri = new URI(HTTP_LOCALHOST_60198); GetMethod getMethod = new GetMethod(uri.toString()); getMethod.setQueryString("param=" + param); HttpConnection cnn = new HttpConnection(uri.getHost(), uri.getPort()); cnn.open(); getMethod.execute(new HttpState(), cnn); System.out.println("GetResponse: " + getMethod.getResponseBodyAsString()); return getMethod.getResponseBodyAsString(); }
/** * Since the same connection is about to be reused, make sure the previous request was completely * processed, and if not consume it now. * * @param conn The connection */ static void finishLastResponse(HttpConnection conn) { InputStream lastResponse = conn.getLastResponseInputStream(); if (lastResponse != null) { conn.setLastResponseInputStream(null); try { lastResponse.close(); } catch (IOException ioe) { // FIXME: badness - close to force reconnect. conn.close(); } } }
public void testHostReusePreference() { final MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); connectionManager.getParams().setDefaultMaxConnectionsPerHost(1); connectionManager.getParams().setMaxTotalConnections(1); final HostConfiguration host1 = new HostConfiguration(); host1.setHost("host1", -1, "http"); final HostConfiguration host2 = new HostConfiguration(); host2.setHost("host2", -1, "http"); HttpConnection connection = connectionManager.getConnection(host1); GetConnectionThread getHost1 = new GetConnectionThread(host1, connectionManager, 200); GetConnectionThread getHost2 = new GetConnectionThread(host2, connectionManager, 200); getHost2.start(); getHost1.start(); // give the threads some time to startup try { Thread.sleep(100); } catch (InterruptedException e1) { e1.printStackTrace(); } // after the connection to host1 is released it should be given to getHost1 connection.releaseConnection(); connection = null; try { getHost1.join(); getHost2.join(); } catch (InterruptedException e) { e.printStackTrace(); } assertNotSame( "Connection should have been given to someone", getHost1.getConnection(), getHost2.getConnection()); assertNotNull("Connection should have been given to host1", getHost1.getConnection()); assertNull("Connection should NOT have been given to host2", getHost2.getConnection()); }
public void testGetConnection() { MultiThreadedHttpConnectionManager mgr = new MultiThreadedHttpConnectionManager(); HostConfiguration hostConfiguration = new HostConfiguration(); hostConfiguration.setHost("www.nosuchserver.com", 80, "http"); // Create a new connection HttpConnection conn = mgr.getConnection(hostConfiguration); // Validate the connection properties assertEquals("Host", "www.nosuchserver.com", conn.getHost()); assertEquals("Port", 80, conn.getPort()); // Release the connection mgr.releaseConnection(conn); // Create a new connection hostConfiguration.setHost("www.nosuchserver.com", -1, "https"); conn = mgr.getConnection(hostConfiguration); // Validate the connection properties assertEquals("Host", "www.nosuchserver.com", conn.getHost()); assertEquals("Port", 443, conn.getPort()); // Release the connection mgr.releaseConnection(conn); // Create a new connection hostConfiguration.setHost("www.nowhere.org", 8080, "http"); conn = mgr.getConnection(hostConfiguration); // Validate the connection properties assertEquals("Host", "www.nowhere.org", conn.getHost()); assertEquals("Port", 8080, conn.getPort()); // Release the connection mgr.releaseConnection(conn); }
private void resetAppEngineConnection() { mConnection = new HttpConnection("http://botczar.appspot.com/", port); try { mConnection.open(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/* * Implementation copied from HttpMethodBase#readResponseHeaders(HttpState, HttpConnection) but changed to use a custom * header parser (ZapHttpParser#parseHeaders(InputStream, String)). */ @Override protected void readResponseHeaders(HttpState state, HttpConnection conn) throws IOException, HttpException { getResponseHeaderGroup().clear(); Header[] headers = ZapHttpParser.parseHeaders( conn.getResponseInputStream(), getParams().getHttpElementCharset()); // Wire logging moved to HttpParser getResponseHeaderGroup().setHeaders(headers); }
/** * Tests that {@link MultiThreadedHttpConnectionManager#shutdown()} closes all resources and makes * the connection manger unusable. */ public void testShutdown() { MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); connectionManager.getParams().setDefaultMaxConnectionsPerHost(1); connectionManager.getParams().setMaxTotalConnections(1); HostConfiguration host1 = new HostConfiguration(); host1.setHost("host1", -1, "http"); // hold on to the only connection HttpConnection connection = connectionManager.getConnection(host1); // wait for a connection on another thread GetConnectionThread getConn = new GetConnectionThread(host1, connectionManager, 0); getConn.start(); connectionManager.shutdown(); // now release this connection, this should close the connection, but have no other effect connection.releaseConnection(); connection = null; try { getConn.join(); } catch (InterruptedException e) { e.printStackTrace(); } // this thread should have caught an exception without getting a connection assertNull("Not connection should have been checked out", getConn.getConnection()); assertNotNull("There should have been an exception", getConn.getException()); try { connectionManager.getConnection(host1); fail("An exception should have occurred"); } catch (Exception e) { // this is expected } }
private void resetConnection() { // Log.e("server", server); try { String ip = new URL(putUrl).getHost(); int port = new URL(putUrl).getPort(); mConnection = new HttpConnection(ip, port == -1 ? 80 : port); mConnection.open(); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
/** * Closes connections that have been idle for at least the given amount of time. * * @param idleTime the minimum idle time, in milliseconds, for connections to be closed */ public void closeIdleConnections(long idleTime) { // the latest time for which connections will be closed long idleTimeout = System.currentTimeMillis() - idleTime; if (LOG.isDebugEnabled()) { LOG.debug("Checking for connections, idleTimeout: " + idleTimeout); } Iterator connectionIter = connectionToAdded.keySet().iterator(); while (connectionIter.hasNext()) { HttpConnection conn = (HttpConnection) connectionIter.next(); Long connectionTime = (Long) connectionToAdded.get(conn); if (connectionTime.longValue() <= idleTimeout) { if (LOG.isDebugEnabled()) { LOG.debug("Closing connection, connection time: " + connectionTime); } connectionIter.remove(); conn.close(); } } }
public void testDeleteClosedConnections() { MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager(); HttpConnection conn = manager.getConnection(client.getHostConfiguration()); assertEquals("connectionsInPool", manager.getConnectionsInPool(), 1); assertEquals( "connectionsInPool(host)", manager.getConnectionsInPool(client.getHostConfiguration()), 1); conn.close(); conn.releaseConnection(); assertEquals("connectionsInPool", manager.getConnectionsInPool(), 1); assertEquals( "connectionsInPool(host)", manager.getConnectionsInPool(client.getHostConfiguration()), 1); manager.deleteClosedConnections(); assertEquals("connectionsInPool", manager.getConnectionsInPool(), 0); assertEquals( "connectionsInPool(host)", manager.getConnectionsInPool(client.getHostConfiguration()), 0); }
public HttpConnection getConnection(HostConfiguration hostConfiguration) { // make sure the host and proxy are correct for this connection // close it and set the values if they are not if (!hostConfiguration.hostEquals(connection) || !hostConfiguration.proxyEquals(connection)) { if (connection.isOpen()) { connection.close(); } connection.setHost(hostConfiguration.getHost()); connection.setPort(hostConfiguration.getPort()); connection.setProtocol(hostConfiguration.getProtocol()); connection.setLocalAddress(hostConfiguration.getLocalAddress()); connection.setProxyHost(hostConfiguration.getProxyHost()); connection.setProxyPort(hostConfiguration.getProxyPort()); } else { finishLastResponse(connection); } connectionReleased = false; return connection; }
/** @param connection */ public void setConnection(HttpConnection connection) { this.connection = connection; connection.setHttpConnectionManager(this); connection.getParams().setDefaults(this.params); }