相关疑难解决方法(0)

在netty中ctx.write()和ctx.channel().write()之间有什么区别?

我注意到ctx与处理程序不同,即使这些处理程序位于同一个管道中,例如

    p.addLast("myHandler1", new MyHandler1());
    p.addLast("myHandler2", new MyHandler2());
Run Code Online (Sandbox Code Playgroud)

在MyHander1

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.err.println("My 1 ctx: " + ctx + " channel: " + ctx.channel());
    super.channelRead(ctx, msg);
}
Run Code Online (Sandbox Code Playgroud)

在MyHandler2中

@Override
protected void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.err.println("My 2 ctx: " + ctx + " channel: " + ctx.channel());
}
Run Code Online (Sandbox Code Playgroud)

和输出:

My 1 ctx: io.netty.channel.DefaultChannelHandlerContext@ba9340 channel: [id: 0xdfad3a16, /127.0.0.1:60887 => /127.0.0.1:8090] 
My 2 ctx: io.netty.channel.DefaultChannelHandlerContext@1551d7f channel: [id: 0xdfad3a16, /127.0.0.1:60887 => /127.0.0.1:8090]
Run Code Online (Sandbox Code Playgroud)

我注意到ctx是不同的,但通道是相同的

那么调用ctx.write()和ctx.channel().write()之间有什么区别吗?

java netty

9
推荐指数
1
解决办法
3901
查看次数

标签 统计

java ×1

netty ×1