コード例 #1
0
  /**
   * 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);
    }
  }
コード例 #2
0
  /** 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);
    }
  }
コード例 #3
0
  public void testWriteRequestReleaseConnection() {

    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.getParams().setDefaultMaxConnectionsPerHost(1);

    client.setHttpConnectionManager(connectionManager);

    GetMethod get =
        new GetMethod("/") {
          protected boolean writeRequestBody(HttpState state, HttpConnection conn)
              throws IOException, HttpException {
            throw new IOException("Oh no!!");
          }
        };

    try {
      client.executeMethod(get);
      fail("An exception should have occurred.");
    } catch (HttpException e) {
      e.printStackTrace();
      fail("HttpException should not have occurred: " + e);
    } catch (IOException e) {
      // expected
    }

    try {
      connectionManager.getConnectionWithTimeout(client.getHostConfiguration(), 1);
    } catch (ConnectTimeoutException e) {
      e.printStackTrace();
      fail("Connection was not released: " + e);
    }
  }
コード例 #4
0
  /** Makes sure that a connection gets released after the content of the body is read. */
  public void testResponseAutoRelease() throws Exception {

    this.server.setHttpService(new EchoService());

    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.getParams().setDefaultMaxConnectionsPerHost(1);

    client.setHttpConnectionManager(connectionManager);
    // we shouldn't have to wait if a connection is available
    client.getParams().setConnectionManagerTimeout(1);

    GetMethod getMethod = new GetMethod("/");

    try {
      client.executeMethod(getMethod);
    } catch (Exception e) {
      fail("error reading from server: " + e);
    }

    // this should release the connection
    getMethod.getResponseBody();

    getMethod = new GetMethod("/");

    try {
      // this should fail quickly if the connection has not been released
      client.executeMethod(getMethod);
    } catch (HttpException e) {
      fail("httpConnection does not appear to have been released: " + e);
    } catch (IOException e) {
      fail("error reading from server; " + e);
    }
  }
コード例 #5
0
  public void testMaxConnectionsPerServer() {

    this.server.setHttpService(new EchoService());

    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.getParams().setDefaultMaxConnectionsPerHost(1);

    client.setHttpConnectionManager(connectionManager);
    // we shouldn't have to wait if a connection is available
    client.getParams().setConnectionManagerTimeout(1);

    GetMethod getMethod = new GetMethod("/");

    try {
      client.executeMethod(getMethod);
    } catch (Exception e) {
      fail("error reading from server: " + e);
    }

    GetMethod getMethod2 = new GetMethod("/");

    try {
      // this should fail quickly since the connection has not been released
      client.executeMethod(getMethod2);
      fail("a httpConnection should not be available");
    } catch (ConnectTimeoutException e) {
    } catch (HttpException e) {
      fail("error reading from server; " + e);
    } catch (IOException e) {
      fail("error reading from server; " + e);
    }
  }
コード例 #6
0
  public void testReclaimUnusedConnection() {

    this.server.setHttpService(new EchoService());

    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager
        .getParams()
        .setIntParameter(HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 1);

    client.setHttpConnectionManager(connectionManager);
    // we shouldn't have to wait if a connection is available
    client.getParams().setConnectionManagerTimeout(30000);

    GetMethod getMethod = new GetMethod("/");

    try {
      client.executeMethod(getMethod);
    } catch (Exception e) {
      fail("error reading from server: " + e);
    }

    getMethod = new GetMethod("/");

    Runtime.getRuntime().gc();

    try {
      // we didn't explicitly release the connection, but it should be
      // reclaimed by the garbage collector, we hope:)
      client.executeMethod(getMethod);
    } catch (HttpException e) {
      fail("httpConnection does not appear to have been reclaimed by the GC: " + e);
    } catch (IOException e) {
      fail("error reading from server; " + e);
    }
  }
