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调用处理程序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Java Scanner类用法及nextLine()产生的换行符问题实例分析
这篇文章主要介绍了Java Scanner类用法及nextLine()产生的换行符问题,结合实例形式分析了Scanner类功能、hasNextInt()和nextInt()方法使用及nextLine()产生的换行符问题解决方法,需要的朋友可以参考下2019-03-03java Nio使用NioSocket客户端与服务端交互实现方式
这篇文章主要介绍了java Nio使用 NioSocket 客户端与服务端交互实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-06-06JDBC中PreparedStatement详解以及应用场景实例介绍
PreparedStatement对象代表的是一个预编译的SQL语句,用它提供的setter方法可以传入查询的变量,这篇文章主要给大家介绍了关于JDBC中PreparedStatement详解以及应用场景实例介绍的相关资料,需要的朋友可以参考下2024-02-02Maven编译错误:程序包com.sun.*包不存在的三种解决方案
J2SE中的类大致可以划分为以下的各个包:java.*,javax.*,org.*,sun.*,本文文章主要介绍了maven编译错误:程序包com.sun.xml.internal.ws.spi不存在的解决方案,感兴趣的可以了解一下2024-02-02
最新评论