【Kafka】kafka AdminClient 闲时关闭连接

394 篇文章 629 订阅 ¥99.90 ¥99.00

在这里插入图片描述

1.概述

原文:https://www.cnblogs.com/allenwas3/p/10289039.html

AdminClient 类提供了创建、删除 topic 的 api。

在项目中创建了一个 AdminClient 对象,每次创建 topic 时,调用

org.apache.kafka.clients.admin.AdminClient#createTopics
如果长时间不使用这个对象,客户端与 broker 之间的连接会被关掉,相关的参数:

connections.max.idle.ms
这个最大空闲参数在 broker 和 客户端都可以配置,即 broker 和客户端都会关闭空闲太久的连接。

org.apache.kafka.common.network.Selector#maybeCloseOldestConnection

 private void maybeCloseOldestConnection(long currentTimeNanos) {
        if (idleExpiryManager == null)
            return;

        Map.Entry<String, Long> expiredConnection = idleExpiryManager.pollExpiredConnection(currentTimeNanos);
        if (expiredConnection != null) {
            String connectionId = expiredConnection.getKey();
            KafkaChannel channel = this.channels.get(connectionId);
            if (channel != null) {
                if (log.isTraceEnabled())
                    log.trace("About to close the idle connection from {} due to being idle for {} millis",
                            connectionId, (currentTimeNanos - expiredConnection.getValue()) / 1000 / 1000);
                channel.state(ChannelState.EXPIRED);
                close(channel, CloseMode.GRACEFUL);
            }
        }
    }

org.apache.kafka.common.network.Selector.IdleExpiryManager#pollExpiredConnection

lruConnections 是 LinkedHashMap 类型,可以按照插入和访问顺序进行排序,这里是按访问顺序进行排序,访问过的顺序放到双向链表的结尾。

public Map.Entry<String, Long> pollExpiredConnection(long currentTimeNanos) {
    if (currentTimeNanos <= nextIdleCloseCheckTime)
        return null;

    if (lruConnections.isEmpty()) {
        nextIdleCloseCheckTime = currentTimeNanos + connectionsMaxIdleNanos;
        return null;
    }

    Map.Entry<String, Long> oldestConnectionEntry = lruConnections.entrySet().iterator().next();
    Long connectionLastActiveTime = oldestConnectionEntry.getValue();
    nextIdleCloseCheckTime = connectionLastActiveTime + connectionsMaxIdleNanos;

    if (currentTimeNanos > nextIdleCloseCheckTime)
        return oldestConnectionEntry;
    else
        return null;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值