最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Backoff策略提高HttpClient連接管理的效率

 更新時間:2023年10月15日 09:04:41   作者:codecraft  
這篇文章主要為大家介紹了Backoff策略提高HttpClient連接管理的效率使用解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

本文主要研究一下HttpClient的ConnectionBackoffStrategy

ConnectionBackoffStrategy

org/apache/http/client/ConnectionBackoffStrategy.java

/**
 * When managing a dynamic number of connections for a given route, this
 * strategy assesses whether a given request execution outcome should
 * result in a backoff signal or not, based on either examining the
 * {@code Throwable} that resulted or by examining the resulting
 * response (e.g. for its status code).
 *
 * @since 4.2
 *
 */
public interface ConnectionBackoffStrategy {

    /**
     * Determines whether seeing the given {@code Throwable} as
     * a result of request execution should result in a backoff
     * signal.
     * @param t the {@code Throwable} that happened
     * @return {@code true} if a backoff signal should be
     *   given
     */
    boolean shouldBackoff(Throwable t);

    /**
     * Determines whether receiving the given {@link HttpResponse} as
     * a result of request execution should result in a backoff
     * signal. Implementations MUST restrict themselves to examining
     * the response header and MUST NOT consume any of the response
     * body, if any.
     * @param resp the {@code HttpResponse} that was received
     * @return {@code true} if a backoff signal should be
     *   given
     */
    boolean shouldBackoff(HttpResponse resp);
}
ConnectionBackoffStrategy定義了shouldBackoff方法,它根據(jù)異常或者response來進行判斷

NullBackoffStrategy

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

public class NullBackoffStrategy implements ConnectionBackoffStrategy {

    @Override
    public boolean shouldBackoff(final Throwable t) {
        return false;
    }

    @Override
    public boolean shouldBackoff(final HttpResponse resp) {
        return false;
    }
}
NullBackoffStrategy實現(xiàn)了ConnectionBackoffStrategy,shouldBackoff方法返回false

DefaultBackoffStrategy

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

public class DefaultBackoffStrategy implements ConnectionBackoffStrategy {

    @Override
    public boolean shouldBackoff(final Throwable t) {
        return t instanceof SocketTimeoutException || t instanceof ConnectException;
    }

    @Override
    public boolean shouldBackoff(final HttpResponse resp) {
        return resp.getStatusLine().getStatusCode() == 429 ||
            resp.getStatusLine().getStatusCode() == HttpStatus.SC_SERVICE_UNAVAILABLE;
    }

}
DefaultBackoffStrategy在SocketTimeoutException或者ConnectException的時候返回true,或者在response code為429或者503的時候返回true

BackoffStrategyExec

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

@Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
public class BackoffStrategyExec implements ClientExecChain {
    private final ClientExecChain requestExecutor;
    private final ConnectionBackoffStrategy connectionBackoffStrategy;
    private final BackoffManager backoffManager;
    public BackoffStrategyExec(
            final ClientExecChain requestExecutor,
            final ConnectionBackoffStrategy connectionBackoffStrategy,
            final BackoffManager backoffManager) {
        super();
        Args.notNull(requestExecutor, "HTTP client request executor");
        Args.notNull(connectionBackoffStrategy, "Connection backoff strategy");
        Args.notNull(backoffManager, "Backoff manager");
        this.requestExecutor = requestExecutor;
        this.connectionBackoffStrategy = connectionBackoffStrategy;
        this.backoffManager = backoffManager;
    }
    @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");
        CloseableHttpResponse out = null;
        try {
            out = this.requestExecutor.execute(route, request, context, execAware);
        } catch (final Exception ex) {
            if (out != null) {
                out.close();
            }
            if (this.connectionBackoffStrategy.shouldBackoff(ex)) {
                this.backoffManager.backOff(route);
            }
            if (ex instanceof RuntimeException) {
                throw (RuntimeException) ex;
            }
            if (ex instanceof HttpException) {
                throw (HttpException) ex;
            }
            if (ex instanceof IOException) {
                throw (IOException) ex;
            }
            throw new UndeclaredThrowableException(ex);
        }
        if (this.connectionBackoffStrategy.shouldBackoff(out)) {
            this.backoffManager.backOff(route);
        } else {
            this.backoffManager.probe(route);
        }
        return out;
    }
}

