org.apache.http.client.HttpRequestRetryHandler Java Examples

The following examples show how to use org.apache.http.client.HttpRequestRetryHandler. 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: ClientBuilder.java    From soabase with Apache License 2.0 6 votes vote down vote up
public HttpClient buildHttpClient(HttpClientConfiguration configuration, String clientName)
{
    Preconditions.checkState(providers.size() == 0, "HttpClient does not support providers");
    Preconditions.checkState(providerClasses.size() == 0, "HttpClient does not support providers");
    Preconditions.checkState(connectorProvider == null, "HttpClient does not support ConnectorProvider");

    HttpRequestRetryHandler nullRetry = new HttpRequestRetryHandler()
    {
        @Override
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context)
        {
            return false;
        }
    };

    HttpClient httpClient = new HttpClientBuilder(environment)
        .using(configuration)
        .using(nullRetry)  // Apache's retry mechanism does not allow changing hosts. Do retries manually
        .build(clientName);
    HttpClient client = new WrappedHttpClient(httpClient, retryComponents);

    SoaBundle.getFeatures(environment).putNamed(client, HttpClient.class, clientName);

    return client;
}
 
Example #2
Source File: SOAPClient.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up the client
 */
private void setup() {
    httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager());
    HttpParams params = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT);
    httpclient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
            return false;
        }
    });
}
 
Example #3
Source File: RESTClient.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up the client
 */
private void setup() {
    httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager());
    HttpParams params = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT);
    httpclient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
            return false;
        }
    });
}
 
Example #4
Source File: HttpEngine.java    From letv with Apache License 2.0 6 votes vote down vote up
private HttpEngine() {
    this.mDefaultHttpClient = null;
    this.mDefaultHttpClient = createHttpClient();
    this.mDefaultHttpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            if (DataStatistics.getInstance().isDebug()) {
                Log.d(DataStatistics.TAG, exception.getClass() + NetworkUtils.DELIMITER_COLON + exception.getMessage() + ",executionCount:" + executionCount);
            }
            if (executionCount >= 3) {
                return false;
            }
            if (exception instanceof NoHttpResponseException) {
                return true;
            }
            if (exception instanceof ClientProtocolException) {
                return true;
            }
            return false;
        }
    });
}
 
Example #5
Source File: DefaultHttpClientConfigurer.java    From rice with Educational Community License v2.0 6 votes vote down vote up
/**
 * Customizes the configuration of the httpClientBuilder.
 *
 * <p>Internally, this uses several helper methods to assist with configuring:
 * <ul>
 *     <li>Calls {@link #buildConnectionManager()} and sets the resulting {@link HttpClientConnectionManager} (if
 *     non-null) into the httpClientBuilder.</li>
 *     <li>Calls {@link #buildRequestConfig()} and sets the resulting {@link RequestConfig} (if non-null) into the
 *     httpClientBuilder.</li>
 *     <li>Calls {@link #buildRetryHandler()} and sets the resulting {@link HttpRequestRetryHandler} (if non-null)
 *     into the httpClientBuilder.</li>
 * </ul>
 * </p>
 *
 * @param httpClientBuilder the httpClientBuilder being configured
 */
@Override
public void customizeHttpClient(HttpClientBuilder httpClientBuilder) {

    HttpClientConnectionManager connectionManager = buildConnectionManager();
    if (connectionManager != null) {
        httpClientBuilder.setConnectionManager(connectionManager);
    }

    RequestConfig requestConfig = buildRequestConfig();
    if (requestConfig != null) {
        httpClientBuilder.setDefaultRequestConfig(requestConfig);
    }

    HttpRequestRetryHandler retryHandler = buildRetryHandler();
    if (retryHandler != null) {
        httpClientBuilder.setRetryHandler(retryHandler);
    }
}
 
Example #6
Source File: HttpLoadTestClient.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    SchemeRegistry supportedSchemes = new SchemeRegistry();
    SocketFactory sf = PlainSocketFactory.getSocketFactory();
    supportedSchemes.register(new Scheme("http", sf, 80));
    ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(supportedSchemes);
    connManager.setDefaultMaxPerRoute(1000);
    DefaultHttpClient client = new DefaultHttpClient(connManager);
    HttpParams params = client.getParams();
    HttpConnectionParams.setConnectionTimeout(params, 30000);
    HttpConnectionParams.setSoTimeout(params, 30000);
    client.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
            return false;
        }
    });
    //test API call
    long t1 = System.currentTimeMillis();
    testEndpoint(client,apiEndpoint);
    long t2 = System.currentTimeMillis();
    timeElapsedForAPICall = t2 - t1;


}
 
