添加ChannelHandler到ChannelPipeline中
在上节中介绍了ChannelPipeline
,其中常用的方法是addLast(handler)
将各类ChannelHandler
实例添加到Channel
的 ChannelPipeline
中。下面我们来看addLast(handler)
方法的实现:
// 接受的第一个参数是事件执行组,当Handler的处理比较耗时时,可以指定其回调方法在事件执行组EventExecutorGroup中执行
@Override
public final ChannelPipeline addLast(EventExecutorGroup group, String name, ChannelHandler handler) {
final AbstractChannelHandlerContext newCtx;
synchronized (this) {
checkMultiplicity(handler);
// 1. 创建ChannelHandlerContext
newCtx = newContext(group, filterName(name, handler), handler);
// 2. 将ChannelHandlerContext加入到ChannelPipeline中
addLast0(newCtx);
if (!registered) {
newCtx.setAddPending();
callHandlerCallbackLater(newCtx, true);
return this;
}
EventExecutor executor = newCtx.executor();
if (!executor.inEventLoop()) {
newCtx.setAddPending();
executor.execute(new Runnable() {
@Override
public void run() {
callHandlerAdded0(newCtx);
}
});
return this;
}
}
// 3. 调用ChannelHandler的handlerAdded方法
callHandlerAdded0(newCtx);
return this;
}
addLast
方法的第一个参数EventExecutorGroup
用来指定当Hander执行耗时较长的任务时,让Handlera的回调方法在EventExecutorGroup
中执行,如new DefaultEventExecutorGroup(16)
。newContext
方法会新建一个DefaultChannelHandlerContext
对象,它持有传入的Handler。addLast0
方法将上一步创建的DefaultChannelHandlerContext
对象加入到ChannelPipeline
的链表中。-
最后调用
callHandlerAdded0
方法,执行刚刚被添加的ChannelHander
对象的handlerAdded
回调方法。private void callHandlerAdded0(final AbstractChannelHandlerContext ctx) { try { ctx.setAddComplete(); ctx.handler().handlerAdded(ctx); } catch (Throwable t) { ...... } }
总结:真正添加到
ChannelPipeline
中的是ChannelHandlerContext
对象,它封装并持有ChannelHandler
对象。添加完成后,会调用ChannelHandler
的handlerAdded
回调方法
ChannelInitializer
ChannelInitializer
是一类特殊的ChannelHandler
,当Channel
注册到EventLoop
时,可以初始化Channel
。
在ServerBootStrap
绑定端口号的过程中,在initAndRegister
方法中:一个ChannelInitializer
被加入到ChannelPipeline
中。
void init(Channel channel) throws Exception {
//以上省略......
ChannelPipeline p = channel.pipeline();
final EventLoopGroup currentChildGroup = childGroup;
final ChannelHandler currentChildHandler = childHandler;
//省略部分代码......
p.addLast(new ChannelInitializer<Channel>() {
@Override
public void initChannel(final Channel ch) throws Exception {
final ChannelPipeline pipeline = ch.pipeline();
ChannelHandler handler = config.handler();
if (handler != null) {
pipeline.addLast(handler);
}
ch.eventLoop().execute(new Runnable() {
@Override
public void run() {
pipeline.addLast(new ServerBootstrapAcceptor(
ch, currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
}
});
}
});
}
更加本文第一部分描述,当一个ChannelHandler
被加入到ChannelPipeline
里后,会调用它的handlerAdded
回调方法。因此我们来看一下ChannelInitializer
的回调方法。
public abstract class ChannelInitializer<C extends Channel> extends ChannelInboundHandlerAdapter {
protected abstract void initChannel(C ch) throws Exception;
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
if (ctx.channel().isRegistered()) {
initChannel(ctx);
}
}
@SuppressWarnings("unchecked")
private boolean initChannel(ChannelHandlerContext ctx) throws Exception {
if (initMap.putIfAbsent(ctx, Boolean.TRUE) == null) {
try {
// 1. 初始化Channel
initChannel((C) ctx.channel());
} catch (Throwable cause) {
exceptionCaught(ctx, cause);
} finally {
// 2. 从ChannelPipeline中移除
remove(ctx);
}
return true;
}
return false;
}
private void remove(ChannelHandlerContext ctx) {
try {
ChannelPipeline pipeline = ctx.pipeline();
if (pipeline.context(this) != null) {
pipeline.remove(this);
}
} finally {
initMap.remove(ctx);
}
}
}
从以上代码可以看到,handlerAdded
方法首先调用initChannel
方法用来添加各种ChannelHandler
实例,然后在finally
中调用remove
,把当前ChannelInitializer
从ChannelPipeline
中移除,因此ChannelInitializer
可以理解为是一次性的。