Example #1
0
 public void init() throws Exception {
   client = new HttpClient();
   client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
   client.setThreadPool(new ExecutorThreadPool(4, 256, timeout, TimeUnit.SECONDS));
   client.setTimeout(timeout);
   client.start();
 }
Example #2
0
 /** Initialize the client object */
 public StockManagerHTTPProxy(String serverAddress) throws Exception {
   setServerAddress(serverAddress);
   client = new HttpClient();
   client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
   client.setMaxConnectionsPerAddress(
       BookStoreClientConstants.CLIENT_MAX_CONNECTION_ADDRESS); // max
   // concurrent
   // connections
   // to
   // every
   // address
   client.setThreadPool(
       new QueuedThreadPool(BookStoreClientConstants.CLIENT_MAX_THREADSPOOL_THREADS)); // max
   // threads
   client.setTimeout(BookStoreClientConstants.CLIENT_MAX_TIMEOUT_MILLISECS); // seconds
   // timeout;
   // if
   // no
   // server
   // reply,
   // the
   // request
   // expires
   client.start();
 }
Example #3
0
  private static HttpClient httpClient() throws Exception {
    if (_httpClient == null) {
      _httpClient = new HttpClient();
      _httpClient.setConnectorType(CONNECTOR_SELECT_CHANNEL);
      _httpClient.setConnectTimeout(TIMEOUT_CONNECT);
      _httpClient.start();
    }

    return _httpClient;
  }
  // TODO: client.start() could potentially have some side effects.
  // Need to understand more of what's going on under the hood.
  @Provides
  HttpClient provideHttpClient() {
    LOGGER.info("Providing http client for connection name " + connectionName);

    HttpClient client = new HttpClient();
    client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
    client.setConnectTimeout(timeoutOption);
    client.setTimeout(timeoutOption);

    return client;
  }
Example #5
0
  protected static void runTest(String requestUrl, long count) {
    HttpClient client = new HttpClient();
    client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
    try {
      client.start();
    } catch (Exception ex) {
      LOG.debug(ex);
    }

    if (client != null) {
      for (long cnt = 0; cnt < count; cnt++) {
        try {
          ContentExchange getExchange = new ContentExchange();
          getExchange.setURL(requestUrl);
          getExchange.setMethod(HttpMethods.GET);

          client.send(getExchange);
          int state = getExchange.waitForDone();

          String content = "";
          int responseStatus = getExchange.getResponseStatus();
          if (responseStatus == HttpStatus.OK_200) {
            content = getExchange.getResponseContent();
          }

          Thread.sleep(100);
        } catch (InterruptedException ex) {
          break;
        } catch (IOException ex) {
          LOG.debug(ex);
        }
      }

      try {
        client.stop();
      } catch (Exception ex) {
        LOG.debug(ex);
      }
    }
  }
 protected void startClient(Realm realm) throws Exception {
   _client = new HttpClient();
   _client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
   if (realm != null) _client.setRealmResolver(new SimpleRealmResolver(realm));
   _client.start();
 }
  @Test
  public void testLastAccessTime() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int maxInactivePeriod = 8;
    int scavengePeriod = 2;
    AbstractTestServer server1 = createServer(0, maxInactivePeriod, scavengePeriod);
    server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
    server1.start();
    int port1 = server1.getPort();
    try {
      AbstractTestServer server2 = createServer(0, maxInactivePeriod, scavengePeriod);
      server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
      server2.start();
      int port2 = server2.getPort();
      try {
        HttpClient client = new HttpClient();
        client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
        client.start();
        try {
          // Perform one request to server1 to create a session
          ContentExchange exchange1 = new ContentExchange(true);
          exchange1.setMethod(HttpMethods.GET);
          exchange1.setURL(
              "http://localhost:" + port1 + contextPath + servletMapping + "?action=init");
          client.send(exchange1);
          exchange1.waitForDone();
          assertEquals(HttpServletResponse.SC_OK, exchange1.getResponseStatus());
          assertEquals("test", exchange1.getResponseContent());
          String sessionCookie = exchange1.getResponseFields().getStringField("Set-Cookie");
          assertTrue(sessionCookie != null);
          // Mangle the cookie, replacing Path with $Path, etc.
          sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");

          // Perform some request to server2 using the session cookie from the previous request
          // This should migrate the session from server1 to server2, and leave server1's
          // session in a very stale state, while server2 has a very fresh session.
          // We want to test that optimizations done to the saving of the shared lastAccessTime
          // do not break the correct working
          int requestInterval = 500;
          for (int i = 0; i < maxInactivePeriod * (1000 / requestInterval); ++i) {
            ContentExchange exchange2 = new ContentExchange(true);
            exchange2.setMethod(HttpMethods.GET);
            exchange2.setURL("http://localhost:" + port2 + contextPath + servletMapping);
            exchange2.getRequestFields().add("Cookie", sessionCookie);
            client.send(exchange2);
            exchange2.waitForDone();
            assertEquals(HttpServletResponse.SC_OK, exchange2.getResponseStatus());
            assertEquals("test", exchange2.getResponseContent());

            String setCookie = exchange1.getResponseFields().getStringField("Set-Cookie");
            if (setCookie != null)
              sessionCookie = setCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");

            Thread.sleep(requestInterval);
          }

          // At this point, session1 should be eligible for expiration.
          // Let's wait for the scavenger to run, waiting 2.5 times the scavenger period
          Thread.sleep(scavengePeriod * 2500L);

          // Access again server1, and ensure that we can still access the session
          exchange1 = new ContentExchange(true);
          exchange1.setMethod(HttpMethods.GET);
          exchange1.setURL("http://localhost:" + port1 + contextPath + servletMapping);
          exchange1.getRequestFields().add("Cookie", sessionCookie);
          client.send(exchange1);
          exchange1.waitForDone();
          assertEquals(HttpServletResponse.SC_OK, exchange1.getResponseStatus());
          // test that the session was kept alive by server 2 and still contains what server1 put in
          // it
          assertEquals("test", exchange1.getResponseContent());

        } finally {
          client.stop();
        }
      } finally {
        server2.stop();
      }
    } finally {
      server1.stop();
    }
  }