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

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

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

本文主要研究一下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ù)異?;蛘遰esponse來(lái)進(jìn)行判斷

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實(shí)現(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的時(shí)候返回true,或者在response code為429或者503的時(shí)候返回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實(shí)現(xiàn)了ClientExecChain接口,其execute執(zhí)行requestExecutor.execute,捕獲到異常的時(shí)候通過(guò)connectionBackoffStrategy.shouldBackoff(ex)來(lái)決定是否需要backOff,是的話執(zhí)行backoffManager.backOff(route);

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

小結(jié)

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

以上就是使用Backoff策略提高HttpClient連接管理的效率的詳細(xì)內(nèi)容,更多關(guān)于HttpClient Backoff連接管理的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

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

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

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

    使用Java讀取本地文件并轉(zhuǎn)換為MultipartFile對(duì)象的方法

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

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

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

    java中response對(duì)象用法實(shí)例分析

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

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

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

    Maven引用自定義jar包方式

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

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

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

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

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

    Java基礎(chǔ)之文件概述

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

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

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

最新評(píng)論

玉林市| 赞皇县| 佛学| 深州市| 肇州县| 霍邱县| 龙州县| 西乡县| 塘沽区| 陇南市| 阳高县| 阳城县| 沙雅县| 长治市| 南木林县| 大邑县| 左贡县| 高台县| 曲靖市| 洛南县| 安康市| 顺昌县| 龙井市| 马鞍山市| 曲周县| 长白| 琼结县| 桐庐县| 综艺| 务川| 习水县| 建瓯市| 香港 | 尼勒克县| 临汾市| 西城区| 商水县| 舞钢市| 嵊泗县| 本溪市| 吉木萨尔县|