欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

前言

ChannelPoolMap是用來(lái)存儲(chǔ)ChannelPool和指定key的一個(gè)集合Map,實(shí)際的應(yīng)用場(chǎng)景就是服務(wù)器端是一個(gè)分布式集群服務(wù),擁有多個(gè)配置地址,這樣我們就可以配置多個(gè)服務(wù)地址,減輕單臺(tái)服務(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);
}

接口提供了兩個(gè)方法,get方法用于獲取指定key對(duì)應(yīng)的ChannelPool,contains方法用來(lái)判定Map集合中是否存在指定key對(duì)應(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)建一個(gè)新的ChannelPool
            pool = newPool(key);
          //如果集合中存在ChannelPool,則返回老的ChannelPool對(duì)象
            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;
    }
  • 定義了一個(gè)ConcurrentMap類型的map類變量,用來(lái)存放key及其對(duì)應(yīng)的ChannelPool;
  • 首先會(huì)從集合中獲取ChannelPool,如果不存在則創(chuàng)建一個(gè)新的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方法是一個(gè)抽象方法,需要用戶自己實(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java模仿windows計(jì)算器示例

    java模仿windows計(jì)算器示例

    這篇文章主要介紹了java模仿windows計(jì)算器示例,需要的朋友可以參考下
    2014-05-05
  • Java并發(fā)線程之線程池的知識(shí)總結(jié)

    Java并發(fā)線程之線程池的知識(shí)總結(jié)

    這篇文章主要介紹了Java并發(fā)線程之線程池的知識(shí)總結(jié),幫助大家更好的理解和學(xué)習(xí)Java并發(fā)線程的相關(guān)內(nèi)容,感興趣的朋友可以了解下
    2021-01-01
  • Java實(shí)現(xiàn)獲取指定個(gè)數(shù)的不同隨機(jī)數(shù)

    Java實(shí)現(xiàn)獲取指定個(gè)數(shù)的不同隨機(jī)數(shù)

    今天小編就為大家分享一篇關(guān)于Java實(shí)現(xiàn)獲取指定個(gè)數(shù)的不同隨機(jī)數(shù),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • Java數(shù)組看這篇就夠了

    Java數(shù)組看這篇就夠了

    這篇文章主要介紹了Java數(shù)組的詳細(xì)解釋,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下,希望能夠給你帶來(lái)幫助
    2021-09-09
  • Java實(shí)現(xiàn)圖片文件上傳

    Java實(shí)現(xiàn)圖片文件上傳

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)圖片文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • SpringBoot整合liquibase及l(fā)iquibase生成初始化腳本的方式

    SpringBoot整合liquibase及l(fā)iquibase生成初始化腳本的方式

    這篇文章主要介紹了SpringBoot整合liquibase的相關(guān)資料,文中給大家介紹了liquibase生成初始化腳本的兩種方式,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • 關(guān)于Springboot的日志配置

    關(guān)于Springboot的日志配置

    Spring Boot默認(rèn)使用LogBack日志系統(tǒng),如果不需要更改為其他日志系統(tǒng)如Log4j2等,則無(wú)需多余的配置,LogBack默認(rèn)將日志打印到控制臺(tái)上,需要的朋友可以參考下
    2023-05-05
  • java并發(fā)編程JUC CountDownLatch線程同步

    java并發(fā)編程JUC CountDownLatch線程同步

    這篇文章主要介紹CountDownLatch是什么、CountDownLatch 如何工作、CountDownLatch 的代碼例子來(lái)展開對(duì)java并發(fā)編程JUC CountDownLatch線程同步,需要的朋友可以參考下面文章內(nèi)容
    2021-09-09
  • java中靜態(tài)變量和實(shí)例變量的區(qū)別詳細(xì)介紹

    java中靜態(tài)變量和實(shí)例變量的區(qū)別詳細(xì)介紹

    本篇文章介紹了,java中靜態(tài)變量和實(shí)例變量的區(qū)別。需要的朋友參考下
    2013-05-05
  • openFeign服務(wù)之間調(diào)用保持請(qǐng)求頭信息處理方式

    openFeign服務(wù)之間調(diào)用保持請(qǐng)求頭信息處理方式

    這篇文章主要介紹了openFeign服務(wù)之間調(diào)用保持請(qǐng)求頭信息處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06

最新評(píng)論