聊聊jest的IdleConnectionReaper

本文主要研究一下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

转载于:https://juejin.im/post/5cbdd1885188250a744e6481

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值