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

The following examples show how to use org.apache.http.impl.client.StandardHttpRequestRetryHandler. 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: HttpClient4Utils.java    From ZTuoExchange_framework with MIT License 8 votes vote down vote up
/**
 * 实例化HttpClient
 *
 * @param maxTotal
 * @param maxPerRoute
 * @param socketTimeout
 * @param connectTimeout
 * @param connectionRequestTimeout
 * @return
 */
public static HttpClient createHttpClient(int maxTotal, int maxPerRoute, int socketTimeout, int connectTimeout,
                                          int connectionRequestTimeout) {
    RequestConfig defaultRequestConfig = RequestConfig.custom()
            .setSocketTimeout(socketTimeout)
            .setConnectTimeout(connectTimeout)
            .setConnectionRequestTimeout(connectionRequestTimeout).build();

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(maxTotal);
    cm.setDefaultMaxPerRoute(maxPerRoute);
    cm.setValidateAfterInactivity(200); // 一个连接idle超过200ms,再次被使用之前,需要先做validation
    CloseableHttpClient httpClient = HttpClients.custom()
            .setConnectionManager(cm)
            .setConnectionTimeToLive(30, TimeUnit.SECONDS)
            .setRetryHandler(new StandardHttpRequestRetryHandler(3, true)) // 配置出错重试
            .setDefaultRequestConfig(defaultRequestConfig).build();

    startMonitorThread(cm);

    return httpClient;
}
 
Example #2
Source File: HttpClient4Utils.java    From ZTuoExchange_framework with MIT License 6 votes vote down vote up
/**
 * 实例化HttpClient
 *
 * @param maxTotal
 * @param maxPerRoute
 * @param socketTimeout
 * @param connectTimeout
 * @param connectionRequestTimeout
 * @return
 */
public static HttpClient createHttpClient(int maxTotal, int maxPerRoute, int socketTimeout, int connectTimeout,
                                          int connectionRequestTimeout) {
    RequestConfig defaultRequestConfig = RequestConfig.custom()
            .setSocketTimeout(socketTimeout)
            .setConnectTimeout(connectTimeout)
            .setConnectionRequestTimeout(connectionRequestTimeout).build();

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(maxTotal);
    cm.setDefaultMaxPerRoute(maxPerRoute);
    cm.setValidateAfterInactivity(200); // 一个连接idle超过200ms,再次被使用之前,需要先做validation
    CloseableHttpClient httpClient = HttpClients.custom()
            .setConnectionManager(cm)
            .setConnectionTimeToLive(30, TimeUnit.SECONDS)
            .setRetryHandler(new StandardHttpRequestRetryHandler(3, true)) // 配置出错重试
            .setDefaultRequestConfig(defaultRequestConfig).build();

    startMonitorThread(cm);

    return httpClient;
}
 
Example #3
Source File: HttpClientHelper.java    From cosmic with Apache License 2.0 6 votes vote down vote up
public static CloseableHttpClient createHttpClient(final int maxRedirects) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
    s_logger.info("Creating new HTTP connection pool and client");
    final Registry<ConnectionSocketFactory> socketFactoryRegistry = createSocketFactoryConfigration();
    final BasicCookieStore cookieStore = new BasicCookieStore();
    final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
    connManager.setDefaultMaxPerRoute(MAX_ALLOCATED_CONNECTIONS_PER_ROUTE);
    connManager.setMaxTotal(MAX_ALLOCATED_CONNECTIONS);
    final RequestConfig requestConfig = RequestConfig.custom()
                                                     .setCookieSpec(CookieSpecs.DEFAULT)
                                                     .setMaxRedirects(maxRedirects)
                                                     .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT)
                                                     .setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT)
                                                     .setConnectTimeout(DEFAULT_CONNECT_TIMEOUT)
                                                     .build();
    return HttpClientBuilder.create()
                            .setConnectionManager(connManager)
                            .setRedirectStrategy(new LaxRedirectStrategy())
                            .setDefaultRequestConfig(requestConfig)
                            .setDefaultCookieStore(cookieStore)
                            .setRetryHandler(new StandardHttpRequestRetryHandler())
                            .build();
}
 