Example #7
Source File: HttpClientUtil.java    From ATest with GNU General Public License v3.0 6 votes vote down vote up
public static CloseableHttpClient getHttpClient(final int executionCount, int retryInterval) {
	ServiceUnavailableRetryStrategy serviceUnavailableRetryStrategy = new MyServiceUnavailableRetryStrategy.Builder()
			.executionCount(executionCount).retryInterval(retryInterval).build();
	return HttpClientBuilder.create().setRetryHandler(new HttpRequestRetryHandler() {
		@Override
		public boolean retryRequest(IOException e, int count, HttpContext contr) {
			if (count >= executionCount) {
				// Do not retry if over max retry count
				return false;
			}
			if (e instanceof InterruptedIOException) {
				// Timeout
				return true;
			}

			return true;
		}
	}).setServiceUnavailableRetryStrategy(serviceUnavailableRetryStrategy)
			.setConnectionManager(new PoolingHttpClientConnectionManager()).build();
}
 
Example #8
Source File: HttpClientUtil.java    From ATest with GNU General Public License v3.0 6 votes vote down vote up
public static CloseableHttpClient getHttpClient(final int executionCount, int retryInterval) {
	ServiceUnavailableRetryStrategy serviceUnavailableRetryStrategy = new MyServiceUnavailableRetryStrategy.Builder()
			.executionCount(executionCount).retryInterval(retryInterval).build();
	return HttpClientBuilder.create().setRetryHandler(new HttpRequestRetryHandler() {
		@Override
		public boolean retryRequest(IOException e, int count, HttpContext contr) {
			if (count >= executionCount) {
				// Do not retry if over max retry count
				return false;
			}
			if (e instanceof InterruptedIOException) {
				// Timeout
				return true;
			}

			return true;
		}
	}).setServiceUnavailableRetryStrategy(serviceUnavailableRetryStrategy)
			.setConnectionManager(new PoolingHttpClientConnectionManager()).build();
}
 
Example #9
Source File: AzureCloudInstanceInformationProcessor.java    From helix with Apache License 2.0 6 votes vote down vote up
public AzureCloudInstanceInformationProcessor(HelixCloudProperty helixCloudProperty) {
  _helixCloudProperty = helixCloudProperty;

  RequestConfig requestConifg = RequestConfig.custom()
      .setConnectionRequestTimeout((int) helixCloudProperty.getCloudRequestTimeout())
      .setConnectTimeout((int) helixCloudProperty.getCloudConnectionTimeout()).build();

  HttpRequestRetryHandler httpRequestRetryHandler =
      (IOException exception, int executionCount, HttpContext context) -> {
        LOG.warn("Execution count: " + executionCount + ".", exception);
        return !(executionCount >= helixCloudProperty.getCloudMaxRetry()
            || exception instanceof InterruptedIOException
            || exception instanceof UnknownHostException || exception instanceof SSLException);
      };

  //TODO: we should regularize the way how httpClient should be used throughout Helix. e.g. Helix-rest could also use in the same way
  _closeableHttpClient = HttpClients.custom().setDefaultRequestConfig(requestConifg)
      .setRetryHandler(httpRequestRetryHandler).build();
}
 
Example #10
Source File: MockHttpClient.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
@Override
protected RequestDirector createClientRequestDirector(
    HttpRequestExecutor requestExec,
    ClientConnectionManager conman,
    ConnectionReuseStrategy reustrat,
    ConnectionKeepAliveStrategy kastrat,
    HttpRoutePlanner rouplan,
    HttpProcessor httpProcessor,
    HttpRequestRetryHandler retryHandler,
    RedirectHandler redirectHandler,
    AuthenticationHandler targetAuthHandler,
    AuthenticationHandler proxyAuthHandler,
    UserTokenHandler stateHandler,
    HttpParams params) {
  return new RequestDirector() {
    @Beta
    public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context)
        throws HttpException, IOException {
      return new BasicHttpResponse(HttpVersion.HTTP_1_1, responseCode, null);
    }
  };
}
 
