Netty中ChannelPoolHandler調(diào)用處理程序詳解
ChannelPoolHandler調(diào)用處理程序
一、ChannelPoolHandler源碼解析
public interface ChannelPoolHandler { /** * Channel信道被ChannelPool#release(Channel)或ChannelPool#release(Channel, Promise)方法 * 調(diào)用,并釋放會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時將會被調(diào)用一次 */ void channelCreated(Channel ch) throws Exception; }
二、AbstractChannelPoolHandler源碼解析
public abstract class AbstractChannelPoolHandler implements ChannelPoolHandler { /** * 無操作實現(xiàn)方法,可以被子類覆蓋 * */ @Override public void channelAcquired(@SuppressWarnings("unused") Channel ch) throws Exception { // NOOP } /** * 無操作實現(xiàn)方法,可以被子類覆蓋 */ @Override public void channelReleased(@SuppressWarnings("unused") Channel ch) throws Exception { // NOOP } }
AbstractChannelPoolHandler抽象類是ChannelPoolHandler的框架實現(xiàn)類,其實現(xiàn)了兩個無任何操作的方法。
三、調(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Spring Cache使用Redisson分布式鎖解決緩存擊穿問題
本文主要介紹了詳解Spring Cache使用Redisson分布式鎖解決緩存擊穿問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Java操作IO對象流進(jìn)行數(shù)據(jù)的讀寫
這篇文章主要介紹了Java操作IO對象流進(jìn)行數(shù)據(jù)的讀寫,本文通過例子逐步介紹了java如何操作IO流,和文字解析,需要的朋友可以參考下2021-07-07Java String index out of range:100錯誤解決方案詳解
這篇文章主要介紹了Java String index out of range:100錯誤解決方案詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08解決BufferedReader.readLine()遇見的坑
這篇文章主要介紹了解決BufferedReader.readLine()遇見的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12Springboot+Jackson自定義注解數(shù)據(jù)脫敏的項目實踐
數(shù)據(jù)脫敏可以對敏感數(shù)據(jù)比如 手機號、銀行卡號等信息進(jìn)行轉(zhuǎn)換或者修改,本文主要介紹了Springboot+Jackson?自定義注解數(shù)據(jù)脫敏,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08