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中创建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处理程序的创建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调用处理程序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
详解Spring Cache使用Redisson分布式锁解决缓存击穿问题
本文主要介绍了详解Spring Cache使用Redisson分布式锁解决缓存击穿问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2022-04-04
Java String index out of range:100错误解决方案详解
这篇文章主要介绍了Java String index out of range:100错误解决方案详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下2021-08-08
解决BufferedReader.readLine()遇见的坑
这篇文章主要介绍了解决BufferedReader.readLine()遇见的坑,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-12-12
Springboot+Jackson自定义注解数据脱敏的项目实践
数据脱敏可以对敏感数据比如 手机号、银行卡号等信息进行转换或者修改,本文主要介绍了Springboot+Jackson 自定义注解数据脱敏,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2023-08-08


最新评论