Example #4
Source File: DefaultsCustomizer.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void customize(final HttpClientPlan plan) {
  checkNotNull(plan);

  plan.setUserAgentBase(userAgentGenerator.generate());

  plan.getClient().setKeepAliveStrategy(new NexusConnectionKeepAliveStrategy(keepAliveDuration.toMillis()));
  plan.getClient().setRetryHandler(new StandardHttpRequestRetryHandler(2, false));

  plan.getConnection().setBufferSize(bufferSize.toBytesI());

  plan.getRequest().setConnectionRequestTimeout(connectionRequestTimeout.toMillisI());
  plan.getRequest().setCookieSpec(CookieSpecs.IGNORE_COOKIES);
  plan.getRequest().setExpectContinueEnabled(false);

  int requestTimeoutMillis = requestTimeout.toMillisI();
  plan.getSocket().setSoTimeout(requestTimeoutMillis);
  plan.getRequest().setConnectTimeout(requestTimeoutMillis);
  plan.getRequest().setSocketTimeout(requestTimeoutMillis);
}
 
Example #5
Source File: HttpManagementInterface.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static CloseableHttpClient createHttpClient(String host, int port, String username, String password) {
    SSLContext sslContext = org.apache.http.ssl.SSLContexts.createDefault();
                SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);

    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .register("https", sslConnectionSocketFactory)
                    .build();
    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(host, port, MANAGEMENT_REALM, AuthSchemes.DIGEST),
                    new UsernamePasswordCredentials(username, password));

    return HttpClientBuilder.create()
                    .setConnectionManager(new PoolingHttpClientConnectionManager(registry))
                    .setRetryHandler(new StandardHttpRequestRetryHandler(5, true))
                    .setDefaultCredentialsProvider(credentialsProvider)
                    .build();
}
 
