不同順序InBoundHandler之間的資料傳遞

陈鸿圳發表於2024-05-27

pipeline

// out handler
socketChannel.pipeline().addLast(new StringEncoder());
// in handler
socketChannel.pipeline().addLast(new StringDecoder());
socketChannel.pipeline().addLast(new MyServerChannelInBoundHandler1());
socketChannel.pipeline().addLast(new MyServerChannelInBoundHandler2());

注意【MyServerChannelInBoundHandler1】必須在【MyServerChannelInBoundHandler2】的上面
【MyServerChannelInBoundHandler1.java】

@Slf4j
public class MyServerChannelInBoundHandler1 extends SimpleChannelInboundHandler<String>
{
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        log.info("MyServerChannelInBoundHandler1.channelRead0[" + ctx.channel().remoteAddress() + "]:" + msg);
        String retMsg = String.format("server have received data from you:[%s]", msg.trim());
        ctx.writeAndFlush(retMsg);      // 傳送資料出去
        ctx.fireChannelRead(retMsg);    // 這一句會觸發下一個InBoundHandler的【channelRead】和【channelRead0】被觸發
    }
}

注意上面呼叫了【writeAndFlush(retMsg)】
注意上面呼叫【fireChannelRead(retMsg)】時引數是被修改過的【retMsg】,而不是【msg】。
【MyServerChannelInBoundHandler2.java】

@Slf4j
public class MyServerChannelInBoundHandler2 extends SimpleChannelInboundHandler<String>
{
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        log.info("MyServerChannelInBoundHandler2.channelRead[" + ctx.channel().remoteAddress() + "]:" + msg);
        super.channelRead(ctx, msg);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) {
        log.info("MyServerChannelInBoundHandler2.channelRead0[" + ctx.channel().remoteAddress() + "]:" + msg);
        ctx.writeAndFlush(msg);        // 傳送資料出去,這裡的msg是被上面的InBoundHandler修改過的
    }
}

注意【ctx.writeAndFlush()】方法被呼叫了兩次,所以資料其實是被髮送了兩次出去。
在正常使用中,如果一個InBoundHandler確定了資料是要傳給下一個InBoundHandler的話,本handler是不會傳送資料的,而是會將資料交給下一個handler去處理,傳送資料也由下一個handler傳送。

相關文章