ReplayingDecoder想要收集多个传入消息并将它们传递给下一个处理程序

时间:2019-01-25 16:09:14

标签: netty decoder

下面是服务器端代码。 实现简单的replayingDecoder将传入消息分为多段 我想将其用作以下处理程序中业务逻辑的预处理器。

但是,在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));
    }
}

log ==================

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

1 个答案:

答案 0 :(得分:0)

我非常确定您的channelRead(...)方法已被调用,但是当您尝试将msg转换为ClassCastException时,它将导致String,而它将是一个ByteBuf当您在ByteBuf中产生TestReplayingDecoder时。