原文:https://wangwei.one/posts/netty-pipeline-source-analyse-2.html
前面 ,我們分析了Netty Pipeline的初始化及節(jié)點(diǎn)添加與刪除邏輯。接下來(lái),我們將來(lái)分析Pipeline的事件傳播機(jī)制。
Netty版本:4.1.30
inBound事件傳播
示例
我們通過(guò)下面這個(gè)例子來(lái)演示Netty Pipeline的事件傳播機(jī)制。
public class NettyPipelineInboundExample {
public static void main(String[] args) {
EventLoopGroup group = new NioEventLoopGroup(1);
ServerBootstrap strap = new ServerBootstrap();
strap.group(group)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(8888))
.childOption(ChannelOption.TCP_NODELAY, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new InboundHandlerA());
ch.pipeline().addLast(new InboundHandlerB());
ch.pipeline().addLast(new InboundHandlerC());
}
});
try {
ChannelFuture future = strap.bind().sync();
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
}
class InboundHandlerA extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("InboundHandler A : " + msg);
// 傳播read事件到下一個(gè)channelhandler
ctx.fireChannelRead(msg);
}
}
class InboundHandlerB extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("InboundHandler B : " + msg);
// 傳播read事件到下一個(gè)channelhandler
ctx.fireChannelRead(msg);
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
// channel激活,觸發(fā)channelRead事件,從pipeline的heandContext節(jié)點(diǎn)開(kāi)始往下傳播
ctx.channel().pipeline().fireChannelRead("Hello world");
}
}
class InboundHandlerC extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("InboundHandler C : " + msg);
// 傳播read事件到下一個(gè)channelhandler
ctx.fireChannelRead(msg);
}
}
通過(guò) telnet 來(lái)連接上面啟動(dòng)好的netty服務(wù),觸發(fā)channel active事件:
$ telnet 127.0.0.1 8888
按照InboundHandlerA、InboundHandlerB、InboundHandlerC的添加順序,控制臺(tái)輸出如下信息:
InboundHandler A : Hello world
InboundHandler B : Hello world
InboundHandler C : Hello world
若是調(diào)用它們的添加順序,則會(huì)輸出對(duì)應(yīng)順序的信息,e.g:
...
ch.pipeline().addLast(new InboundHandlerB());
ch.pipeline().addLast(new InboundHandlerA());
ch.pipeline().addLast(new InboundHandlerC());
...
輸出如下信息:
InboundHandler B : Hello world
InboundHandler A : Hello world
InboundHandler C : Hello world
源碼分析
強(qiáng)烈建議 下面的流程,自己通過(guò)IDE的Debug模式來(lái)分析
待netty啟動(dòng)成功,通過(guò)telnet連接到netty,然后通過(guò)telnet終端輸入任意字符(這一步才開(kāi)啟Debug模式),進(jìn)入Debug模式。
觸發(fā)channel read事件,從下面的入口開(kāi)始調(diào)用
public class DefaultChannelPipeline implements ChannelPipeline {
...
// 出發(fā)channel read事件
@Override
public final ChannelPipeline fireChannelRead(Object msg) {
// 從head節(jié)點(diǎn)開(kāi)始往下傳播read事件
AbstractChannelHandlerContext.invokeChannelRead(head, msg);
return this;
}
...
}
調(diào)用 AbstractChannelHandlerContext 中的 invokeChannelRead(head, msg) 接口:
abstract class AbstractChannelHandlerContext extends DefaultAttributeMap
implements ChannelHandlerContext, ResourceLeakHint {
...
// 調(diào)用channel read
static void invokeChannelRead(final AbstractChannelHandlerContext next, Object msg) {
// 獲取消息
final Object m = next.pipeline.touch(ObjectUtil.checkNotNull(msg, "msg"), next);
// 獲取 EventExecutor
EventExecutor executor = next.executor();
// true
if (executor.inEventLoop()) {
// 調(diào)用下面的invokeChannelRead接口:invokeChannelRead(Object msg)
next.invokeChannelRead(m);
} else {
executor.execute(new Runnable() {
@Override
public void run() {
next.invokeChannelRead(m);
}
});
}
}
private void invokeChannelRead(Object msg) {
if (invokeHandler()) {
try {
// handler():獲取當(dāng)前遍歷到的channelHandler,第一個(gè)為HeandContext,最后為TailContext
// 調(diào)用channel handler的channelRead接口
((ChannelInboundHandler) handler()).channelRead(this, msg);
} catch (Throwable t) {
notifyHandlerException(t);
}
} else {
fireChannelRead(msg);
}
}
...
@Override
public ChannelHandlerContext fireChannelRead(final Object msg) {
// 調(diào)回到上面的 invokeChannelRead(final AbstractChannelHandlerContext next, Object msg)
invokeChannelRead(findContextInbound(), msg);
return this;
}
...
// 遍歷出下一個(gè)ChannelHandler
private AbstractChannelHandlerContext findContextInbound() {
AbstractChannelHandlerContext ctx = this;
do {
//獲取下一個(gè)inbound類型的節(jié)點(diǎn)
ctx = ctx.next;
// 必須為inbound類型
} while (!ctx.inbound);
return ctx;
}
...
}
Pipeline中的第一個(gè)節(jié)點(diǎn)為HeadContext,它對(duì)于channelRead事件的處理,是直接往下傳播,代碼如下:
final class HeadContext extends AbstractChannelHandlerContext
...
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// HeadContext往下傳播channelRead事件,
// 調(diào)用HeandlerContext中的接口:fireChannelRead(final Object msg)
ctx.fireChannelRead(msg);
}
...
}
就這樣一直循環(huán)下去,依次會(huì)調(diào)用到 InboundHandlerA、InboundHandlerB、InboundHandlerC 中的 channelRead(ChannelHandlerContext ctx, Object msg) 接口。
到最后一個(gè)TailContext節(jié)點(diǎn),它對(duì)channelRead事件的處理如下:
public class DefaultChannelPipeline implements ChannelPipeline {
final class TailContext extends AbstractChannelHandlerContext implements ChannelInboundHandler {
...
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// 調(diào)用onUnhandledInboundMessage接口
onUnhandledInboundMessage(msg);
}
...
}
...
// 對(duì)未處理inbound消息做最后的處理
protected void onUnhandledInboundMessage(Object msg) {
try {
logger.debug("Discarded inbound message {} that reached at the tail of the pipeline. Please check your pipeline configuration.", msg);
} finally {
// 對(duì)msg對(duì)象的引用數(shù)減1,當(dāng)msg對(duì)象的引用數(shù)為0時(shí),釋放該對(duì)象的內(nèi)存
ReferenceCountUtil.release(msg);
}
}
...
}
以上就是pipeline對(duì)inBound消息的處理流程。
SimpleChannelInboundHandler
在前面的例子中,假如中間有一個(gè)ChannelHandler未對(duì)channelRead事件進(jìn)行傳播,就會(huì)導(dǎo)致消息對(duì)象無(wú)法得到釋放,最終導(dǎo)致內(nèi)存泄露。
我們還可以繼承 SimpleChannelInboundHandler 來(lái)自定義ChannelHandler,它的channelRead方法,對(duì)消息對(duì)象做了msg處理,防止內(nèi)存泄露。
public abstract class SimpleChannelInboundHandler<I> extends ChannelInboundHandlerAdapter {
...
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
boolean release = true;
try {
if (acceptInboundMessage(msg)) {
@SuppressWarnings("unchecked")
I imsg = (I) msg;
channelRead0(ctx, imsg);
} else {
release = false;
ctx.fireChannelRead(msg);
}
} finally {
if (autoRelease && release) {
// 對(duì)msg對(duì)象的引用數(shù)減1,當(dāng)msg對(duì)象的引用數(shù)為0時(shí),釋放該對(duì)象的內(nèi)存
ReferenceCountUtil.release(msg);
}
}
}
...
}
outBound事件傳播
接下來(lái),我們來(lái)分析Pipeline的outBound事件傳播機(jī)制。代碼示例如下:
示例
public class NettyPipelineOutboundExample {
public static void main(String[] args) {
EventLoopGroup group = new NioEventLoopGroup(1);
ServerBootstrap strap = new ServerBootstrap();
strap.group(group)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(8888))
.childOption(ChannelOption.TCP_NODELAY, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new OutboundHandlerA());
ch.pipeline().addLast(new OutboundHandlerB());
ch.pipeline().addLast(new OutboundHandlerC());
}
});
try {
ChannelFuture future = strap.bind().sync();
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
}
class OutboundHandlerA extends ChannelOutboundHandlerAdapter {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
// 輸出消息
System.out.println("OutboundHandlerA: " + msg);
// 傳播write事件到下一個(gè)節(jié)點(diǎn)
ctx.write(msg, promise);
}
}
class OutboundHandlerB extends ChannelOutboundHandlerAdapter {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
// 輸出消息
System.out.println("OutboundHandlerB: " + msg);
// 傳播write事件到下一個(gè)節(jié)點(diǎn)
ctx.write(msg, promise);
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
// 待handlerAdded事件觸發(fā)3s后,模擬觸發(fā)一個(gè)
ctx.executor().schedule(() -> {
// ctx.write("Hello world ! ");
ctx.channel().write("Hello world ! ");
}, 3, TimeUnit.SECONDS);
}
}
class OutboundHandlerC extends ChannelOutboundHandlerAdapter {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
// 輸出消息
System.out.println("OutboundHandlerC: " + msg);
// 傳播write事件到下一個(gè)節(jié)點(diǎn)
ctx.write(msg, promise);
}
}
通過(guò) telnet 來(lái)連接上面啟動(dòng)好的netty服務(wù),觸發(fā)channel added事件:
$ telnet 127.0.0.1 8888
按照OutboundHandlerA、OutboundHandlerB、OutboundHandlerC的添加順序,控制臺(tái)輸出如下信息:
OutboundHandlerC: Hello world !
OutboundHandlerB: Hello world !
OutboundHandlerA: Hello world !
輸出的順序正好與ChannelHandler的添加順序相反。
若是調(diào)用它們的添加順序,則會(huì)輸出對(duì)應(yīng)順序的信息,e.g:
...
ch.pipeline().addLast(new InboundHandlerB());
ch.pipeline().addLast(new InboundHandlerA());
ch.pipeline().addLast(new InboundHandlerC());
...
輸出如下信息:
OutboundHandlerC: Hello world !
OutboundHandlerA: Hello world !
OutboundHandlerB: Hello world !
源碼分析
強(qiáng)烈建議 下面的流程,自己通過(guò)IDE的Debug模式來(lái)分析
從channel的write方法開(kāi)始,往下傳播write事件:
public abstract class AbstractChannel extends DefaultAttributeMap implements Channel {
...
@Override
public ChannelFuture write(Object msg) {
// 調(diào)用pipeline往下傳播wirte事件
return pipeline.write(msg);
}
...
}
接著來(lái)看看Pipeline中的write接口:
public class DefaultChannelPipeline implements ChannelPipeline {
...
@Override
public final ChannelFuture write(Object msg) {
// 從tail節(jié)點(diǎn)開(kāi)始傳播
return tail.write(msg);
}
...
}
調(diào)用ChannelHandlerContext中的write接口:
abstract class AbstractChannelHandlerContext extends DefaultAttributeMap
implements ChannelHandlerContext, ResourceLeakHint {
...
@Override
public ChannelFuture write(Object msg) {
// 往下調(diào)用write接口
return write(msg, newPromise());
}
@Override
public ChannelFuture write(final Object msg, final ChannelPromise promise) {
if (msg == null) {
throw new NullPointerException("msg");
}
try {
if (isNotValidPromise(promise, true)) {
ReferenceCountUtil.release(msg);
// cancelled
return promise;
}
} catch (RuntimeException e) {
ReferenceCountUtil.release(msg);
throw e;
}
// 往下調(diào)用write接口
write(msg, false, promise);
return promise;
}
...
private void write(Object msg, boolean flush, ChannelPromise promise) {
// 尋找下一個(gè)outbound類型的channelHandlerContext
AbstractChannelHandlerContext next = findContextOutbound();
final Object m = pipeline.touch(msg, next);
EventExecutor executor = next.executor();
if (executor.inEventLoop()) {
if (flush) {
next.invokeWriteAndFlush(m, promise);
} else {
// 調(diào)用接口 invokeWrite(Object msg, ChannelPromise promise)
next.invokeWrite(m, promise);
}
} else {
AbstractWriteTask task;
if (flush) {
task = WriteAndFlushTask.newInstance(next, m, promise);
} else {
task = WriteTask.newInstance(next, m, promise);
}
safeExecute(executor, task, promise, m);
}
}
// 尋找下一個(gè)outbound類型的channelHandlerContext
private AbstractChannelHandlerContext findContextOutbound() {
AbstractChannelHandlerContext ctx = this;
do {
ctx = ctx.prev;
} while (!ctx.outbound);
return ctx;
}
private void invokeWrite(Object msg, ChannelPromise promise) {
if (invokeHandler()) {
// 繼續(xù)往下調(diào)用
invokeWrite0(msg, promise);
} else {
write(msg, promise);
}
}
private void invokeWrite0(Object msg, ChannelPromise promise) {
try {
// 獲取當(dāng)前的channelHandler,調(diào)用其write接口
// handler()依次會(huì)返回 OutboundHandlerC OutboundHandlerB OutboundHandlerA
((ChannelOutboundHandler) handler()).write(this, msg, promise);
} catch (Throwable t) {
notifyOutboundHandlerException(t, promise);
}
}
...
}
最終會(huì)調(diào)用到HeadContext的write接口:
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
// 調(diào)用unsafe進(jìn)行寫數(shù)據(jù)操作
unsafe.write(msg, promise);
}
異常傳播
了解了Pipeline的入站與出站事件的機(jī)制之后,我們?cè)賮?lái)看看Pipeline的異常處理機(jī)制。
示例
public class NettyPipelineExceptionCaughtExample {
public static void main(String[] args) {
EventLoopGroup group = new NioEventLoopGroup(1);
ServerBootstrap strap = new ServerBootstrap();
strap.group(group)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(8888))
.childOption(ChannelOption.TCP_NODELAY, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new InboundHandlerA());
ch.pipeline().addLast(new InboundHandlerB());
ch.pipeline().addLast(new InboundHandlerC());
ch.pipeline().addLast(new OutboundHandlerA());
ch.pipeline().addLast(new OutboundHandlerB());
ch.pipeline().addLast(new OutboundHandlerC());
}
});
try {
ChannelFuture future = strap.bind().sync();
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
static class InboundHandlerA extends ChannelInboundHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("InboundHandlerA.exceptionCaught:" + cause.getMessage());
ctx.fireExceptionCaught(cause);
}
}
static class InboundHandlerB extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
throw new Exception("ERROR !!!");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("InboundHandlerB.exceptionCaught:" + cause.getMessage());
ctx.fireExceptionCaught(cause);
}
}
static class InboundHandlerC extends ChannelInboundHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("InboundHandlerC.exceptionCaught:" + cause.getMessage());
ctx.fireExceptionCaught(cause);
}
}
static class OutboundHandlerA extends ChannelOutboundHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("OutboundHandlerA.exceptionCaught:" + cause.getMessage());
ctx.fireExceptionCaught(cause);
}
}
static class OutboundHandlerB extends ChannelOutboundHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("OutboundHandlerB.exceptionCaught:" + cause.getMessage());
ctx.fireExceptionCaught(cause);
}
}
static class OutboundHandlerC extends ChannelOutboundHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("OutboundHandlerC.exceptionCaught:" + cause.getMessage());
ctx.fireExceptionCaught(cause);
}
}
}
通過(guò) telnet 來(lái)連接上面啟動(dòng)好的netty服務(wù),并在控制臺(tái)發(fā)送任意字符:
$ telnet 127.0.0.1 8888
觸發(fā)channel read事件并拋出異常,控制臺(tái)輸出如下信息:
InboundHandlerB.exceptionCaught:ERROR !!!
InboundHandlerC.exceptionCaught:ERROR !!!
OutboundHandlerA.exceptionCaught:ERROR !!!
OutboundHandlerB.exceptionCaught:ERROR !!!
OutboundHandlerC.exceptionCaught:ERROR !!!
可以看到異常的捕獲與我們添加的ChannelHandler順序相同。
源碼分析
在我們的示例中,InboundHandlerB的ChannelRead接口拋出異常,導(dǎo)致從InboundHandlerA將ChannelRead事件傳播到InboundHandlerB的過(guò)程中出現(xiàn)異常,異常被捕獲。
abstract class AbstractChannelHandlerContext extends DefaultAttributeMap
implements ChannelHandlerContext, ResourceLeakHint {
...
@Override
public ChannelHandlerContext fireExceptionCaught(final Throwable cause) {
//調(diào)用invokeExceptionCaught接口
invokeExceptionCaught(next, cause);
return this;
}
static void invokeExceptionCaught(final AbstractChannelHandlerContext next, final Throwable cause) {
ObjectUtil.checkNotNull(cause, "cause");
EventExecutor executor = next.executor();
if (executor.inEventLoop()) {
// 調(diào)用下一個(gè)節(jié)點(diǎn)的invokeExceptionCaught接口
next.invokeExceptionCaught(cause);
} else {
try {
executor.execute(new Runnable() {
@Override
public void run() {
next.invokeExceptionCaught(cause);
}
});
} catch (Throwable t) {
if (logger.isWarnEnabled()) {
logger.warn("Failed to submit an exceptionCaught() event.", t);
logger.warn("The exceptionCaught() event that was failed to submit was:", cause);
}
}
}
}
...
private void invokeChannelRead(Object msg) {
if (invokeHandler()) {
try {
// 拋出異常
((ChannelInboundHandler) handler()).channelRead(this, msg);
} catch (Throwable t) {
// 異常捕獲,往下傳播
notifyHandlerException(t);
}
} else {
fireChannelRead(msg);
}
}
// 通知Handler發(fā)生異常事件
private void notifyHandlerException(Throwable cause) {
if (inExceptionCaught(cause)) {
if (logger.isWarnEnabled()) {
logger.warn(
"An exception was thrown by a user handler " +
"while handling an exceptionCaught event", cause);
}
return;
}
// 往下調(diào)用invokeExceptionCaught接口
invokeExceptionCaught(cause);
}
private void invokeExceptionCaught(final Throwable cause) {
if (invokeHandler()) {
try {
// 調(diào)用當(dāng)前ChannelHandler的exceptionCaught接口
// 在我們的案例中,依次會(huì)調(diào)用InboundHandlerB、InboundHandlerC、
// OutboundHandlerA、OutboundHandlerB、OutboundHandlC
handler().exceptionCaught(this, cause);
} catch (Throwable error) {
if (logger.isDebugEnabled()) {
logger.debug(
"An exception {}" +
"was thrown by a user handler's exceptionCaught() " +
"method while handling the following exception:",
ThrowableUtil.stackTraceToString(error), cause);
} else if (logger.isWarnEnabled()) {
logger.warn(
"An exception '{}' [enable DEBUG level for full stacktrace] " +
"was thrown by a user handler's exceptionCaught() " +
"method while handling the following exception:", error, cause);
}
}
} else {
fireExceptionCaught(cause);
}
}
...
}
最終會(huì)調(diào)用到TailContext節(jié)點(diǎn)的exceptionCaught接口,如果我們中途沒(méi)有對(duì)異常進(jìn)行攔截處理,做會(huì)打印出一段警告信息!
public class DefaultChannelPipeline implements ChannelPipeline {
...
final class TailContext extends AbstractChannelHandlerContext implements ChannelInboundHandler {
...
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
onUnhandledInboundException(cause);
}
...
protected void onUnhandledInboundException(Throwable cause) {
try {
logger.warn(
"An exceptionCaught() event was fired, and it reached at the tail of the pipeline. " +
"It usually means the last handler in the pipeline did not handle the exception.",
cause);
} finally {
ReferenceCountUtil.release(cause);
}
}
}
...
}
在實(shí)際的應(yīng)用中,一般會(huì)定一個(gè)ChannelHandler,放置Pipeline末尾,專門用來(lái)處理中途出現(xiàn)的各種異常。
最佳異常處理實(shí)踐
單獨(dú)定義ExceptionCaughtHandler來(lái)處理異常:
...
class ExceptionCaughtHandler extends ChannelInboundHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
if (cause instanceof Exception) {
// TODO
System.out.println("Successfully caught exception ! ");
} else {
// TODO
}
}
}
...
ch.pipeline().addLast(new ExceptionCaughtHandler());
...
輸出:
InboundHandlerB.exceptionCaught:ERROR !!!
InboundHandlerC.exceptionCaught:ERROR !!!
OutboundHandlerA.exceptionCaught:ERROR !!!
OutboundHandlerB.exceptionCaught:ERROR !!!
OutboundHandlerC.exceptionCaught:ERROR !!!
Successfully caught exception ! // 成功捕獲日志
Pipeline回顧與總結(jié)
至此,我們對(duì)Pipeline的原理的解析就完成了。
- Pipeline是在什么時(shí)候創(chuàng)建的?
- Pipeline添加與刪除節(jié)點(diǎn)的邏輯是怎么樣的?
- netty是如何判斷ChannelHandler類型的?
- 如何處理ChannelHandler中拋出的異常?
- 對(duì)于ChannelHandler的添加應(yīng)遵循什么樣的順序?