org.apache.http.impl.client.DefaultBackoffStrategy Java Examples

The following examples show how to use org.apache.http.impl.client.DefaultBackoffStrategy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpRoutingDataReader.java    From helix with Apache License 2.0 6 votes vote down vote up
/**
 * Makes an HTTP call to fetch all routing data.
 * @return
 * @throws IOException
 */
private static String getAllRoutingData(String msdsEndpoint) throws IOException {
  // Note that MSDS_ENDPOINT should provide high-availability - it risks becoming a single point
  // of failure if it's backed by a single IP address/host
  // Retry count is 3 by default.
  HttpGet requestAllData = new HttpGet(
      msdsEndpoint + MetadataStoreRoutingConstants.MSDS_GET_ALL_ROUTING_DATA_ENDPOINT);

  // Define timeout configs
  RequestConfig config = RequestConfig.custom().setConnectTimeout(HTTP_TIMEOUT_IN_MS)
      .setConnectionRequestTimeout(HTTP_TIMEOUT_IN_MS).setSocketTimeout(HTTP_TIMEOUT_IN_MS)
      .build();

  try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(config)
      .setConnectionBackoffStrategy(new DefaultBackoffStrategy())
      .setRetryHandler(new DefaultHttpRequestRetryHandler()).build()) {
    // Return the JSON because try-resources clause closes the CloseableHttpResponse
    HttpEntity entity = httpClient.execute(requestAllData).getEntity();
    if (entity == null) {
      throw new IOException("Response's entity is null!");
    }
    return EntityUtils.toString(entity, "UTF-8");
  }
}
 
Example #2
Source File: SnowizardClient.java    From snowizard with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get a new CloseableHttpClient
 * 
 * @return CloseableHttpClient
 */
public static CloseableHttpClient newHttpClient() {
    final SocketConfig socketConfig = SocketConfig.custom()
            .setSoKeepAlive(Boolean.TRUE).setTcpNoDelay(Boolean.TRUE)
            .setSoTimeout(SOCKET_TIMEOUT_MS).build();

    final PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager();
    manager.setDefaultMaxPerRoute(MAX_HOSTS);
    manager.setMaxTotal(MAX_HOSTS);
    manager.setDefaultSocketConfig(socketConfig);

    final RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(CONNECTION_TIMEOUT_MS)
            .setCookieSpec(CookieSpecs.IGNORE_COOKIES)
            .setStaleConnectionCheckEnabled(Boolean.FALSE)
            .setSocketTimeout(SOCKET_TIMEOUT_MS).build();

    final CloseableHttpClient client = HttpClients
            .custom()
            .disableRedirectHandling()
            .setConnectionManager(manager)
            .setDefaultRequestConfig(requestConfig)
            .setConnectionReuseStrategy(
                    new DefaultConnectionReuseStrategy())
            .setConnectionBackoffStrategy(new DefaultBackoffStrategy())
            .setRetryHandler(
                    new DefaultHttpRequestRetryHandler(MAX_RETRIES, false))
            .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())
            .build();
    return client;
}