成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

apache的HttpClient的默認(rèn)重試機(jī)制

MartinDai / 3434人閱讀

摘要:異常重試默認(rèn)重試次,三次都失敗則拋出或其他異常

maven
        
            org.apache.httpcomponents
            httpclient
            4.5.2
        
異常重試log
2017-01-31 19:31:39.057  INFO 3873 --- [askScheduler-13] o.apache.http.impl.execchain.RetryExec   : I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {}->http://192.168.99.100:8080: The target server failed to respond
2017-01-31 19:31:39.058  INFO 3873 --- [askScheduler-13] o.apache.http.impl.execchain.RetryExec   : Retrying request to {}->http://192.168.99.100:8080
RetryExec

org/apache/http/impl/execchain/RetryExec.java

/**
 * Request executor in the request execution chain that is responsible
 * for making a decision whether a request failed due to an I/O error
 * should be re-executed.
 * 

* Further responsibilities such as communication with the opposite * endpoint is delegated to the next executor in the request execution * chain. *

* * @since 4.3 */ @Immutable public class RetryExec implements ClientExecChain { private final Log log = LogFactory.getLog(getClass()); private final ClientExecChain requestExecutor; private final HttpRequestRetryHandler retryHandler; 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; } @Override public CloseableHttpResponse execute( final HttpRoute route, final HttpRequestWrapper request, final HttpClientContext context, final HttpExecutionAware execAware) throws IOException, HttpException { Args.notNull(route, "HTTP route"); Args.notNull(request, "HTTP request"); Args.notNull(context, "HTTP context"); final Header[] origheaders = request.getAllHeaders(); for (int execCount = 1;; execCount++) { try { return this.requestExecutor.execute(route, request, context, execAware); } catch (final IOException ex) { if (execAware != null && execAware.isAborted()) { this.log.debug("Request has been aborted"); throw ex; } if (retryHandler.retryRequest(ex, execCount, context)) { if (this.log.isInfoEnabled()) { this.log.info("I/O exception ("+ ex.getClass().getName() + ") caught when processing request to " + route + ": " + ex.getMessage()); } if (this.log.isDebugEnabled()) { this.log.debug(ex.getMessage(), ex); } if (!RequestEntityProxy.isRepeatable(request)) { this.log.debug("Cannot retry non-repeatable request"); throw new NonRepeatableRequestException("Cannot retry request " + "with a non-repeatable request entity", ex); } request.setHeaders(origheaders); if (this.log.isInfoEnabled()) { this.log.info("Retrying request to " + route); } } else { if (ex instanceof NoHttpResponseException) { final NoHttpResponseException updatedex = new NoHttpResponseException( route.getTargetHost().toHostString() + " failed to respond"); updatedex.setStackTrace(ex.getStackTrace()); throw updatedex; } else { throw ex; } } } } } }
DefaultHttpRequestRetryHandler

org/apache/http/impl/client/DefaultHttpRequestRetryHandler.java

/**
 * The default {@link HttpRequestRetryHandler} used by request executors.
 *
 * @since 4.0
 */
@Immutable
public class DefaultHttpRequestRetryHandler implements HttpRequestRetryHandler {

    public static final DefaultHttpRequestRetryHandler INSTANCE = new DefaultHttpRequestRetryHandler();

    /** the number of times a method will be retried */
    private final int retryCount;

    /** Whether or not methods that have successfully sent their request will be retried */
    private final boolean requestSentRetryEnabled;

    private final Set> nonRetriableClasses;

    /**
     * Create the request retry handler using the specified IOException classes
     *
     * @param retryCount how many times to retry; 0 means no retries
     * @param requestSentRetryEnabled true if it"s OK to retry requests that have been sent
     * @param clazzes the IOException types that should not be retried
     * @since 4.3
     */
    protected DefaultHttpRequestRetryHandler(
            final int retryCount,
            final boolean requestSentRetryEnabled,
            final Collection> clazzes) {
        super();
        this.retryCount = retryCount;
        this.requestSentRetryEnabled = requestSentRetryEnabled;
        this.nonRetriableClasses = new HashSet>();
        for (final Class clazz: clazzes) {
            this.nonRetriableClasses.add(clazz);
        }
    }

