开发者

Netty中ChannelPoolHandler调用处理程序详解

开发者 https://www.devze.com 2023-11-19 11:20 出处:网络 作者: 立小研先森
目录ChannelPoolHandler调用处理程序一、ChannelPoolHandler源码解析二、AbstractChannelPoolHandler源码解析三、调用channelCreated方法四、获取Channel信道方法五、释放Channel信道方法ChannelPoolHandler调用处理
目录
  • ChannelPoolHandler调用处理程序
    • 一、ChannelPoolHandler源码解析
    • 二、AbstractChannelPoolHandler源码解析
    • 三、调用channelCreated方法
    • 四、获取Channel信道方法
    • 五、释放Channel信道方法

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 {

    /**python
     * 无操作实现方法,可以被子类覆盖
     *
     */
    @Override
    public void channelAcquired(@SuppressWarnings("unused") Channel ch) throws Exception {
        // NOOP
    }

    /**
     * 无操作实现方法,可以被子类覆盖
http://www.devze.com     */
    @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 = releasphpeHealthCheck;
        // 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<Cjavascripthannel> 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) thr编程客栈ows Exception {
        if (offerChannel(channel)) {
            handler.channelReleased(channel);
            promise.setSuccess(null);
        } else {
            closeAndFail(channel, new ChannelPoolFullException(), promise);
        }
    }

到此这篇关于Netty中ChannelPoolHandler调用处理程序详解的文章就介绍到这了,更多相关ChannelPoolHandler调用处理程序内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号