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