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信道通過(guò)調(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)文章
通過(guò)實(shí)例了解java checked和unchecked異常
這篇文章主要介紹了通過(guò)實(shí)例了解checked和unchecked異常,Java異常分為兩種類型,checked異常和unchecked異常,另一種叫法是異常和錯(cuò)誤。下面小編就帶大家來(lái)一起學(xué)習(xí)一下吧2019-06-06
IDEA利用自帶Axis工具和wsdl文件反向生成服務(wù)端客戶端代碼圖文詳解
這篇文章主要介紹了IDEA利用自帶Axis工具和wsdl文件反向生成服務(wù)端客戶端代碼詳細(xì)流程,在這里小編使用的是idea2021.1最新開(kāi)發(fā)工具,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-05-05
logback標(biāo)記日志過(guò)濾器MarkerFilter源碼解讀
這篇文章主要為大家介紹了logback標(biāo)記日志過(guò)濾器MarkerFilter源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
Java中LocalDate日期格式轉(zhuǎn)換(使用系統(tǒng)時(shí)區(qū))
本文主要介紹了Java中LocalDate日期格式轉(zhuǎn)換(使用系統(tǒng)時(shí)區(qū)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2007-02-02
Java SpringMVC 異常處理SimpleMappingExceptionResolver類詳解
這篇文章主要介紹了SpringMVC 異常處理SimpleMappingExceptionResolver類詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
Java使用Catcher捕獲異常的實(shí)現(xiàn)
本文主要介紹了Java使用Catcher捕獲異常的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
spring-AOP 及 AOP獲取request各項(xiàng)參數(shù)操作
這篇文章主要介紹了spring-AOP 及 AOP獲取request各項(xiàng)參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
基于Java實(shí)現(xiàn)PPT到PDF的高效轉(zhuǎn)換詳解
在日常開(kāi)發(fā)中,經(jīng)常會(huì)遇到將PPT文檔批量或單文件轉(zhuǎn)換為PDF的需求,本文將詳細(xì)介紹其使用流程、核心代碼與常見(jiàn)問(wèn)題解決方案,希望對(duì)大家有所幫助2025-11-11
Java數(shù)據(jù)結(jié)構(gòu)之樹(shù)和二叉樹(shù)的相關(guān)資料
這篇文章主要介紹了Java?數(shù)據(jù)結(jié)構(gòu)之樹(shù)和二叉樹(shù)相關(guān)資料,文中通過(guò)示例代碼和一些相關(guān)題目來(lái)做介紹,非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下!2023-01-01