    /**
     * Create the request retry handler using the following list of
     * non-retriable IOException classes: 
*
    *
  • InterruptedIOException
  • *
  • UnknownHostException
  • *
  • ConnectException
  • *
  • SSLException
  • *
* @param retryCount how many times to retry; 0 means no retries * @param requestSentRetryEnabled true if it"s OK to retry non-idempotent requests that have been sent */ @SuppressWarnings("unchecked") public DefaultHttpRequestRetryHandler(final int retryCount, final boolean requestSentRetryEnabled) { this(retryCount, requestSentRetryEnabled, Arrays.asList( InterruptedIOException.class, UnknownHostException.class, ConnectException.class, SSLException.class)); } /** * Create the request retry handler with a retry count of 3, requestSentRetryEnabled false * and using the following list of non-retriable IOException classes:
*
    *
  • InterruptedIOException
  • *
  • UnknownHostException
  • *
  • ConnectException
  • *
  • SSLException
  • *
*/ public DefaultHttpRequestRetryHandler() { this(3, false); } /** * Used {@code retryCount} and {@code requestSentRetryEnabled} to determine * if the given method should be retried. */ @Override public boolean retryRequest( final IOException exception, final int executionCount, final HttpContext context) { Args.notNull(exception, "Exception parameter"); Args.notNull(context, "HTTP context"); if (executionCount > this.retryCount) { // Do not retry if over max retry count return false; } if (this.nonRetriableClasses.contains(exception.getClass())) { return false; } else { for (final Class rejectException : this.nonRetriableClasses) { if (rejectException.isInstance(exception)) { return false; } } } final HttpClientContext clientContext = HttpClientContext.adapt(context); final HttpRequest request = clientContext.getRequest(); if(requestIsAborted(request)){ return false; } if (handleAsIdempotent(request)) { // Retry if the request is considered idempotent return true; } if (!clientContext.isRequestSent() || this.requestSentRetryEnabled) { // Retry if the request has not been sent fully or // if it"s OK to retry methods that have been sent return true; } // otherwise do not retry return false; } /** * @return {@code true} if this handler will retry methods that have * successfully sent their request, {@code false} otherwise */ public boolean isRequestSentRetryEnabled() { return requestSentRetryEnabled; } /** * @return the maximum number of times a method will be retried */ public int getRetryCount() { return retryCount; } /** * @since 4.2 */ protected boolean handleAsIdempotent(final HttpRequest request) { return !(request instanceof HttpEntityEnclosingRequest); } /** * @since 4.2 * * @deprecated (4.3) */ @Deprecated protected boolean requestIsAborted(final HttpRequest request) { HttpRequest req = request; if (request instanceof RequestWrapper) { // does not forward request to original req = ((RequestWrapper) request).getOriginal(); } return (req instanceof HttpUriRequest && ((HttpUriRequest)req).isAborted()); } }

默認(rèn)重試3次,三次都失敗則拋出NoHttpResponseException或其他異常

doc

Apache HttpComponents

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/66577.html

相關(guān)文章

  • 記一次線程掛死排查過(guò)程(附 HttpClient 配置建議)

    摘要:此時(shí)我想到了福爾摩斯說(shuō)過(guò)的一句話當(dāng)你排除掉各種不可能出現(xiàn)的情況之后,剩下的情況無(wú)論多么難以置信,都是真相。福爾摩斯冷靜下來(lái)想一想,這個(gè)線程,有可能靜悄悄地退出了嗎,沒留下半點(diǎn)異常日志從理論上來(lái)說(shuō),不可能。配置建議最后,附上一份配置建議。 1、事發(fā) 我們有個(gè)視頻處理程序,基于 SpringBoot,會(huì)啟動(dòng)幾個(gè)線程來(lái)跑。要退出程序時(shí),會(huì)發(fā)送一個(gè)信號(hào)給程序,每個(gè)線程收到信號(hào)后會(huì)平滑退出,等全...

    jollywing 評(píng)論0 收藏0
  • Apache httpclientexecute方法調(diào)試

    摘要:因?yàn)楣ぷ餍枰?,想研究一下?zhí)行的邏輯。在這一行調(diào)用的實(shí)現(xiàn)我在代碼里聲明的只是一個(gè)接口,實(shí)現(xiàn)類是。首先根據(jù)傳入的請(qǐng)求決定出目標(biāo)投遞到執(zhí)行。 因?yàn)楣ぷ餍枰?,想研究一下execute執(zhí)行的邏輯。 在這一行調(diào)用execute: response = getHttpClient().execute(get); getHttpClient的實(shí)現(xiàn): private HttpClient getHttp...

    wudengzan 評(píng)論0 收藏0
  • 爬蟲框架WebMagic源碼分析之Downloader

    摘要:方法,首先判斷是否有這是在中配置的,如果有,直接調(diào)用的將相應(yīng)內(nèi)容轉(zhuǎn)化成對(duì)應(yīng)編碼字符串,否則智能檢測(cè)響應(yīng)內(nèi)容的字符編碼。 Downloader是負(fù)責(zé)請(qǐng)求url獲取返回值(html、json、jsonp等)的一個(gè)組件。當(dāng)然會(huì)同時(shí)處理POST重定向、Https驗(yàn)證、ip代理、判斷失敗重試等。 接口:Downloader 定義了download方法返回Page,定義了setThread方法來(lái)...

    104828720 評(píng)論0 收藏0
  • spring-cloud-feign源碼深度解析

    摘要:內(nèi)部使用了的動(dòng)態(tài)代理為目標(biāo)接口生成了一個(gè)動(dòng)態(tài)代理類,這里會(huì)生成一個(gè)動(dòng)態(tài)代理原理統(tǒng)一的方法攔截器,同時(shí)為接口的每個(gè)方法生成一個(gè)攔截器,并解析方法上的元數(shù)據(jù),生成一個(gè)請(qǐng)求模板。的核心源碼解析到此結(jié)束了,不知道是否對(duì)您有無(wú)幫助,可留言跟我交流。 Feign是一個(gè)聲明式的Web服務(wù)客戶端。這使得Web服務(wù)客戶端的寫入更加方便 要使用Feign創(chuàng)建一個(gè)界面并對(duì)其進(jìn)行注釋。它具有可插拔注釋支持,包...

    vibiu 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<