Loading [MathJax]/jax/input/TeX/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >聊聊jest的IdleConnectionReaper

聊聊jest的IdleConnectionReaper

原创
作者头像
code4it
发布于 2019-04-22 14:43:44
发布于 2019-04-22 14:43:44
1K00
代码可运行
举报
文章被收录于专栏:码匠的流水账码匠的流水账
运行总次数:0
代码可运行

本文主要研究一下jest的IdleConnectionReaper

IdleConnectionReaper

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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
代码运行次数:0
运行
AI代码解释
复制
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
代码运行次数:0
运行
AI代码解释
复制
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 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
聊聊jest的NodeChecker
jest-common-6.3.1-sources.jar!/io/searchbox/client/config/discovery/NodeChecker.java
code4it
2019/04/21
1.1K0
聊聊jest的NodeChecker
聊聊httpclient的evict操作
org/apache/http/impl/client/HttpClientBuilder.java
code4it
2023/10/06
5230
Elasticsearch-Jest 配置ES集群&源码解读
直接访问 https://github.com/searchbox-io/Jest ,把源码拉下来
小小工匠
2021/08/17
8880
高并发场景下的httpClient优化使用
1.背景 我们有个业务,会调用其他部门提供的一个基于http的服务,日调用量在千万级别。使用了httpclient来完成业务。之前因为qps上不去,就看了一下业务代码,并做了一些优化,记录在这里。 先对比前后:优化之前,平均执行时间是250ms;优化之后,平均执行时间是80ms,降低了三分之二的消耗,容器不再动不动就报警线程耗尽了,清爽~ 2.分析 项目的原实现比较粗略,就是每次请求时初始化一个httpclient,生成一个httpPost对象,执行,然后从返回结果取出entity,保存成一个字符串,最后显
老白
2018/03/19
6.9K0
聊聊httpclient的evict操作
org/apache/http/impl/client/HttpClientBuilder.java
code4it
2023/10/06
3070
聊聊httpclient的evict操作
聊聊dubbo的NettyServer
dubbo-2.7.3/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractServer.java
code4it
2019/08/30
8770
聊聊dubbo的NettyServer
聊聊spring的async注解
这里讨论一下getAsyncExecutor这里定义null的情况。 spring-context-4.3.9.RELEASE-sources.jar!/org/springframework/scheduling/annotation/AbstractAsyncConfiguration.java
code4it
2018/09/17
1.7K0
聊聊Elasticsearch的EsThreadPoolExecutor
本文主要研究一下Elasticsearch的EsThreadPoolExecutor
code4it
2019/06/02
5270
聊聊Elasticsearch的EsThreadPoolExecutor
聊聊springboot jest autoconfigure
spring-boot-autoconfigure-2.1.4.RELEASE-sources.jar!/org/springframework/boot/autoconfigure/elasticsearch/jest/JestProperties.java
code4it
2019/04/20
1.3K0
聊聊springboot jest autoconfigure
聊聊apache gossip的ActiveGossiper
incubator-retired-gossip/gossip-base/src/main/java/org/apache/gossip/manager/AbstractActiveGossiper.java
code4it
2019/05/04
4590
聊聊apache gossip的ActiveGossiper
聊聊hystrix的queueSizeRejectionThreshold参数
本文主要研究一下hystrix的queueSizeRejectionThreshold参数
code4it
2018/09/17
1.5K0
聊聊ExecutorService的监控
metrics-core-4.0.2-sources.jar!/com/codahale/metrics/InstrumentedExecutorService.java
code4it
2018/10/18
1.3K0
聊聊flink的ScheduledExecutor
java.base/java/util/concurrent/Executor.java
code4it
2019/03/13
1.1K0
聊聊flink的ScheduledExecutor
聊聊dubbo的ConnectionOrderedDispatcher
本文主要研究一下dubbo的ConnectionOrderedDispatcher
code4it
2019/09/01
6320
聊聊dubbo的ConnectionOrderedDispatcher
HttpComponents HttpClient连接池(6)-连接清理
在上一篇文章里我们介绍了 httpclient 连接池中连接的可用性检查,在这里我们主要介绍空闲 http 连接的清理。对于连接池中的连接基本都是复用的,其中避免不了 server 端主动关闭连接,这个时候取出的连接自然是不可用的,当然可以通过上一篇文章中的可用性检查避免。但同时 httpclient 连接池也提供了 http 连接的清理策略,用来对连接进行清除。
TA码字
2020/04/01
3.4K0
httpclient参数配置
默认的话,是从response里头读timeout参数的,没有读到则设置为-1,这个代表无穷,这样设置是有点问题了,如果是https链接的话,则可能会经常报
code4it
2018/09/17
7.1K0
聊聊spring cloud的AbstractLoadBalancingClient
本文主要研究一下spring cloud的AbstractLoadBalancingClient
code4it
2018/09/17
1.3K0
聊聊claudb的NotificationManager
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/event/NotificationManager.java
code4it
2020/09/03
1730
聊聊claudb的NotificationManager
聊聊jedis的testWhileIdle
org/apache/commons/pool2/impl/GenericObjectPool.java
code4it
2023/09/23
2910
聊聊spring cloud eureka的instanceEnabledOnit属性
本文主要研究下spring cloud eureka的instanceEnabledOnit属性
code4it
2018/09/17
7410
相关推荐
聊聊jest的NodeChecker
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验