コード例 #7
0
  /**
   * Tests the MultiThreadedHttpConnectionManager's ability to restrict the maximum number of
   * connections per host.
   */
  public void testMaxConnectionsPerHost() throws Exception {

    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.getParams().setDefaultMaxConnectionsPerHost(1);
    connectionManager.getParams().setMaxTotalConnections(100);

    HostConfiguration host1 = new HostConfiguration();
    host1.setHost("host1", -1, "http");

    HostConfiguration host2 = new HostConfiguration();
    host2.setHost("host2", -1, "http");

    HostConfiguration host3 = new HostConfiguration();
    host3.setHost("host3", -1, "http");

    connectionManager.getParams().setMaxConnectionsPerHost(host1, 3);
    connectionManager.getParams().setMaxConnectionsPerHost(host2, 2);

    // Host1
    HttpConnection connection1 = connectionManager.getConnectionWithTimeout(host1, 1000);
    HttpConnection connection2 = connectionManager.getConnectionWithTimeout(host1, 1000);
    HttpConnection connection3 = connectionManager.getConnectionWithTimeout(host1, 1000);
    try {
      // this should fail quickly since the connection has not been released
      connectionManager.getConnectionWithTimeout(host1, 100);
      fail("ConnectionPoolTimeoutException should not be available");
    } catch (ConnectionPoolTimeoutException e) {
      // expected
    }

    // Host2
    connection1 = connectionManager.getConnectionWithTimeout(host2, 1000);
    connection2 = connectionManager.getConnectionWithTimeout(host2, 1000);
    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) {
      // expected
    }

    // Host3 (should use the default per host value)
    connection1 = connectionManager.getConnectionWithTimeout(host3, 1000);
    try {
      // this should fail quickly since the connection has not been released
      connectionManager.getConnectionWithTimeout(host3, 100);
      fail("ConnectionPoolTimeoutException should not be available");
    } catch (ConnectionPoolTimeoutException e) {
      // expected
    }
  }
コード例 #8
0
 public void run() {
   try {
     connection = connectionManager.getConnectionWithTimeout(hostConfiguration, timeout);
   } catch (Exception e) {
     this.exception = e;
   }
 }
コード例 #9
0
  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());
  }
コード例 #10
0
  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);
  }
コード例 #11
0
  public void testTimeout() {
    MultiThreadedHttpConnectionManager mgr = new MultiThreadedHttpConnectionManager();
    mgr.getParams().setDefaultMaxConnectionsPerHost(2);

    try {
      HostConfiguration hostConfig = new HostConfiguration();
      hostConfig.setHost("www.nosuchserver.com", 80, "http");

      HttpConnection conn1 = mgr.getConnection(hostConfig);
      HttpConnection conn2 = mgr.getConnection(hostConfig);

      HttpConnection conn3 = mgr.getConnectionWithTimeout(hostConfig, 1000);
      fail("Expected an HttpException.");

    } catch (ConnectTimeoutException e) {
      // Expected result
    }
  }
コード例 #12
0
ファイル: HTTPSession.java プロジェクト: feihugis/NetCDF
 private static synchronized void kill() {
   if (sessionList != null) {
     for (HTTPSession session : sessionList) {
       session.close();
     }
     sessionList.clear();
     // Rebuild the connection manager
     connmgr.shutdown();
     connmgr = new MultiThreadedHttpConnectionManager();
     setGlobalThreadCount(DFALTTHREADCOUNT);
   }
 }
コード例 #13
0
  /**
   * 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
    }
  }
コード例 #14
0
  protected void initHttpClient() {
    if (MockServer.isTestMode()) {
      return;
    }
    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost(
        diamondConfigure.getDomainNameList().get(this.domainNamePos.get()),
        diamondConfigure.getPort());

    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.closeIdleConnections(diamondConfigure.getPollingIntervalTime() * 10);

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setStaleCheckingEnabled(diamondConfigure.isConnectionStaleCheckingEnabled());
    params.setMaxConnectionsPerHost(hostConfiguration, diamondConfigure.getMaxHostConnections());
    params.setMaxTotalConnections(diamondConfigure.getMaxTotalConnections());
    params.setConnectionTimeout(diamondConfigure.getConnectionTimeout());
    params.setSoTimeout(60 * 1000);

    connectionManager.setParams(params);
    httpClient = new HttpClient(connectionManager);
    httpClient.setHostConfiguration(hostConfiguration);
  }
コード例 #15
0
  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);
  }
コード例 #16
0
ファイル: HTTPSession.java プロジェクト: feihugis/NetCDF
 public static void setGlobalThreadCount(int nthreads) {
   connmgr.getParams().setMaxTotalConnections(nthreads);
   connmgr.getParams().setDefaultMaxConnectionsPerHost(nthreads);
 }
コード例 #17
0
  /** Test that the ConnectMethod correctly releases connections when CONNECT fails. */
  public void testConnectMethodFailureRelease() throws Exception {

    MultiThreadedHttpConnectionManager mgr = new MultiThreadedHttpConnectionManager();
    mgr.getParams().setIntParameter(HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 1);
    client.setHttpConnectionManager(mgr);
    this.server.setHttpService(new RejectConnectService());

    // we're going to execute a connect method against the localhost, assuming
    // that CONNECT is not supported.  This should test the fakeResponse()
    // code on HttpMethodBase.
    client.getHostConfiguration().setProxy(server.getLocalAddress(), server.getLocalPort());
    // we must set the host to a secure destination or the CONNECT method
    // will not be used
    client
        .getHostConfiguration()
        .setHost(
            "notARealHost",
            1234,
            new Protocol(
                "https", (ProtocolSocketFactory) new FakeSecureProtocolSocketFactory(), 443));

    GetMethod get = new GetMethod("/");
    try {
      assertTrue(client.executeMethod(get) != 200);
    } catch (IOException e) {
      e.printStackTrace();
      fail("Error executing connect: " + e);
    }

    // this should calling releaseConnection() releases the connection
    try {
      get.releaseConnection();
      mgr.getConnectionWithTimeout(client.getHostConfiguration(), 1).releaseConnection();
    } catch (ConnectTimeoutException e1) {
      fail("Connection should have been available.");
    }

    get = new GetMethod("/");

    try {
      assertTrue(client.executeMethod(get) != 200);
    } catch (IOException e) {
      e.printStackTrace();
      fail("Error executing connect: " + e);
    }

    // make sure reading the response fully releases the connection
    try {
      get.getResponseBodyAsString();
      mgr.getConnectionWithTimeout(client.getHostConfiguration(), 1).releaseConnection();
    } catch (ConnectTimeoutException e1) {
      fail("Connection should have been available.");
    }

    get = new GetMethod("/");

    try {
      assertTrue(client.executeMethod(get) != 200);
    } catch (IOException e) {
      e.printStackTrace();
      fail("Error executing connect: " + e);
    }

    // make sure closing the output stream releases the connection
    try {
      get.getResponseBodyAsStream().close();
      mgr.getConnectionWithTimeout(client.getHostConfiguration(), 1).releaseConnection();
    } catch (ConnectTimeoutException e) {
      fail("Connection should have been available.");
    } catch (IOException e) {
      e.printStackTrace();
      fail("Close connection failed: " + e);
    }
  }
