【问题标题】:The ReplayingDecoder wants to collect multiple incoming messages and pass them to the next handlerReplayingDecoder 想要收集多个传入消息并将它们传递给下一个处理程序
【发布时间】:2019-06-19 12:08:34
【问题描述】:

以下是服务器端代码。 实现简单的 replayingDecoder 将传入的消息分成多个部分 我想将它用作以下Handler中业务逻辑的预处理器。

但是在TestReplayingDecoder中解码完成后,anotherHandler的channRead方法并没有被执行 仅执行 channelReadComplete 方法。为什么?

同样,当您使用 checkPoint 和 state 实现 ReplayingDecode 时, 我已确认以下处理程序的 channelRead 运行良好。

.childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(new LoggingHandler(LogLevel.INFO));
                        pipeline.addLast(new TestReplayingDecoder());
                        pipeline.addLast(new AnotherHandler());
                    }
                });



@Slf4j
public class TestReplayingDecoder extends ReplayingDecoder<Void> {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        out.add(in.readBytes(in.readInt()));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}



@Slf4j
@Sharable
public class AnotherHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        String msgString = (String) msg;
        ctx.writeAndFlush(Unpooled.copiedBuffer(makeResponse(msg), CharsetUtil.US_ASCII));
    }
}

日志 ====================

nioEventLoopGroup-2-1 [-:-] [id: 0x2e31ca6f, L:/0:0:0:0:0:0:0:0:21103] READ: [id: 0x653410de, L:/127.0.0.1:21103 - R:/127.0.0.1:61671]
nioEventLoopGroup-2-1 [-:-] [id: 0x2e31ca6f, L:/0:0:0:0:0:0:0:0:21103] READ COMPLETE
nioEventLoopGroup-3-2 [-:-] [id: 0x653410de, L:/127.0.0.1:21103 - R:/127.0.0.1:61671] REGISTERED
nioEventLoopGroup-3-2 [-:-] [id: 0x653410de, L:/127.0.0.1:21103 - R:/127.0.0.1:61671] ACTIVE
nioEventLoopGroup-3-2 [-:-] AnotherHandler.channelActive !!
nioEventLoopGroup-3-2 [-:-] [id: 0x653410de, L:/127.0.0.1:21103 - R:/127.0.0.1:61671] READ: 1024B
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 32 39 36 36 41 42 41 30 37 41 52 50 30 32 30 30 |.                |
~ ~
+--------+-------------------------------------------------+----------------+
nioEventLoopGroup-3-2 [-:-] TestReplayingDecoder.decode !!
nioEventLoopGroup-3-2 [-:-] [id: 0x653410de, L:/127.0.0.1:21103 - R:/127.0.0.1:61671] READ: 1024B
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 |                |
~ ~ 
+--------+-------------------------------------------------+----------------+
nioEventLoopGroup-3-2 [-:-] TestReplayingDecoder.decode !!
nioEventLoopGroup-3-2 [-:-] [id: 0x653410de, L:/127.0.0.1:21103 - R:/127.0.0.1:61671] READ: 922B
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 |                |
~ ~ 
+--------+-------------------------------------------------+----------------+
nioEventLoopGroup-3-2 [-:-] TestReplayingDecoder.decode !!
nioEventLoopGroup-3-2 [-:-] [id: 0x653410de, L:/127.0.0.1:21103 - R:/127.0.0.1:61671] READ COMPLETE
nioEventLoopGroup-3-2 [-:-] AnotherHandler.channelReadComplete !!
nioEventLoopGroup-3-2 [-:-] [id: 0x653410de, L:/127.0.0.1:21103 - R:/127.0.0.1:61671] WRITE: 0B
nioEventLoopGroup-3-2 [-:-] [id: 0x653410de, L:/127.0.0.1:21103 - R:/127.0.0.1:61671] FLUSH

【问题讨论】:

  • 我想知道解码发生n次后调用AnotherHandler的channelReadComplete,这是正常流程吗?

标签: netty decoder


【解决方案1】:

我很确定您的 channelRead(...) 方法已被调用,但是当您尝试将 msg 转换为 String 时,它会导致 ClassCastException,而当您生成 ByteBufs 时它将是 ByteBuf你的TestReplayingDecoder

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 2011-03-12
    • 2012-01-07
    • 1970-01-01
    • 2020-09-04
    • 2018-03-11
    • 1970-01-01
    相关资源
    最近更新 更多