Example #11
Source File: HttpUtilManager.java    From zheshiyigeniubidexiangmu with MIT License 6 votes vote down vote up
private HttpUtilManager() {
	cm = new PoolingHttpClientConnectionManager();
	cm.setMaxTotal(500);
	cm.setDefaultMaxPerRoute(100);//例如默认每路由最高50并发,具体依据业务来定
	client = Init.httpClientBuilder
			.setConnectionManager(cm)
			.setKeepAliveStrategy(keepAliveStrat)
			.setMaxConnPerRoute(100)
			.setRetryHandler(new HttpRequestRetryHandler() {
				@Override
				public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
					return false;  //不需要retry
				}
			})
			.setMaxConnTotal(100)
			.build();
}
 
Example #12
Source File: HttpUtilManager.java    From zheshiyigeniubidexiangmu with MIT License 6 votes vote down vote up
private HttpUtilManager() {
	cm = new PoolingHttpClientConnectionManager();
	cm.setMaxTotal(500);
	cm.setDefaultMaxPerRoute(100);//例如默认每路由最高50并发,具体依据业务来定
	client = Init.httpClientBuilder
			.setConnectionManager(cm)
			.setKeepAliveStrategy(keepAliveStrat)
			.setMaxConnPerRoute(100)
			.setRetryHandler(new HttpRequestRetryHandler() {
				@Override
				public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
					return false;  //不需要retry
				}
			})
			.setMaxConnTotal(100)
			.build();
}
 
Example #13
Source File: ApacheHttpClientFactory.java    From raptor with Apache License 2.0 6 votes vote down vote up
public CloseableHttpClient createHttpClient(HttpClientConnectionManager httpClientConnectionManager,
                                         RaptorHttpClientProperties httpClientProperties) {

    RequestConfig defaultRequestConfig = RequestConfig.custom()
            .setConnectTimeout(httpClientProperties.getConnectionTimeout())
            .setSocketTimeout(httpClientProperties.getReadTimeout())
            .setRedirectsEnabled(httpClientProperties.isFollowRedirects())
            .build();

    HttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(httpClientProperties.getRetryCount(),
            httpClientProperties.isRequestSentRetryEnabled());
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().disableContentCompression()
            .disableCookieManagement()
            .useSystemProperties()
            .setRetryHandler(retryHandler)
            .setConnectionManager(httpClientConnectionManager)
            .setDefaultRequestConfig(defaultRequestConfig);

    if(!keepAlive){
        httpClientBuilder.setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE);
    }

    return httpClientBuilder.build();
}
 
Example #14
Source File: HttpUtils.java    From common-mvc with MIT License 6 votes vote down vote up
private HttpUtils() {
	cm = new PoolingHttpClientConnectionManager();
	cm.setMaxTotal(500);
	cm.setDefaultMaxPerRoute(100);//例如默认每路由最高50并发,具体依据业务来定
	client = HttpClients.custom()
			.setConnectionManager(cm)
			.setKeepAliveStrategy(keepAliveStrat)
			.setMaxConnPerRoute(100)
			.setRetryHandler(new HttpRequestRetryHandler() {
				@Override
				public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
					return false;  //不需要retry
				}
			})
			.setMaxConnTotal(100)
			.build();
}
 
Example #15
Source File: HttpHelper.java    From miappstore with Apache License 2.0 6 votes vote down vote up
/** 执行网络访问 */
private static HttpResult execute(String url, HttpRequestBase requestBase) {
	boolean isHttps = url.startsWith("https://");//判断是否需要采用https
	AbstractHttpClient httpClient = HttpClientFactory.create(isHttps);
	HttpContext httpContext = new SyncBasicHttpContext(new BasicHttpContext());
	HttpRequestRetryHandler retryHandler = httpClient.getHttpRequestRetryHandler();//获取重试机制
	int retryCount = 0;
	boolean retry = true;
	while (retry) {
		try {
			HttpResponse response = httpClient.execute(requestBase, httpContext);//访问网络
			if (response != null) {
				return new HttpResult(response, httpClient, requestBase);
			}
		} catch (Exception e) {
			IOException ioException = new IOException(e.getMessage());
			retry = retryHandler.retryRequest(ioException, ++retryCount, httpContext);//把错误异常交给重试机制,以判断是否需要采取从事
		}
	}
	return null;
}
 