BackoffStrategyExec實現(xiàn)了ClientExecChain接口,其execute執(zhí)行requestExecutor.execute,捕獲到異常的時候通過connectionBackoffStrategy.shouldBackoff(ex)來決定是否需要backOff,是的話執(zhí)行backoffManager.backOff(route);

若沒有異常則通過connectionBackoffStrategy.shouldBackoff(out)根據(jù)response來判斷是否需要backOff,是的化執(zhí)行backoffManager.backOff(route)

小結

HttpClient的DefaultBackoffStrategy在SocketTimeoutException或者ConnectException的時候返回true,或者在response code為429或者503的時候返回true;BackoffStrategyExec則通過connectionBackoffStrategy與backoffManager來配合執(zhí)行backOff。這個backOff的目的就是動態(tài)調整每個route的connection大小(MaxPerRoute)。

以上就是使用Backoff策略提高HttpClient連接管理的效率的詳細內容,更多關于HttpClient Backoff連接管理的資料請關注腳本之家其它相關文章!

相關文章

  • springboot使用spring-data-jpa操作MySQL數(shù)據(jù)庫

    springboot使用spring-data-jpa操作MySQL數(shù)據(jù)庫

    這篇文章主要介紹了springboot使用spring-data-jpa操作MySQL數(shù)據(jù)庫,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • 使用Java讀取本地文件并轉換為MultipartFile對象的方法

    使用Java讀取本地文件并轉換為MultipartFile對象的方法

    在許多Java Web應用中,我們經(jīng)常會遇到將本地文件上傳至服務器或其他系統(tǒng)的需求,在這種場景下,MultipartFile對象非常常用,用來表示HTTP請求中的文件,在本文中,我將演示如何編寫代碼來讀取本地文件并將其轉換為自定義的MultipartFile對象,需要的朋友可以參考下
    2025-08-08
  • spring如何實現(xiàn)依賴注入DI(spring-test方式)

    spring如何實現(xiàn)依賴注入DI(spring-test方式)

    本文主要介紹如何實現(xiàn)spring 的依賴注入,并且淺顯的講述一下注入需要注意的事項。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • java中response對象用法實例分析

    java中response對象用法實例分析

    這篇文章主要介紹了java中response對象用法,結合實例形式分析了Java中response對象的功能及具體使用技巧,需要的朋友可以參考下
    2015-12-12
  • Java實現(xiàn)高校教務系統(tǒng)

    Java實現(xiàn)高校教務系統(tǒng)

    這篇文章主要為大家詳細介紹了Java實現(xiàn)高校教務系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Maven引用自定義jar包方式

    Maven引用自定義jar包方式

    這篇文章主要介紹了Maven引用自定義jar包方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • spring整合atomikos實現(xiàn)分布式事務的方法示例

    spring整合atomikos實現(xiàn)分布式事務的方法示例

    本文整合了一個spring和atomikos的demo,并且通過案例演示說明atomikos的作用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • SpringBoot統(tǒng)一功能處理示例詳解(攔截器)

    SpringBoot統(tǒng)一功能處理示例詳解(攔截器)

    這篇文章主要介紹了SpringBoot統(tǒng)一功能處理(攔截器),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • Java基礎之文件概述

    Java基礎之文件概述

    這篇文章主要介紹了Java基礎之文件概述,文中有非常詳細的代碼示例,對正在學習java基礎的小伙伴們有一定的幫助,需要的朋友可以參考下
    2021-05-05
  • JAVA JDK8 List分組的實現(xiàn)和用法

    JAVA JDK8 List分組的實現(xiàn)和用法

    今天小編就為大家分享一篇關于JAVA JDK8 List分組的實現(xiàn)和用法,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12

最新評論

龙川县| 常熟市| 镇江市| 瑞丽市| 桑植县| 岗巴县| 静安区| 富裕县| 朝阳县| 信丰县| 徐闻县| 丁青县| 台中市| 延川县| 兴城市| 香港 | 大港区| 凤台县| 江西省| 新津县| 易门县| 通城县| 宝山区| 综艺| 城步| 开江县| 固镇县| 南投县| 梁平县| 阿荣旗| 苍溪县| 闸北区| 德昌县| 平遥县| 通许县| 凉山| 邓州市| 山东| 鲁甸县| 和林格尔县| 广丰县|