在Netty pipleline中有多个ChannelInboundHandlerAdapter

时间:2016-03-18 03:56:56

标签: java netty

我是netty的新手,我想创建一个TCP服务器,在实例化连接时进行自定义应用程序层握手。在握手之后,我想将消息(ByteBuf)传递给队列,以便它们可以被其他一些线程处理。

我的问题是,我可以在渠道管道中拥有多个ChannelInboundHandlerAdapter吗?一个用于应用层握手协议,另一个用于将消息传递到队列。此外,我想知道消息如何流经管道。如果在一个处理程序(或解码器/编码器)处接收到消息,则如何将其传递给另一个处理程序。

具体来说,如果我从here更改EchoServer并添加另一个ChannelInboundHandlerAdapter,则echo服务器处理程序将停止接收任何消息。

ServerBootstrap b = new ServerBootstrap();
            b.group(group)
             .channel(NioServerSocketChannel.class)
             .localAddress(new InetSocketAddress(port))
             .childHandler(new ChannelInitializer<SocketChannel>() {
                 @Override
                 public void initChannel(SocketChannel ch) 
                     throws Exception {
                 ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                     @Override
                     public void channelRead(ChannelHandlerContext ctx,
                          Object msg) {
                           }
                          });

                  ch.pipeline().addLast(
                       new EchoServerHandler());
                 }
                 });

我的逻辑是:有2个ChannelInboundHandlerAdapter然后与第一个处理程序进行握手,如果它们与握手条件不匹配则丢弃数据包,然后通过第二个ChannelInboundHandlerAdapter将消息传递给队列。我的逻辑是否正确?如果不是应该怎么做?

非常感谢。

2 个答案:

答案 0 :(得分:3)

ChannelInboundHandlerAdapterChannelInBoundHandler接口的适配器类。首先,您可以使用SimpleChannelInboundHandler(或者更复杂的是,您可以扩展适配器类,编写自己的扩展ChannelInboundHandlerAdapter的处理程序)。 SimpleCHannelInboundHandlerchannelRead()之后自动释放消息(从而将其传递给ChannelPipeline中的下一个处理程序)。

要使用更简单的SimpleChannelInboundHandler,请参阅此帖子Netty hello world example not working

所以代替这个ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {}

你必须写一个扩展SimpleChannelInboundHandler的新类,如

public class MyHandler extends SimpleChannelInboundHandler{


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {


        try {
            System.out.println(in.toString(io.netty.util.CharsetUtil.US_ASCII));
        } finally {
            in.release();
        }


    }
}

并像

一样调用它
public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new MyHandler());
                    }

如上所述,SimpleCHannelInboundHandler在channelRead()之后自动释放消息(从而将其传递给ChannelPipeline中的下一个处理程序)。

如果你使用ChannelInboundHandlerAdapter,你必须自己实现将消息/事件传递给下一个处理程序

处理程序必须调用ChannelHandlerContext ctx中的事件传播方法将事件转发到其下一个处理程序。(在SimpleChannelInboundHandler类中,这已实现)

 public class MyInboundHandler extends ChannelInboundHandlerAdapter {
   @Override
   public void channelActive(ChannelHandlerContext ctx) {
       System.out.println("Connected!");
       ctx.fireChannelActive();
    }
 }

请参阅此http://netty.io/4.0/api/io/netty/channel/ChannelPipeline.html

答案 1 :(得分:0)

我必须提醒一下:

只能将一个SimpleChannelInboundHandler扩展添加到管道链。 因为SimpleChannelInboundHandler有一个finally代码块会释放所有的msg。

@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) {
            //release all handled messages,so the next handler won't be executed
            ReferenceCountUtil.release(msg);**
        }
    }
}

改为使用ChannelInboundHandlerAdapter:

public class CustomizeChannelInboundHandler extends ChannelInboundHandlerAdapter {
  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.out.println("do something you like!")

    super.channelRead(ctx, msg);
  }

}