コード例 #18
0
ファイル: HTTPSession.java プロジェクト: feihugis/NetCDF
 public static void setGlobalConnectionTimeout(int timeout) {
   connmgr.getParams().setConnectionTimeout(timeout);
 }
コード例 #19
0
  {
    String useMultiThreadedConnectionMgr = LPS.getProperty("http.useConnectionPool", "true");

    if (Boolean.valueOf(useMultiThreadedConnectionMgr).booleanValue()) {
      mLogger.info(
          /* (non-Javadoc)
           * @i18n.test
           * @org-mes="using connection pool"
           */
          org.openlaszlo.i18n.LaszloMessages.getMessage(
              HTTPDataSource.class.getName(), "051018-81"));
      mConnectionMgr = new MultiThreadedHttpConnectionManager();
    } else {
      mLogger.info(
          /* (non-Javadoc)
           * @i18n.test
           * @org-mes="not using connection pool"
           */
          org.openlaszlo.i18n.LaszloMessages.getMessage(
              HTTPDataSource.class.getName(), "051018-91"));
    }

    // Parse multi connection properties anyway. May be used by AXIS. See
    // ResponderCache for details.
    {
      String maxConns = LPS.getProperty("http.maxConns", "1000");
      mMaxTotalConnections = Integer.parseInt(maxConns);
      if (mConnectionMgr != null) {
        mConnectionMgr.setMaxTotalConnections(mMaxTotalConnections);
      }

      maxConns = LPS.getProperty("http.maxConnsPerHost", maxConns);
      mMaxConnectionsPerHost = Integer.parseInt(maxConns);
      if (mConnectionMgr != null) {
        mConnectionMgr.setMaxConnectionsPerHost(mMaxConnectionsPerHost);
      }
    }

    String maxRetries = LPS.getProperty("http.maxBackendRetries", "1");
    mMaxRetries = Integer.parseInt(maxRetries);

    String followRedirects = LPS.getProperty("http.followRedirects", "0");
    mFollowRedirects = Integer.parseInt(followRedirects);

    String timeout = LPS.getProperty("http.backendTimeout", "30000");
    mTimeout = Integer.parseInt(timeout);

    timeout = LPS.getProperty("http.backendConnectionTimeout", timeout);
    mConnectionTimeout = Integer.parseInt(timeout);

    timeout = LPS.getProperty("http.connectionPoolTimeout", "0");
    mConnectionPoolTimeout = Integer.parseInt(timeout);

    String useHttp11 = LPS.getProperty("http.useHttp11", "true");
    mUseHttp11 = Boolean.valueOf(useHttp11).booleanValue();
    if (mUseHttp11) {
      mLogger.info("using HTTP 1.1");
    } else {
      mLogger.info("not using HTTP 1.1");
    }
  }
コード例 #20
0
ファイル: HTTPSession.java プロジェクト: feihugis/NetCDF
 public static int getGlobalThreadCount() {
   return connmgr.getParams().getMaxTotalConnections();
 }