Exemple #1
0
 /**
  * Prior to {@code Build()}, host, port, and endpoint must be specified.
  *
  * @return a {@link JobClient}.
  * @throws URISyntaxException
  */
 public JobClient build() throws URISyntaxException {
   // The definition of the following parameters are optional.
   if (_statusUpdateIntervalSeconds == null) {
     _statusUpdateIntervalSeconds = DEFAULT_STATUS_UPDATE_INTERVAL_SECONDS;
   }
   if (_batchRequestSize == null) {
     _batchRequestSize = DEFAULT_BATCH_REQUEST_SIZE;
   }
   if (_requestTimeoutSeconds == null) {
     _requestTimeoutSeconds = DEFAULT_REQUEST_TIMEOUT_SECONDS;
   }
   RequestConfig requestConfig =
       RequestConfig.custom()
           .setSocketTimeout(_requestTimeoutSeconds * 1000)
           .setConnectTimeout(_requestTimeoutSeconds * 1000)
           .setConnectionRequestTimeout(_requestTimeoutSeconds * 1000)
           .setStaleConnectionCheckEnabled(true)
           .build();
   _httpClientBuilder.setDefaultRequestConfig(requestConfig);
   _httpClientBuilder.setRetryHandler(new StandardHttpRequestRetryHandler());
   return new JobClient(
       Preconditions.checkNotNull(_host, "host must be set"),
       Preconditions.checkNotNull(_port, "port must be set"),
       Preconditions.checkNotNull(_endpoint, "endpoint must be set"),
       _statusUpdateIntervalSeconds,
       _batchRequestSize,
       _instanceDecorator,
       _httpClientBuilder.build());
 }
  public void afterPropertiesSet() {
    HttpClientBuilder httpClientBuilder = HttpClients.custom();

    httpClientBuilder.setConnectionManager(getPoolingHttpClientConnectionManager());

    if ((_login != null) && (_password != null)) {
      CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

      credentialsProvider.setCredentials(
          new AuthScope(_hostName, _hostPort), new UsernamePasswordCredentials(_login, _password));

      httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
      httpClientBuilder.setRetryHandler(new HttpRequestRetryHandlerImpl());
    } else {
      if (_logger.isWarnEnabled()) {
        _logger.warn("Login and password are required");
      }
    }

    try {
      setProxyHost(httpClientBuilder);

      _closeableHttpClient = httpClientBuilder.build();

      if (_logger.isDebugEnabled()) {
        _logger.debug("Configured client for " + _protocol + "://" + _hostName);
      }
    } catch (Exception e) {
      _logger.error("Unable to configure client", e);
    }
  }
  private CloseableHttpClient generateClient(Site site) {
    HttpClientBuilder httpClientBuilder =
        HttpClients.custom().setConnectionManager(connectionManager);
    if (site != null && site.getUserAgent() != null) {
      httpClientBuilder.setUserAgent(site.getUserAgent());
    } else {
      httpClientBuilder.setUserAgent("");
    }
    if (site == null || site.isUseGzip()) {
      httpClientBuilder.addInterceptorFirst(
          new HttpRequestInterceptor() {

            public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
              if (!request.containsHeader("Accept-Encoding")) {
                request.addHeader("Accept-Encoding", "gzip");
              }
            }
          });
    }
    SocketConfig socketConfig =
        SocketConfig.custom().setSoKeepAlive(true).setTcpNoDelay(true).build();
    httpClientBuilder.setDefaultSocketConfig(socketConfig);
    if (site != null) {
      httpClientBuilder.setRetryHandler(
          new DefaultHttpRequestRetryHandler(site.getRetryTimes(), true));
    }
    generateCookie(httpClientBuilder, site);
    return httpClientBuilder.build();
  }