调用 channelInactive 后的链式操作
chain operations after channelInactive called
我在 channelInactive 中有一些清理代码:
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
clean-up code
super.channelInactive(ctx);
}
有时候,我需要主动断开频道:
ChannelFuture f = channel.disconnect();
f.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) {
// the problem is the channelInactive might not be called yet
// how can I make sure channelInactive has been called?
do something but the clean-up code in channelInactive MUST be called
}
});
Netty 无法执行此操作。如果channel.disconnect不在netty的事件循环中调用,它会异步触发channelInactive之类的事件,所以disconnect之间没有顺序,它的相关事件会被触发。
AbstractChannelHandlerContext
中的代码:
static void invokeChannelInactive(final AbstractChannelHandlerContext next) {
EventExecutor executor = next.executor();
if (executor.inEventLoop()) {
next.invokeChannelInactive();
} else {
executor.execute(new Runnable() {
@Override
public void run() {
next.invokeChannelInactive();
}
});
}
}
我在 channelInactive 中有一些清理代码:
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
clean-up code
super.channelInactive(ctx);
}
有时候,我需要主动断开频道:
ChannelFuture f = channel.disconnect();
f.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) {
// the problem is the channelInactive might not be called yet
// how can I make sure channelInactive has been called?
do something but the clean-up code in channelInactive MUST be called
}
});
Netty 无法执行此操作。如果channel.disconnect不在netty的事件循环中调用,它会异步触发channelInactive之类的事件,所以disconnect之间没有顺序,它的相关事件会被触发。
AbstractChannelHandlerContext
中的代码:
static void invokeChannelInactive(final AbstractChannelHandlerContext next) {
EventExecutor executor = next.executor();
if (executor.inEventLoop()) {
next.invokeChannelInactive();
} else {
executor.execute(new Runnable() {
@Override
public void run() {
next.invokeChannelInactive();
}
});
}
}