本文整理汇总了Java中org.apache.http.client.HttpRequestRetryHandler类的典型用法代码示例。如果您正苦于以下问题:Java HttpRequestRetryHandler类的具体用法?Java HttpRequestRetryHandler怎么用?Java HttpRequestRetryHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HttpRequestRetryHandler类属于org.apache.http.client包,在下文中一共展示了HttpRequestRetryHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: DefaultConnector
import org.apache.http.client.HttpRequestRetryHandler; //导入依赖的package包/类
/**
*
*/
public DefaultConnector() {
final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.closeIdleConnections(120, TimeUnit.SECONDS);
// would be nice to set this from outside -> keep alive
final SocketConfig sConfig = SocketConfig.custom().setSoKeepAlive(true).setSoTimeout(Context.SOCKET_TO).build();
cm.setDefaultSocketConfig(sConfig);
cm.setMaxTotal(150);
cm.setDefaultMaxPerRoute(150);
cm.setValidateAfterInactivity(0);
final HttpRequestRetryHandler rh = new DefaultHttpRequestRetryHandler(3, true);
httpClient = HttpClients.custom().setRetryHandler(rh).setConnectionManager(cm).build();
}
示例2: execute
import org.apache.http.client.HttpRequestRetryHandler; //导入依赖的package包/类
/** 执行网络访问 */
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;
}
示例3: buildRetryHandler
import org.apache.http.client.HttpRequestRetryHandler; //导入依赖的package包/类
/**
* Builds a custom retry handler that will retry a request up to 20 times if the call fails to connect to the
* destination. This is used since startup of nginx and other upstream servers are asynchronous. This smooths
* over the "bumpiness" of getting everything started up before we make a call.
*
* @return The retry handler that will be used by the custom http client.
*/
static HttpRequestRetryHandler buildRetryHandler() {
final int maxRetries = calcMaxRetries();
return (exception, executionCount, context) -> {
if (executionCount > maxRetries) {
// Do not retry if over max retry count
return false;
}
try {
Thread.sleep(getProperties().getMaxNginxStartupPollingTimeMs());
} catch (InterruptedException e) {
}
// Retry if the server dropped connection on us
return true;
};
}
示例4: HttpEngine
import org.apache.http.client.HttpRequestRetryHandler; //导入依赖的package包/类
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;
}
});
}
示例5: DefaultRequestDirector
import org.apache.http.client.HttpRequestRetryHandler; //导入依赖的package包/类
@Deprecated
public DefaultRequestDirector(
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 userTokenHandler,
final HttpParams params) {
this(LogFactory.getLog(DefaultRequestDirector.class),
requestExec, conman, reustrat, kastrat, rouplan, httpProcessor, retryHandler,
new DefaultRedirectStrategyAdaptor(redirectHandler),
new AuthenticationStrategyAdaptor(targetAuthHandler),
new AuthenticationStrategyAdaptor(proxyAuthHandler),
userTokenHandler,
params);
}
示例6: execute
import org.apache.http.client.HttpRequestRetryHandler; //导入依赖的package包/类
/** 执行网络访问 */
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);//把错误异常交给重试机制,以判断是否需要采取从事
LogUtils.e(e);
}
}
return null;
}
示例7: customizeHttpClient
import org.apache.http.client.HttpRequestRetryHandler; //导入依赖的package包/类
/**
* 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);
}
}
示例8: buildHttpClient
import org.apache.http.client.HttpRequestRetryHandler; //导入依赖的package包/类
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;
}
示例9: getRequestDirector
import org.apache.http.client.HttpRequestRetryHandler; //导入依赖的package包/类
/** This method mainly exists to make the wrapper more testable. oh, apache's insanity. */
protected RequestDirector getRequestDirector(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 DefaultRequestDirector(requestExec, conman, reustrat, kastrat, rouplan,
httpProcessor, retryHandler, redirectHandler, targetAuthHandler, proxyAuthHandler,
stateHandler, params);
}
示例10: getRequestDirector
import org.apache.http.client.HttpRequestRetryHandler; //导入依赖的package包/类
/** This method mainly exists to make the wrapper more testable. oh, apache's insanity. */
protected RequestDirector getRequestDirector(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 DefaultRequestDirector(requestExec, conman, reustrat, kastrat, rouplan,
httpProcessor, retryHandler, redirectHandler, targetAuthHandler, proxyAuthHandler,
stateHandler, params);
}
示例11: createHttpClient
import org.apache.http.client.HttpRequestRetryHandler; //导入依赖的package包/类
private static CloseableHttpClient createHttpClient(int maxTotal, int maxPerRoute, int maxRoute, String hostname, int port) {
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory
.getSocketFactory();
LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory
.getSocketFactory();
Registry<ConnectionSocketFactory> registry = RegistryBuilder
.<ConnectionSocketFactory>create()
.register("http", plainsf)
.register("https", sslsf)
.build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(
registry);
// 将最大连接数增加
cm.setMaxTotal(maxTotal);
// 将每个路由基础的连接增加
cm.setDefaultMaxPerRoute(maxPerRoute);
// 将目标主机的最大连接数增加
HttpHost httpHost = new HttpHost(hostname, port);
cm.setMaxPerRoute(new HttpRoute(httpHost), maxRoute);
// 请求重试处理
HttpRequestRetryHandler httpRequestRetryHandler = (exception, executionCount, context) -> {
if (executionCount >= 5) {// 如果已经重试了5次,就放弃
return false;
}
if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接,那么就重试
return true;
}
if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常
return false;
}
if (exception instanceof InterruptedIOException) {// 超时
return false;
}
if (exception instanceof UnknownHostException) {// 目标服务器不可达
return false;
}
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;
};
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cm)
.setRetryHandler(httpRequestRetryHandler)
.build();
return httpClient;
}
示例12: createClientRequestDirector
import org.apache.http.client.HttpRequestRetryHandler; //导入依赖的package包/类
/**
* @deprecated (4.1) do not use
*/
@Deprecated
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 userTokenHandler,
final HttpParams params) {
return new DefaultRequestDirector(
requestExec,
conman,
reustrat,
kastrat,
rouplan,
httpProcessor,
retryHandler,
redirectHandler,
targetAuthHandler,
proxyAuthHandler,
userTokenHandler,
params);
}
示例13: createHttpClient
import org.apache.http.client.HttpRequestRetryHandler; //导入依赖的package包/类
public static CloseableHttpClient createHttpClient() {
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
ConnectionSocketFactory sslsf = new EasySSLConnectionSocketFactory();
//SSLConnectionSocketFactory.getSocketFactory();
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", plainsf)
.register("https", sslsf)
.build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
// 将最大连接数增加到200
cm.setMaxTotal(200);
// 将每个路由基础的连接增加到20
cm.setDefaultMaxPerRoute(20);
//请求重试处理
HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (executionCount >= 5) {// 如果已经重试了5次,就放弃
return false;
}
if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接,那么就重试
return true;
}
if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常
return false;
}
if (exception instanceof InterruptedIOException) {// 超时
return false;
}
if (exception instanceof UnknownHostException) {// 目标服务器不可达
return false;
}
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;
}
};
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cm)
.setRetryHandler(httpRequestRetryHandler)
.build();
return httpClient;
}
示例14: init
import org.apache.http.client.HttpRequestRetryHandler; //导入依赖的package包/类
public static void init() throws RuntimeException {
try {
logger.warn(NOTICELINE + " httpUtil init begin " + NOTICELINE);
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
// sslContextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
sslContextBuilder.loadTrustMaterial(null,new TrustAnyTrustManager());
SSLConnectionSocketFactory sslConnectionSocketFactory =
new SSLConnectionSocketFactory(
sslContextBuilder.build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().
register("http", new PlainConnectionSocketFactory()).
register("https", sslConnectionSocketFactory).
build();
logger.warn(NOTICELINE + " SSL context init done " + NOTICELINE);
//init connectionManager , ThreadSafe pooled conMgr
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(registry);
poolingHttpClientConnectionManager.setMaxTotal(30);
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(3);
//init request config. pooltimeout,sotime,contimeout
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(POOL_TIMECOUT).setConnectTimeout(CON_TIMEOUT).setSocketTimeout(SO_TIMEOUT).build();
// begin construct httpclient
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager);
httpClientBuilder.setDefaultRequestConfig(requestConfig);
httpClientBuilder.setRetryHandler(new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (executionCount >= HTTP_RETRY_COUNT) {
return false;
}
if (exception instanceof InterruptedIOException) {
// Timeout
logger.warn("httpUtil retry for InterruptIOException");
return true;
}
if (exception instanceof UnknownHostException) {
// Unknown host
return false;
}
if (exception instanceof SSLException) {
// SSL handshake exception
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent) {
// Retry if the request is considered idempotent
logger.warn("httpUtil retry for idempotent");
return true;
}
return false;
}
});
logger.warn(NOTICELINE + " poolManager , requestconfig init done " + NOTICELINE);
httpclient = httpClientBuilder.build();
logger.warn(NOTICELINE + " httpUtil init done " + NOTICELINE);
} catch (Exception e) {
logger.error(NOTICELINE + "httpclient init fail" + NOTICELINE, e);
throw new RuntimeException(e);
}
}
示例15: RetryExec
import org.apache.http.client.HttpRequestRetryHandler; //导入依赖的package包/类
public RetryExec(
final ClientExecChain requestExecutor,
final HttpRequestRetryHandler retryHandler) {
Args.notNull(requestExecutor, "HTTP request executor");
Args.notNull(retryHandler, "HTTP request retry handler");
this.requestExecutor = requestExecutor;
this.retryHandler = retryHandler;
}