聊聊jest的IdleConnectionReaper

栏目: Java · 发布时间: 5年前

内容简介:本文主要研究一下jest的IdleConnectionReaperjest-common-6.3.1-sources.jar!/io/searchbox/client/config/idle/IdleConnectionReaper.javajest-common-6.3.1-sources.jar!/io/searchbox/client/config/idle/ReapableConnectionManager.java

本文主要研究一下jest的IdleConnectionReaper

IdleConnectionReaper

jest-common-6.3.1-sources.jar!/io/searchbox/client/config/idle/IdleConnectionReaper.java

public class IdleConnectionReaper extends AbstractScheduledService {

    final static Logger logger = LoggerFactory.getLogger(IdleConnectionReaper.class);

    private final ReapableConnectionManager reapableConnectionManager;
    private final ClientConfig clientConfig;

    public IdleConnectionReaper(ClientConfig clientConfig, ReapableConnectionManager reapableConnectionManager) {
        this.reapableConnectionManager = reapableConnectionManager;
        this.clientConfig = clientConfig;
    }

    @Override
    protected void runOneIteration() throws Exception {
        logger.debug("closing idle connections...");
        reapableConnectionManager.closeIdleConnections(clientConfig.getMaxConnectionIdleTime(),
                                                       clientConfig.getMaxConnectionIdleTimeDurationTimeUnit());
    }

    @Override
    protected Scheduler scheduler() {
        return Scheduler.newFixedDelaySchedule(0l,
                                               clientConfig.getMaxConnectionIdleTime(),
                                               clientConfig.getMaxConnectionIdleTimeDurationTimeUnit());
    }

    @Override
    protected ScheduledExecutorService executor() {
        final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(
            new ThreadFactoryBuilder()
                .setDaemon(true)
                .setNameFormat(serviceName())
                .build());
        // Add a listener to shutdown the executor after the service is stopped.  This ensures that the
        // JVM shutdown will not be prevented from exiting after this service has stopped or failed.
        // Technically this listener is added after start() was called so it is a little gross, but it
        // is called within doStart() so we know that the service cannot terminate or fail concurrently
        // with adding this listener so it is impossible to miss an event that we are interested in.
        addListener(new Listener() {
            @Override public void terminated(State from) {
                executor.shutdown();
            }
            @Override public void failed(State from, Throwable failure) {
                executor.shutdown();
            }}, MoreExecutors.directExecutor());
        return executor;
    }
}
  • IdleConnectionReaper继承了AbstractScheduledService,它的构造器接收clientConfig及reapableConnectionManager;其runOneIteration执行了reapableConnectionManager.closeIdleConnections;其scheduler方法创建的是fixedDelay Scheduler;其executor方法创建的是SingleThreadScheduledExecutor

ReapableConnectionManager

jest-common-6.3.1-sources.jar!/io/searchbox/client/config/idle/ReapableConnectionManager.java

public interface ReapableConnectionManager {
    void closeIdleConnections(long idleTimeout, TimeUnit unit);
}
  • ReapableConnectionManager接口定义了closeIdleConnections方法

HttpReapableConnectionManager

jest-6.3.1-sources.jar!/io/searchbox/client/config/idle/HttpReapableConnectionManager.java

public class HttpReapableConnectionManager implements ReapableConnectionManager {
    private final HttpClientConnectionManager connectionManager;
    private final NHttpClientConnectionManager nConnectionManager;

    public HttpReapableConnectionManager(HttpClientConnectionManager connectionManager, NHttpClientConnectionManager nConnectionManager) {
        if(connectionManager == null || nConnectionManager == null) throw new IllegalArgumentException();

        this.connectionManager = connectionManager;
        this.nConnectionManager = nConnectionManager;
    }

    @Override
    public void closeIdleConnections(long idleTimeout, TimeUnit unit) {
        connectionManager.closeIdleConnections(idleTimeout, unit);
        nConnectionManager.closeIdleConnections(idleTimeout, unit);
    }
}
  • HttpReapableConnectionManager实现了ReapableConnectionManager接口;其构造器要求输入connectionManager及nConnectionManager,二者不能同时为null;其closeIdleConnections方法分别执行了connectionManager.closeIdleConnections及nConnectionManager.closeIdleConnections

小结

  • IdleConnectionReaper继承了AbstractScheduledService,它的构造器接收clientConfig及reapableConnectionManager;其runOneIteration执行了reapableConnectionManager.closeIdleConnections;其scheduler方法创建的是fixedDelay Scheduler;其executor方法创建的是SingleThreadScheduledExecutor
  • ReapableConnectionManager接口定义了closeIdleConnections方法
  • HttpReapableConnectionManager实现了ReapableConnectionManager接口;其构造器要求输入connectionManager及nConnectionManager,二者不能同时为null;其closeIdleConnections方法分别执行了connectionManager.closeIdleConnections及nConnectionManager.closeIdleConnections

doc


以上所述就是小编给大家介绍的《聊聊jest的IdleConnectionReaper》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

设计原本

设计原本

Frederick P. Brooks, Jr. / InfoQ中文站、王海鹏、高博 / 机械工业出版社 / 2011-1-1 / 55.00元

无论是软件开发、工程还是建筑,有效的设计都是工作的核心。《设计原本:计算机科学巨匠Frederick P. Brooks的思考》将对设计过程进行深入分析,揭示进行有效和优雅设计的方法。 本书包含了多个行业设计者的特别领悟。Frederick P. Brooks, Jr.精确发现了所有设计项目中内在的不变因素,揭示 了进行优秀设计的过程和模式。通过与几十位优秀设计者的对话,以及他自己在几个设计......一起来看看 《设计原本》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具