Example #16
Source File: HttpHelper.java    From NetEasyNews with GNU General Public License v3.0 5 votes vote down vote up
/**
     * 执行网络访问
     */
    private static void execute(String url, HttpRequestBase requestBase, HttpCallbackListener httpCallbackListener) {
        boolean isHttps = url.startsWith("https://");//判断是否需要采用https
        AbstractHttpClient httpClient = HttpClientFactory.create(isHttps);
        HttpContext httpContext = new SyncBasicHttpContext(new BasicHttpContext());
        HttpRequestRetryHandler retryHandler = httpClient.getHttpRequestRetryHandler();//获取重试机制
        int retryCount = 0;
        boolean retry = true;
        while (retry) {
            try {
                HttpResponse response = httpClient.execute(requestBase, httpContext);//访问网络
                int stateCode  = response.getStatusLine().getStatusCode();
//                LogUtils.e(TAG, "http状态码:" + stateCode);
                if (response != null) {
                    if (stateCode == HttpURLConnection.HTTP_OK){
                        HttpResult httpResult = new HttpResult(response, httpClient, requestBase);
                        String result = httpResult.getString();
                        if (!TextUtils.isEmpty(result)){
                            httpCallbackListener.onSuccess(result);
                            return;
                        } else {
                            throw new RuntimeException("数据为空");
                        }
                    } else {
                        throw new RuntimeException(HttpRequestCode.ReturnCode(stateCode));
                    }
                }
            } catch (Exception e) {
                IOException ioException = new IOException(e.getMessage());
                retry = retryHandler.retryRequest(ioException, ++retryCount, httpContext);//把错误异常交给重试机制,以判断是否需要采取从事
                LogUtils.e(TAG, "重复次数:" + retryCount + "   :"+ e);
                if (!retry){
                    httpCallbackListener.onError(e);
                }
            }
        }
    }
 
Example #17
Source File: HttpClientConfigurer.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void configureRetryHandler(DefaultHttpClient httpClient) {
    httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            return false;
        }
    });
}
 
Example #18
Source File: HttpClientConfigurer.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void configureRetryHandler(DefaultHttpClient httpClient) {
    httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            return false;
        }
    });
}
 
Example #19
Source File: SimpleHttpClient.java    From product-ei with Apache License 2.0 5 votes vote down vote up
public SimpleHttpClient() {
    this.client = new DefaultHttpClient(new ThreadSafeClientConnManager());
    HttpParams params = client.getParams();
    HttpConnectionParams.setConnectionTimeout(params, 30000);
    HttpConnectionParams.setSoTimeout(params, 30000);
    client.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
            return false;
        }
    });
}
 
Example #20
Source File: RESTClient.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up the client
 */
private void setup() {
    httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager());
    HttpParams params = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT);
    httpclient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
            return false;
        }
    });
}
 
Example #21
Source File: SOAPClient.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up the client
 */
private void setup() {
    httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager());
    HttpParams params = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT);
    httpclient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
            return false;
        }
    });
}
 
Example #22
Source File: SimpleHttpClient.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public SimpleHttpClient() {
    this.client = new DefaultHttpClient(new ThreadSafeClientConnManager());
    HttpParams params = client.getParams();
    HttpConnectionParams.setConnectionTimeout(params, 30000);
    HttpConnectionParams.setSoTimeout(params, 30000);
    client.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
            return false;
        }
    });
}
 
Example #23
Source File: HttpUtils.java    From yuzhouwan with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化 httpClient 客户端.
 */
private HttpClientBuilder initHttpClient(PoolingHttpClientConnectionManager httpClientConnectionManager,
                                         RequestConfig defaultRequestConfig, LaxRedirectStrategy redirectStrategy,
                                         HttpRequestRetryHandler retryHandler) {
    return HttpClients.custom()
            .setConnectionManager(httpClientConnectionManager).setDefaultRequestConfig(defaultRequestConfig)
            .setRedirectStrategy(redirectStrategy).setRetryHandler(retryHandler);
}
 
Example #24
Source File: HttpUtils.java    From yuzhouwan with Apache License 2.0 5 votes vote down vote up
/**
 * 重试策略.
 */
private HttpRequestRetryHandler retryPolicy() {
    return (IOException exception, int executionCount, HttpContext context) -> {
        // Do not retry if over max retry count
        if (executionCount >= MAX_RETRY) return false;
        // Retry if the server dropped connection on us
        if (exception instanceof NoHttpResponseException) return true;
        // Do not retry on SSL handshake exception
        if (exception instanceof SSLHandshakeException) return false;
        HttpRequest request = (HttpRequest) context.getAttribute(HttpCoreContext.HTTP_REQUEST);
        // Retry if the request is considered idempotent
        return !(request instanceof HttpEntityEnclosingRequest);
    };
}
 
