Ejemplo n.º 1
0
  /** Set the connection timeout and socket timeout */
  public static void setTimeouts(long connectionTimeout, long socketTimeout) {
    Options.setOption(Option.CONNECTION_TIMEOUT, connectionTimeout);
    Options.setOption(Option.SOCKET_TIMEOUT, socketTimeout);

    // Reload the client implementations
    Options.refresh();
  }
Ejemplo n.º 2
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);
  }
Ejemplo n.º 3
0
 /** Set default header */
 @SuppressWarnings("unchecked")
 public static void setDefaultHeader(String name, String value) {
   Object headers = Options.getOption(Option.DEFAULT_HEADERS);
   if (headers == null) {
     headers = new HashMap<String, String>();
   }
   ((Map<String, String>) headers).put(name, value);
   Options.setOption(Option.DEFAULT_HEADERS, headers);
 }
Ejemplo n.º 4
0
  /**
   * Close the asynchronous client and its event loop. Use this method to close all the threads and
   * allow an application to exit.
   */
  public static void shutdown() throws IOException {
    // Closing the sync client
    CloseableHttpClient syncClient = (CloseableHttpClient) Options.getOption(Option.HTTPCLIENT);
    syncClient.close();

    SyncIdleConnectionMonitorThread syncIdleConnectionMonitorThread =
        (SyncIdleConnectionMonitorThread) Options.getOption(Option.SYNC_MONITOR);
    syncIdleConnectionMonitorThread.shutdown();

    // Closing the async client (if running)
    CloseableHttpAsyncClient asyncClient =
        (CloseableHttpAsyncClient) Options.getOption(Option.ASYNCHTTPCLIENT);
    if (asyncClient.isRunning()) {
      asyncClient.close();
      AsyncIdleConnectionMonitorThread asyncIdleConnectionMonitorThread =
          (AsyncIdleConnectionMonitorThread) Options.getOption(Option.ASYNC_MONITOR);
      asyncIdleConnectionMonitorThread.shutdown();
    }
  }
Ejemplo n.º 5
0
 /**
  * Set the asynchronous AbstractHttpAsyncClient implementation to use for every asynchronous
  * request
  */
 public static void setAsyncHttpClient(CloseableHttpAsyncClient asyncHttpClient) {
   Options.setOption(Option.ASYNCHTTPCLIENT, asyncHttpClient);
 }
Ejemplo n.º 6
0
 /** Clear default headers */
 public static void clearDefaultHeaders() {
   Options.setOption(Option.DEFAULT_HEADERS, null);
 }
Ejemplo n.º 7
0
 /** Set the HttpClient implementation to use for every synchronous request */
 public static void setHttpClient(HttpClient httpClient) {
   Options.setOption(Option.HTTPCLIENT, httpClient);
 }