Netty进阶之ChannelPoolMap源码解析

 更新时间:2023年11月16日 09:32:54   作者:立小研先森  
这篇文章主要介绍了Netty进阶之ChannelPoolMap源码解析,ChannelPoolMap是用来存储ChannelPool和指定key的一个集合Map,实际的应用场景就是服务器端是一个分布式集群服务,拥有多个配置地址,这样我们就可以配置多个服务地址,减轻单台服务器的压力,需要的朋友可以参考下

前言

ChannelPoolMap是用来存储ChannelPool和指定key的一个集合Map,实际的应用场景就是服务器端是一个分布式集群服务,拥有多个配置地址,这样我们就可以配置多个服务地址,减轻单台服务器的压力;Netty框架提供了ChannelPoolMap接口和AbstractChannelPoolMap抽象方法。

一、ChannelPoolMap接口源码分析

package io.netty.channel.pool;

/**
 * Allows to map {@link ChannelPool} implementations to a specific key.
 *
 * @param <K> the type of the key
 * @param <P> the type of the {@link ChannelPool}
 */
public interface ChannelPoolMap<K, P extends ChannelPool> {
    /**
     * Return the {@link ChannelPool} for the {@code code}. This will never return {@code null},
     * but create a new {@link ChannelPool} if non exists for they requested {@code key}.
     *
     * Please note that {@code null} keys are not allowed.
     */
    P get(K key);

    /**
     * Returns {@code true} if a {@link ChannelPool} exists for the given {@code key}.
     *
     * Please note that {@code null} keys are not allowed.
     */
    boolean contains(K key);
}

接口提供了两个方法,get方法用于获取指定key对应的ChannelPool,contains方法用来判定Map集合中是否存在指定key对应的ChannelPool。

二、AbstractChannelPoolMap抽象实现类

1.get方法分析

    private final ConcurrentMap<K, P> map = PlatformDependent.newConcurrentHashMap();

    @Override
    public final P get(K key) {
      //获取ChannelPool
        P pool = map.get(checkNotNull(key, "key"));
        if (pool == null) {
          //创建一个新的ChannelPool
            pool = newPool(key);
          //如果集合中存在ChannelPool,则返回老的ChannelPool对象
            P old = map.putIfAbsent(key, pool);
           //若果老的ChannelPool真实存在
            if (old != null) {
               //异步销毁新创建的ChannelPool
                // We need to destroy the newly created pool as we not use it.
                poolCloseAsyncIfSupported(pool);
                pool = old;
            }
        }
        return pool;
    }
  • 定义了一个ConcurrentMap类型的map类变量,用来存放key及其对应的ChannelPool;
  • 首先会从集合中获取ChannelPool,如果不存在则创建一个新的ChannelPool;
    /**
     * Called once a new {@link ChannelPool} needs to be created as non exists yet for the {@code key}.
     */
    protected abstract P newPool(K key);

newPool方法是一个抽象方法,需要用户自己实现ChannelPool的创建操作;

    /**
     * If the pool implementation supports asynchronous close, then use it to avoid a blocking close call in case
     * the ChannelPoolMap operations are called from an EventLoop.
     *
     * @param pool the ChannelPool to be closed
     */
    private static Future<Void> poolCloseAsyncIfSupported(ChannelPool pool) {
        if (pool instanceof SimpleChannelPool) {
            return ((SimpleChannelPool) pool).closeAsync();
        } else {
            try {
                pool.close();
                return GlobalEventExecutor.INSTANCE.newSucceededFuture(null);
            } catch (Exception e) {
                return GlobalEventExecutor.INSTANCE.newFailedFuture(e);
            }
        }
    }

异步关闭ChannelPool,如果是SimpleChannelPool的实现,则调用异步方法closeAsync;如果是其它实现,则调用close方法。

2.remove方法分析

    public final boolean remove(K key) {
      //移除指定key的ChannelPool
        P pool =  map.remove(checkNotNull(key, "key"));
        if (pool != null) {
          //如果移除成功,则异步的关闭ChannelPool,避免阻塞方法
            poolCloseAsyncIfSupported(pool);
            return true;
        }
        return false;
    }

到此这篇关于Netty进阶之ChannelPoolMap源码解析的文章就介绍到这了,更多相关ChannelPoolMap源码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 若依后端MyBatis改为MyBatis-Plus方式

    若依后端MyBatis改为MyBatis-Plus方式

    文章介绍了如何将MyBatis-Plus集成到RuoYi项目中,包括添加依赖、修改配置文件、重写MyBatis配置和修改代码生成器文件
    2024-11-11
  • SpringBoot概述及在idea中创建方式

    SpringBoot概述及在idea中创建方式

    SpringBoot提供了一种快速使用Spring的方式,基于约定大于配置的思想,可以让开发人员不必在配置与逻辑业务之间进行思维的切换,这篇文章主要介绍了SpringBoot概述及在idea中创建方式,需要的朋友可以参考下
    2022-09-09
  • Java中Jedis基本使用

    Java中Jedis基本使用

    Redis的Java实现的客户端,本文主要介绍了Java中Jedis基本使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-05-05
  • java使用链表实现约瑟夫环

    java使用链表实现约瑟夫环

    这篇文章主要为大家详细介绍了java使用链表实现约瑟夫环,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • SpringCloud Gateway跨域配置代码实例

    SpringCloud Gateway跨域配置代码实例

    这篇文章主要介绍了SpringCloud Gateway跨域配置代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • SpringBoot返回对象时,如何将Long类型转换为String

    SpringBoot返回对象时,如何将Long类型转换为String

    这篇文章主要介绍了SpringBoot返回对象时,实现将Long类型转换为String,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • 利用Java实现玩家打怪小游戏的完整过程

    利用Java实现玩家打怪小游戏的完整过程

    这篇文章主要介绍了如何使用Java创建一个简单的“打怪小游戏”,游戏中的角色分为法师、战士、BOSS和一个Team类,代码展示了如何通过面向对象编程来实现一个基本的战斗系统,需要的朋友可以参考下
    2024-12-12
  • Spring Boot自动配置实战指南

    Spring Boot自动配置实战指南

    本文深入探讨了Spring Boot自动配置这一核心特性,首先介绍了 Spring Boot在Java开发中的重要地位,以及自动配置的概念和重要性,它通过 “约定优于配置” 的理念,大大简化了开发过程,提高了开发效率 ,感兴趣的朋友跟随小编一起看看吧
    2026-04-04
  • Java实现数组翻转的实现代码

    Java实现数组翻转的实现代码

    这篇文章主要介绍了Java实现数组翻转的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • SpringCloud-Gateway转发WebSocket失败问题及解决

    SpringCloud-Gateway转发WebSocket失败问题及解决

    这篇文章主要介绍了SpringCloud-Gateway转发WebSocket失败问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09

最新评论