Netty中ChannelPoolHandler調(diào)用處理程序詳解
ChannelPoolHandler調(diào)用處理程序
一、ChannelPoolHandler源碼解析
public interface ChannelPoolHandler { /** * Channel信道被ChannelPool#release(Channel)或ChannelPool#release(Channel, Promise)方法 * 調(diào)用,并釋放會(huì)ChannelPool連接池, */ void channelReleased(Channel ch) throws Exception; /** * Channel信道通過調(diào)用ChannelPool#acquire()或ChannelPool#acquire(Promise)方法獲取 */ void channelAcquired(Channel ch) throws Exception; /** * 在ChannelPool中創(chuàng)建Channel時(shí)將會(huì)被調(diào)用一次 */ void channelCreated(Channel ch) throws Exception; }
二、AbstractChannelPoolHandler源碼解析
public abstract class AbstractChannelPoolHandler implements ChannelPoolHandler { /** * 無(wú)操作實(shí)現(xiàn)方法,可以被子類覆蓋 * */ @Override public void channelAcquired(@SuppressWarnings("unused") Channel ch) throws Exception { // NOOP } /** * 無(wú)操作實(shí)現(xiàn)方法,可以被子類覆蓋 */ @Override public void channelReleased(@SuppressWarnings("unused") Channel ch) throws Exception { // NOOP } }
AbstractChannelPoolHandler抽象類是ChannelPoolHandler的框架實(shí)現(xiàn)類,其實(shí)現(xiàn)了兩個(gè)無(wú)任何操作的方法。
三、調(diào)用channelCreated方法
SimpleChannelPool#SimpleChannelPool構(gòu)造函數(shù)中調(diào)用channelCreated方法
public SimpleChannelPool(Bootstrap bootstrap, final ChannelPoolHandler handler, ChannelHealthChecker healthCheck, boolean releaseHealthCheck, boolean lastRecentUsed) { this.handler = checkNotNull(handler, "handler"); this.healthCheck = checkNotNull(healthCheck, "healthCheck"); this.releaseHealthCheck = releaseHealthCheck; // Clone the original Bootstrap as we want to set our own handler this.bootstrap = checkNotNull(bootstrap, "bootstrap").clone(); this.bootstrap.handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { assert ch.eventLoop().inEventLoop(); //此處調(diào)用ChannelPoolHandler處理程序的創(chuàng)建Channel信道方法 handler.channelCreated(ch); } }); this.lastRecentUsed = lastRecentUsed; }
四、獲取Channel信道方法
SimpleChannelPool#notifyConnect方法中調(diào)用channelAcquired獲取Channel信道方法
private void notifyConnect(ChannelFuture future, Promise<Channel> promise) { Channel channel = null; try { if (future.isSuccess()) { channel = future.channel(); //調(diào)用獲取Channel信道方法 handler.channelAcquired(channel); if (!promise.trySuccess(channel)) { // Promise was completed in the meantime (like cancelled), just release the channel again release(channel); } } else { promise.tryFailure(future.cause()); } } catch (Throwable cause) { closeAndFail(channel, cause, promise); } }
五、釋放Channel信道方法
SimpleChannelPool#releaseAndOffer和SimpleChannelPool#releaseAndOffer調(diào)用channelReleased釋放Channel信道方法
private void releaseAndOfferIfHealthy(Channel channel, Promise<Void> promise, Future<Boolean> future) { try { if (future.getNow()) { //channel turns out to be healthy, offering and releasing it. releaseAndOffer(channel, promise); } else { //channel not healthy, just releasing it. handler.channelReleased(channel); promise.setSuccess(null); } } catch (Throwable cause) { closeAndFail(channel, cause, promise); } } private void releaseAndOffer(Channel channel, Promise<Void> promise) throws Exception { if (offerChannel(channel)) { handler.channelReleased(channel); promise.setSuccess(null); } else { closeAndFail(channel, new ChannelPoolFullException(), promise); } }
到此這篇關(guān)于Netty中ChannelPoolHandler調(diào)用處理程序詳解的文章就介紹到這了,更多相關(guān)ChannelPoolHandler調(diào)用處理程序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實(shí)現(xiàn)API接口的完整代碼
這篇文章主要給大家介紹了關(guān)于SpringBoot實(shí)現(xiàn)API接口的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10詳解Spring Cache使用Redisson分布式鎖解決緩存擊穿問題
本文主要介紹了詳解Spring Cache使用Redisson分布式鎖解決緩存擊穿問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Java操作IO對(duì)象流進(jìn)行數(shù)據(jù)的讀寫
這篇文章主要介紹了Java操作IO對(duì)象流進(jìn)行數(shù)據(jù)的讀寫,本文通過例子逐步介紹了java如何操作IO流,和文字解析,需要的朋友可以參考下2021-07-07Java String index out of range:100錯(cuò)誤解決方案詳解
這篇文章主要介紹了Java String index out of range:100錯(cuò)誤解決方案詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08java并發(fā)訪問重復(fù)請(qǐng)求過濾問題
本篇文章給大家分享了關(guān)于java并發(fā)訪問重復(fù)請(qǐng)求過濾的相關(guān)問題以及解決方法,對(duì)此有需要的朋友參考學(xué)習(xí)下。2018-05-05解決BufferedReader.readLine()遇見的坑
這篇文章主要介紹了解決BufferedReader.readLine()遇見的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Springboot+Jackson自定義注解數(shù)據(jù)脫敏的項(xiàng)目實(shí)踐
數(shù)據(jù)脫敏可以對(duì)敏感數(shù)據(jù)比如 手機(jī)號(hào)、銀行卡號(hào)等信息進(jìn)行轉(zhuǎn)換或者修改,本文主要介紹了Springboot+Jackson?自定義注解數(shù)據(jù)脫敏,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08