前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊jest的IdleConnectionReaper

聊聊jest的IdleConnectionReaper

原创
作者头像
code4it
发布2019-04-22 22:43:44
9720
发布2019-04-22 22:43:44
举报
文章被收录于专栏:码匠的流水账

本文主要研究一下jest的IdleConnectionReaper

IdleConnectionReaper

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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • IdleConnectionReaper
  • ReapableConnectionManager
  • HttpReapableConnectionManager
  • 小结
  • doc
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档