Example #6
Source File: HttpGenericOperationUnitTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static CloseableHttpClient createHttpClient(String host, int port, String username, String password) {
    try {
        SSLContext sslContext = SSLContexts.createDefault();
        SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("https", sslConnectionSocketFactory)
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .build();
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(host, port, MANAGEMENT_REALM, AuthSchemes.DIGEST),
                new UsernamePasswordCredentials(username, password));
        PoolingHttpClientConnectionManager connectionPool = new PoolingHttpClientConnectionManager(registry);
        HttpClientBuilder.create().setConnectionManager(connectionPool).build();
        return HttpClientBuilder.create()
                .setConnectionManager(connectionPool)
                .setRetryHandler(new StandardHttpRequestRetryHandler(5, true))
                .setDefaultCredentialsProvider(credsProvider).build();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #7
Source File: AbstractDatabricksRestClientImpl.java    From databricks-rest-client with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a rest client.
 * @param host databricks host
 * @param apiVersion databricks api version
 * @param maxRetry how many retries
 * @param retryInterval interval between retries
 * @param requestSentRetryEnabled from the docs in DefaultHttpRequestRetryHandler:
 *                                Whether or not methods that have successfully sent their request will be retried
 */
public AbstractDatabricksRestClientImpl(
    String host,
    String apiVersion,
    int maxRetry,
    long retryInterval,
    boolean requestSentRetryEnabled
) {
  this.host = host;
  this.apiVersion = apiVersion;
  this.retryHandler = new StandardHttpRequestRetryHandler(maxRetry, requestSentRetryEnabled);
  this.retryStrategy = new HttpServiceUnavailableRetryStrategy(maxRetry, retryInterval);
}
 
Example #8
Source File: HttpEndpoint.java    From Brutusin-RPC with Apache License 2.0 5 votes vote down vote up
public HttpEndpoint(URI endpoint, Config cfg, HttpClientContextFactory clientContextFactory) {
    if (endpoint == null) {
        throw new IllegalArgumentException("Endpoint is required");
    }
    if (cfg == null) {
        cfg = new ConfigurationBuilder().build();
    }
    CacheConfig cacheConfig = CacheConfig.custom()
            .setMaxCacheEntries(cfg.getMaxCacheEntries())
            .setMaxObjectSize(cfg.getMaxCacheObjectSize())
            .build();
    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(1000 * cfg.getConnectTimeOutSeconds())
            .setSocketTimeout(1000 * cfg.getSocketTimeOutSeconds())
            .build();

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(cfg.getMaxConections());

    this.endpoint = endpoint;
    this.httpClient = CachingHttpClients.custom()
            .setCacheConfig(cacheConfig)
            .setDefaultRequestConfig(requestConfig)
            .setRetryHandler(new StandardHttpRequestRetryHandler())
            .setConnectionManager(cm)
            .build();
    this.clientContextFactory = clientContextFactory;
    initPingThread(cfg.getPingSeconds());
}
 
Example #9
Source File: ConfigurationCustomizer.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Apply connection configuration to plan.
 */
private void apply(final ConnectionConfiguration connection, final HttpClientPlan plan) {
  if (connection.getTimeout() != null) {
    int timeout = connection.getTimeout().toMillisI();
    plan.getSocket().setSoTimeout(timeout);
    plan.getRequest().setConnectTimeout(timeout);
    plan.getRequest().setSocketTimeout(timeout);
  }

  if (connection.getMaximumRetries() != null) {
    plan.getClient().setRetryHandler(new StandardHttpRequestRetryHandler(connection.getMaximumRetries(), false));
  }

  if (connection.getUserAgentSuffix() != null) {
    checkState(plan.getUserAgentBase() != null, "Default User-Agent not set");
    plan.setUserAgentSuffix(connection.getUserAgentSuffix());
  }

  if (Boolean.TRUE.equals(connection.getUseTrustStore())) {
    plan.getAttributes().put(SSLContextSelector.USE_TRUST_STORE, Boolean.TRUE);
  }

  if (Boolean.TRUE.equals(connection.getEnableCircularRedirects())) {
    plan.getRequest().setCircularRedirectsAllowed(true);
  }

  if (Boolean.TRUE.equals(connection.getEnableCookies())) {
    plan.getRequest().setCookieSpec(CookieSpecs.DEFAULT);
  }
}
 
Example #10
Source File: ApacheHttpClient43EngineWithRetry.java    From pnc with Apache License 2.0 5 votes vote down vote up
@Override
protected HttpClient createDefaultHttpClient() {
    logger.info("Bootstrapping http engine with request retry handler...");
    final HttpClientBuilder builder = HttpClientBuilder.create();
    RequestConfig.Builder requestBuilder = RequestConfig.custom();
    if (defaultProxy != null) {
        requestBuilder.setProxy(defaultProxy);
    }
    builder.disableContentCompression();
    builder.setDefaultRequestConfig(requestBuilder.build());

    HttpRequestRetryHandler retryHandler = new StandardHttpRequestRetryHandler();
    builder.setRetryHandler(retryHandler);
    return builder.build();
}
 
Example #11
Source File: HttpClientHelper.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public static CloseableHttpClient createHttpClient(final int maxRedirects) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
    final Registry<ConnectionSocketFactory> socketFactoryRegistry = createSocketFactoryConfigration();
    final BasicCookieStore cookieStore = new BasicCookieStore();
    return HttpClientBuilder.create()
        .setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry))
        .setRedirectStrategy(new LaxRedirectStrategy())
        .setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).setMaxRedirects(maxRedirects).build())
        .setDefaultCookieStore(cookieStore)
        .setRetryHandler(new StandardHttpRequestRetryHandler())
        .build();
}