@Override
 public void releaseConnection(
     final HttpClientConnection conn,
     final Object newState,
     final long validDuration,
     final TimeUnit timeUnit) {
   connectionManager.releaseConnection(conn, newState, validDuration, timeUnit);
 }
예제 #2
0
  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;
      }
    }
  }