Example #25
Source File: HCB.java    From httpclientutil with Apache License 2.0 5 votes vote down vote up
/**
 * 重试(如果请求是幂等的,就再次尝试)
 * 
 * @param tryTimes						重试次数
 * @param retryWhenInterruptedIO		连接拒绝时,是否重试
 * @return	返回当前对象
 */
public HCB retry(final int tryTimes, final boolean retryWhenInterruptedIO){
	// 请求重试处理
    HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            if (executionCount >= tryTimes) {// 如果已经重试了n次,就放弃
                return false;
            }
            if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接,那么就重试
                return true;
            }
            if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常
                return false;
            }
            if (exception instanceof InterruptedIOException) {// 超时
                //return false;
                return retryWhenInterruptedIO;
            }
            if (exception instanceof UnknownHostException) {// 目标服务器不可达
                return true;
            }
            if (exception instanceof ConnectTimeoutException) {// 连接被拒绝
            	return false;
            }
            if (exception instanceof SSLException) {// SSL握手异常
                return false;
            }

            HttpClientContext clientContext = HttpClientContext .adapt(context);
            HttpRequest request = clientContext.getRequest();
            // 如果请求是幂等的,就再次尝试
            if (!(request instanceof HttpEntityEnclosingRequest)) {
                return true;
            }
            return false;
        }
    };
    this.setRetryHandler(httpRequestRetryHandler);
    return this;
}
 
Example #26
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 #27
Source File: DefaultHttpClientConfigurer.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * Builds the retry handler if {@link #RETRY_SOCKET_EXCEPTION_PROPERTY} is true in the project's configuration.
 *
 * @return the HttpRequestRetryHandler or null depending on configuration
 */
protected HttpRequestRetryHandler buildRetryHandler() {
    // If configured to do so, allow the client to retry once
    if (ConfigContext.getCurrentContextConfig().getBooleanProperty(RETRY_SOCKET_EXCEPTION_PROPERTY, false)) {
        return new DefaultHttpRequestRetryHandler(1, true);
    }

    return null;
}
 
Example #28
Source File: LibHttpClient.java    From YiBo with Apache License 2.0 5 votes vote down vote up
@Override
protected RequestDirector createClientRequestDirector(
        final HttpRequestExecutor requestExec,
        final ClientConnectionManager conman,
        final ConnectionReuseStrategy reustrat,
        final ConnectionKeepAliveStrategy kastrat,
        final HttpRoutePlanner rouplan,
        final HttpProcessor httpProcessor,
        final HttpRequestRetryHandler retryHandler,
        final RedirectHandler redirectHandler,
        final AuthenticationHandler targetAuthHandler,
        final AuthenticationHandler proxyAuthHandler,
        final UserTokenHandler stateHandler,
        final HttpParams params) {
    return new LibRequestDirector(
            requestExec,
            conman,
            reustrat,
            kastrat,
            rouplan,
            httpProcessor,
            retryHandler,
            redirectHandler,
            targetAuthHandler,
            proxyAuthHandler,
            stateHandler,
            params);
}
 
Example #29
Source File: AviRestUtils.java    From sdk with Apache License 2.0 5 votes vote down vote up
/**
 * This method sets a custom HttpRequestRetryHandler in order to enable a custom
 * exception recovery mechanism.
 * 
 * @return A HttpRequestRetryHandler representing handling of the retryHandler.
 */
private static HttpRequestRetryHandler retryHandler(AviCredentials creds) {
	return (exception, executionCount, context) -> {

		if (executionCount >= creds.getNumApiRetries()) {
			// Do not retry if over max retry count
			return false;
		}
		if (exception instanceof InterruptedIOException) {
			// Timeout
			return false;
		}
		if (exception instanceof UnknownHostException) {
			// Unknown host
			return false;
		}
		if (exception instanceof SSLException) {
			// SSL handshake exception
			return false;
		}
		if (exception instanceof HttpHostConnectException) {
			return true;
		}
		HttpClientContext clientContext = HttpClientContext.adapt(context);
		HttpRequest request = clientContext.getRequest();
		boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
		if (idempotent) {
			// Retry if the request is considered idempotent
			return true;
		}
		return false;
	};
}
 
Example #30
Source File: HttpClientConfigTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void testGetAndSetHttpRequestRetryHandler()
{
    HttpRequestRetryHandler handler = mock(HttpRequestRetryHandler.class);
    config.setHttpRequestRetryHandler(handler);
    assertEquals(handler, config.getHttpRequestRetryHandler());
}