private Future<HttpResponse> checkForUpdates() {
   CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
   httpclient.start();
   HttpHead request = new HttpHead("http://broken-by.me/download/pdf-metadata-editor/");
   Future<HttpResponse> future = httpclient.execute(request, null);
   return future;
 }
  public static void main(final String[] args) throws Exception {

    System.out.println("-------------------------------------------------------------------");
    System.out.println("java.version: " + System.getProperty("java.version"));
    System.out.println("java.vendor.url: " + System.getProperty("java.vendor.url"));
    System.out.println("java.vendor: " + System.getProperty("java.vendor"));
    System.out.println("java.home: " + System.getProperty("java.home"));
    System.out.println("os.arch: " + System.getProperty("os.arch"));
    System.out.println("os.name: " + System.getProperty("os.name"));
    System.out.println("os.version: " + System.getProperty("os.version"));
    System.out.println("-------------------------------------------------------------------");

    CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
    httpclient.start();
    try {
      long start = System.currentTimeMillis();
      for (int i = 0; i < 10; i++) {
        Future<Boolean> future =
            httpclient.execute(
                HttpAsyncMethods.createGet("http://www.google.com/"),
                new MyResponseConsumer(),
                null);
        Boolean result = future.get();
        System.out.println("result: " + result);
        if (!result) {
          System.out.println("Request failed");
        }
      }
      long end = System.currentTimeMillis();
      System.out.println("Total time (sec):" + ((double) (end - start)) / 1000);
    } finally {
      httpclient.close();
    }
    System.out.println("Done");
  }
Пример #3
0
  public static void refresh() {
    // Load timeouts
    Object connectionTimeout = Options.getOption(Option.CONNECTION_TIMEOUT);
    if (connectionTimeout == null) connectionTimeout = CONNECTION_TIMEOUT;
    Object socketTimeout = Options.getOption(Option.SOCKET_TIMEOUT);
    if (socketTimeout == null) socketTimeout = SOCKET_TIMEOUT;

    // Create common default configuration
    RequestConfig clientConfig =
        RequestConfig.custom()
            .setConnectTimeout(((Long) connectionTimeout).intValue())
            .setSocketTimeout(((Long) socketTimeout).intValue())
            .build();

    // Create clients
    setOption(
        Option.HTTPCLIENT,
        HttpClientBuilder.create().setDefaultRequestConfig(clientConfig).build());

    CloseableHttpAsyncClient asyncClient =
        HttpAsyncClientBuilder.create().setDefaultRequestConfig(clientConfig).build();
    asyncClient.start();

    setOption(Option.ASYNCHTTPCLIENT, asyncClient);
  }
Пример #4
0
  public Object get(
      String url, AbstractAsyncResponseConsumer myConsumer, Map<String, String> headerMap)
      throws IOException {
    URI uri = URI.create(url);
    CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
    Object obj = null;
    try {
      httpclient.start();
      HttpGet httpget = new HttpGet(uri);
      HttpHost httphost = URIUtils.extractHost(uri);
      if (headerMap != null) {
        for (String key : headerMap.keySet()) {
          httpget.addHeader(key, headerMap.get(key));
        }
      }
      BasicAsyncRequestProducer requset = new BasicAsyncRequestProducer(httphost, httpget);

      Future<Object> future = httpclient.execute(requset, myConsumer, null);
      obj = future.get();
    } catch (InterruptedException e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    } catch (ExecutionException e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    } finally {
      httpclient.close();
      return obj;
    }
  }
Пример #5
0
  public Object post(String url, AbstractAsyncResponseConsumer myConsumer, String json)
      throws IOException {
    CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
    Object obj = null;
    try {
      httpclient.start();

      Future<Object> future =
          httpclient.execute(
              HttpAsyncMethods.createPost(url, json, ContentType.APPLICATION_JSON),
              myConsumer,
              null);
      obj = future.get();
    } catch (InterruptedException e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    } catch (ExecutionException e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    } finally {
      httpclient.close();
      return obj;
    }
  }
Пример #6
0
 static CloseableHttpAsyncClient buildDefaultHttpClient() {
   // TODO: Increase timeout??
   RequestConfig requestConfig = RequestConfig.custom().build();
   CloseableHttpAsyncClient httpClient =
       HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig).build();
   httpClient.start();
   return httpClient;
 }
  public static CloseableHttpAsyncClient getHttpAsyncClient() {
    if (httpAsyncClient == null) {
      synchronized (HttpClientManager.class) {
        if (httpAsyncClient == null) {
          CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
          client.start();
          httpAsyncClient = client;
        }
      }
    }

    return httpAsyncClient;
  }
Пример #8
0
  static {
    RequestConfig clientConfig =
        RequestConfig.custom()
            .setConnectTimeout(CONNECTION_TIMEOUT)
            .setSocketTimeout(SOCKET_TIMEOUT)
            .build();

    setOption(
        Option.HTTPCLIENT,
        HttpClientBuilder.create().setDefaultRequestConfig(clientConfig).build());

    CloseableHttpAsyncClient asyncClient =
        HttpAsyncClientBuilder.create().setDefaultRequestConfig(clientConfig).build();
    asyncClient.start();

    setOption(Option.ASYNCHTTPCLIENT, asyncClient);
  }
  @Override
  public void init(Object obj) {
    config = (HECTransportConfig) obj;

    this.batchBuffer = Collections.synchronizedList(new LinkedList<String>());
    this.lastEventReceivedTime = System.currentTimeMillis();

    try {

      Registry<SchemeIOSessionStrategy> sslSessionStrategy =
          RegistryBuilder.<SchemeIOSessionStrategy>create()
              .register("http", NoopIOSessionStrategy.INSTANCE)
              .register("https", new SSLIOSessionStrategy(getSSLContext(), HOSTNAME_VERIFIER))
              .build();

      ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
      PoolingNHttpClientConnectionManager cm =
          new PoolingNHttpClientConnectionManager(ioReactor, sslSessionStrategy);
      cm.setMaxTotal(config.getPoolsize());

      HttpHost splunk = new HttpHost(config.getHost(), config.getPort());
      cm.setMaxPerRoute(new HttpRoute(splunk), config.getPoolsize());

      httpClient = HttpAsyncClients.custom().setConnectionManager(cm).build();

      uri =
          new URIBuilder()
              .setScheme(config.isHttps() ? "https" : "http")
              .setHost(config.getHost())
              .setPort(config.getPort())
              .setPath("/services/collector")
              .build();

      httpClient.start();

      if (config.isBatchMode()) {
        new BatchBufferActivityCheckerThread().start();
      }

    } catch (Exception e) {
      logger.error("Error initialising HEC Transport: " + ModularInput.getStackTrace(e));
    }
  }
 public static void main(final String[] args) throws Exception {
   CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
   try {
     httpclient.start();
     Future<Boolean> future =
         httpclient.execute(
             HttpAsyncMethods.createGet("http://www.sina.com.cn/"),
             new MyResponseConsumer(),
             null);
     Boolean result = future.get();
     if (result != null && result.booleanValue()) {
       System.out.println("Request successfully executed");
     } else {
       System.out.println("Request failed");
     }
     System.out.println("Shutting down");
   } finally {
     httpclient.close();
   }
   System.out.println("Done");
 }