最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Netty進(jìn)階之ChannelPoolMap源碼解析

 更新時間:2023年11月16日 09:32:54   作者:立小研先森  
這篇文章主要介紹了Netty進(jìn)階之ChannelPoolMap源碼解析,ChannelPoolMap是用來存儲ChannelPool和指定key的一個集合Map,實(shí)際的應(yīng)用場景就是服務(wù)器端是一個分布式集群服務(wù),擁有多個配置地址,這樣我們就可以配置多個服務(wù)地址,減輕單臺服務(wù)器的壓力,需要的朋友可以參考下

前言

ChannelPoolMap是用來存儲ChannelPool和指定key的一個集合Map,實(shí)際的應(yīng)用場景就是服務(wù)器端是一個分布式集群服務(wù),擁有多個配置地址,這樣我們就可以配置多個服務(wù)地址,減輕單臺服務(wù)器的壓力;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對應(yīng)的ChannelPool,contains方法用來判定Map集合中是否存在指定key對應(yīng)的ChannelPool。

二、AbstractChannelPoolMap抽象實(shí)現(xiàn)類

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) {
          //創(chuàng)建一個新的ChannelPool
            pool = newPool(key);
          //如果集合中存在ChannelPool,則返回老的ChannelPool對象
            P old = map.putIfAbsent(key, pool);
           //若果老的ChannelPool真實(shí)存在
            if (old != null) {
               //異步銷毀新創(chuàng)建的ChannelPool
                // We need to destroy the newly created pool as we not use it.
                poolCloseAsyncIfSupported(pool);
                pool = old;
            }
        }
        return pool;
    }
  • 定義了一個ConcurrentMap類型的map類變量,用來存放key及其對應(yīng)的ChannelPool;
  • 首先會從集合中獲取ChannelPool,如果不存在則創(chuàng)建一個新的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方法是一個抽象方法,需要用戶自己實(shí)現(xiàn)ChannelPool的創(chuàng)建操作;

    /**
     * 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);
            }
        }
    }

異步關(guān)閉ChannelPool,如果是SimpleChannelPool的實(shí)現(xiàn),則調(diào)用異步方法closeAsync;如果是其它實(shí)現(xiàn),則調(diào)用close方法。

2.remove方法分析

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

到此這篇關(guān)于Netty進(jìn)階之ChannelPoolMap源碼解析的文章就介紹到這了,更多相關(guān)ChannelPoolMap源碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

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

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

    文章介紹了如何將MyBatis-Plus集成到RuoYi項(xiàng)目中,包括添加依賴、修改配置文件、重寫MyBatis配置和修改代碼生成器文件
    2024-11-11
  • SpringBoot概述及在idea中創(chuàng)建方式

    SpringBoot概述及在idea中創(chuàng)建方式

    SpringBoot提供了一種快速使用Spring的方式,基于約定大于配置的思想,可以讓開發(fā)人員不必在配置與邏輯業(yè)務(wù)之間進(jìn)行思維的切換,這篇文章主要介紹了SpringBoot概述及在idea中創(chuàng)建方式,需要的朋友可以參考下
    2022-09-09
  • Java中Jedis基本使用

    Java中Jedis基本使用

    Redis的Java實(shí)現(xiàn)的客戶端,本文主要介紹了Java中Jedis基本使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • java使用鏈表實(shí)現(xiàn)約瑟夫環(huán)

    java使用鏈表實(shí)現(xiàn)約瑟夫環(huán)

    這篇文章主要為大家詳細(xì)介紹了java使用鏈表實(shí)現(xiàn)約瑟夫環(huán),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • SpringCloud Gateway跨域配置代碼實(shí)例

    SpringCloud Gateway跨域配置代碼實(shí)例

    這篇文章主要介紹了SpringCloud Gateway跨域配置代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • SpringBoot返回對象時,如何將Long類型轉(zhuǎn)換為String

    SpringBoot返回對象時,如何將Long類型轉(zhuǎn)換為String

    這篇文章主要介紹了SpringBoot返回對象時,實(shí)現(xiàn)將Long類型轉(zhuǎn)換為String,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 利用Java實(shí)現(xiàn)玩家打怪小游戲的完整過程

    利用Java實(shí)現(xiàn)玩家打怪小游戲的完整過程

    這篇文章主要介紹了如何使用Java創(chuàng)建一個簡單的“打怪小游戲”,游戲中的角色分為法師、戰(zhàn)士、BOSS和一個Team類,代碼展示了如何通過面向?qū)ο缶幊虂韺?shí)現(xiàn)一個基本的戰(zhàn)斗系統(tǒng),需要的朋友可以參考下
    2024-12-12
  • Spring Boot自動配置實(shí)戰(zhàn)指南

    Spring Boot自動配置實(shí)戰(zhàn)指南

    本文深入探討了Spring Boot自動配置這一核心特性,首先介紹了 Spring Boot在Java開發(fā)中的重要地位,以及自動配置的概念和重要性,它通過 “約定優(yōu)于配置” 的理念,大大簡化了開發(fā)過程,提高了開發(fā)效率 ,感興趣的朋友跟隨小編一起看看吧
    2026-04-04
  • Java實(shí)現(xiàn)數(shù)組翻轉(zhuǎn)的實(shí)現(xiàn)代碼

    Java實(shí)現(xiàn)數(shù)組翻轉(zhuǎn)的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Java實(shí)現(xiàn)數(shù)組翻轉(zhuǎn)的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • SpringCloud-Gateway轉(zhuǎn)發(fā)WebSocket失敗問題及解決

    SpringCloud-Gateway轉(zhuǎn)發(fā)WebSocket失敗問題及解決

    這篇文章主要介紹了SpringCloud-Gateway轉(zhuǎn)發(fā)WebSocket失敗問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09

最新評論

龙川县| 京山县| 静乐县| 临沭县| 屯昌县| 和政县| 桐乡市| 松溪县| 绥江县| 社旗县| 荔波县| 南漳县| 武乡县| 梨树县| 平塘县| 杭州市| 建始县| 丹东市| 酉阳| 乐昌市| 安阳市| 枣强县| 渭南市| 偃师市| 仁化县| 分宜县| 定陶县| 马公市| 太康县| 布拖县| 抚远县| 定安县| 和硕县| 龙胜| 怀仁县| 和平县| 北京市| 建昌县| 缙云县| 紫金县| 昭平县|