Java 在Netty 4中向上游迁移

Java 在Netty 4中向上游迁移,java,netty,Java,Netty,我正在从netty 3迁移到netty 4。我有一个管道处理程序,它充当一个典型的过滤器,在途中拦截/处理不符合要求的消息,并将符合要求的消息推到上游 基于文档(),我希望使用ctx.fireInboundBufferUpdated()代替ctx.sendUpStream()来中继入站。但是,我发现这不起作用,但是ChannelHandlerUtil.addTonBuundBuffer()起作用。我希望得到以下方面的指导: 我对当前文档断言ctx.sendUpstream->ctx.firein

我正在从netty 3迁移到netty 4。我有一个管道处理程序,它充当一个典型的过滤器,在途中拦截/处理不符合要求的消息,并将符合要求的消息推到上游

基于文档(),我希望使用
ctx.fireInboundBufferUpdated()
代替
ctx.sendUpStream()
来中继入站。但是,我发现这不起作用,但是
ChannelHandlerUtil.addTonBuundBuffer()
起作用。我希望得到以下方面的指导:

  • 我对当前文档断言
    ctx.sendUpstream->ctx.fireinboundBufferUpdate
  • 在这种情况下,如果与我下面所做的不同,那么最佳实践是什么
  • 守则:

    //The pipeline
    
    public class ServerInitializer extends ChannelInitializer<SocketChannel> {
    
     @Override
     public void initChannel(SocketChannel ch) throws Exception {
         ChannelPipeline p = ch.pipeline();
         p.addLast("decoder", new HttpRequestDecoder());
         p.addLast("encoder", new HttpResponseEncoder());
         p.addLast("inbound", InboundHttpRequestFilter.INSTANCE);
         p.addLast("handler", handlerClass.newInstance());
    
     }
    }
    
    //The filter
    public class InboundHttpRequestFilter extends
            ChannelInboundMessageHandlerAdapter<Object> {
    
        @Override
        public void messageReceived(ChannelHandlerContext ctx, Object msg)
                throws Exception {
            ... discard/handle as necessary …;
            //ctx.fireInboundBufferUpdated(); - doesn't propagate upstream
            ChannelHandlerUtil.addToNextInboundBuffer(ctx, msg); // sends upstream
        }
    }
    
    //管道
    公共类ServerInitializer扩展了ChannelInitializer{
    @凌驾
    public void initChannel(SocketChannel ch)引发异常{
    ChannelPipeline p=通道管道();
    p、 addLast(“解码器”,新的HttpRequestDecoder());
    p、 addLast(“编码器”,新的HttpResponseEncoder());
    p、 addLast(“入站”,InboundHttpRequestFilter.INSTANCE);
    p、 addLast(“handler”,handlerClass.newInstance());
    }
    }
    //过滤器
    公共类InboundHttpRequestFilter扩展
    ChannelInboundMessageHandlerAdapter{
    @凌驾
    接收到公共无效消息(ChannelHandlerContext ctx,对象消息)
    抛出异常{
    …必要时丢弃/处理…;
    //ctx.fireInboundBufferUpdated();-不向上游传播
    ChannelHandlerUtil.AddTonBuffer(ctx,msg);//向上游发送
    }
    }
    
    试试这个:

    ctx.nextInboundMessageBuffer().add(msg)
    
    Javadoc:

    Interface ChannelHandlerContext
    MessageBuf<Object>  nextInboundMessageBuffer()
        Return the MessageBuf of the next ChannelInboundMessageHandler in the pipeline.
    
    接口ChannelHandlerContext
    MessageBuf nextInboundMessageBuffer()
    返回管道中下一个ChannelInboundMessageHandler的MessageBuf。
    
    Netty 4多处理器示例:

    MultiHandlerServer.java

    import io.netty.bootstrap.ServerBootstrap;
    import io.netty.channel.ChannelFuture;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.channel.socket.nio.NioServerSocketChannel;
    import io.netty.handler.codec.LineBasedFrameDecoder;
    import io.netty.handler.codec.string.StringDecoder;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.nio.charset.Charset;
    
    public class MultiHandlerServer {
        private static final Logger logger = LoggerFactory.getLogger(MultiHandlerServer.class);
    
        final int port;
    
        public MultiHandlerServer(final int port) {
            this.port = port;
        }
    
        public void run() throws InterruptedException {
            final NioEventLoopGroup bossGroup = new NioEventLoopGroup();
            final NioEventLoopGroup workerGroup = new NioEventLoopGroup();
            try {
    
                final ServerBootstrap serverBootstrap = new ServerBootstrap()
                        .group(bossGroup, workerGroup)
                        .channel(NioServerSocketChannel.class)
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            protected void initChannel(SocketChannel ch) throws Exception {
                                ch.pipeline().addLast(
                                        new LineBasedFrameDecoder(8192),
                                        new StringDecoder(Charset.forName("UTF-8")),
                                        new MultiHandler01(), new MultiHandler02());
                            }
                        });
    
                final ChannelFuture future = serverBootstrap.bind(port).sync();
                future.channel().closeFuture().sync();
            } finally {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
        }
    
        public static void main(String[] args) throws InterruptedException {
            final MultiHandlerServer client = new MultiHandlerServer(8080);
            client.run();
        }
    }
    
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundMessageHandlerAdapter;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     */
    class MultiHandler01 extends ChannelInboundMessageHandlerAdapter<String> {
        private Logger logger = LoggerFactory.getLogger(MultiHandler01.class);
    
        MultiHandler01() {
        }
    
        @Override
        public void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
            logger.info(String.format("Handler01 receive message: %s", msg));
            ctx.nextInboundMessageBuffer().add(msg);
            ctx.fireInboundBufferUpdated();
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            logger.error("Exception caught: %s", ctx.channel().remoteAddress(), cause);
            ctx.close();
        }
    }
    
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundMessageHandlerAdapter;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     */
    class MultiHandler02 extends ChannelInboundMessageHandlerAdapter<String> {
        private Logger logger = LoggerFactory.getLogger(MultiHandler02.class);
    
        MultiHandler02() {
        }
    
        @Override
        public void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
            logger.info(String.format("Handler02 receive message: %s", msg));
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            logger.error("Exception caught: %s", ctx.channel().remoteAddress(), cause);
            ctx.close();
        }
    }
    
    导入io.netty.bootstrap.ServerBootstrap;
    导入io.netty.channel.ChannelFuture;
    导入io.netty.channel.ChannelInitializer;
    导入io.netty.channel.nio.NioEventLoopGroup;
    导入io.netty.channel.socket.SocketChannel;
    导入io.netty.channel.socket.nio.NioServerSocketChannel;
    导入io.netty.handler.codec.LineBasedFrameDecoder;
    导入io.netty.handler.codec.string.StringDecoder;
    导入org.slf4j.Logger;
    导入org.slf4j.LoggerFactory;
    导入java.nio.charset.charset;
    公共类多处理器服务器{
    私有静态最终记录器Logger=LoggerFactory.getLogger(MultiHandlerServer.class);
    最终国际端口;
    公共多处理器服务器(最终int端口){
    this.port=端口;
    }
    public void run()引发InterruptedException{
    final NioEventLoopGroup bossGroup=新的NioEventLoopGroup();
    final NioEventLoopGroup workerGroup=新的NioEventLoopGroup();
    试一试{
    final ServerBootstrap ServerBootstrap=newserverbootstrap()
    .group(bossGroup、workerGroup)
    .channel(NioServerSocketChannel.class)
    .childHandler(新的通道初始值设定项(){
    @凌驾
    受保护的void initChannel(SocketChannel ch)引发异常{
    ch.pipeline().addLast(
    新的LineBasedFrameDecoder(8192),
    新的字符串解码器(字符集forName(“UTF-8”),
    新的MultiHandler01(),新的MultiHandler02());
    }
    });
    final ChannelFuture=serverBootstrap.bind(port.sync();
    future.channel().closeFuture().sync();
    }最后{
    bossGroup.shutdownGracefully();
    workerGroup.shutdownGracefully();
    }
    }
    公共静态void main(字符串[]args)引发InterruptedException{
    最终MultiHandlerServer客户端=新的MultiHandlerServer(8080);
    client.run();
    }
    }
    
    MultiHandler01.java

    import io.netty.bootstrap.ServerBootstrap;
    import io.netty.channel.ChannelFuture;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.channel.socket.nio.NioServerSocketChannel;
    import io.netty.handler.codec.LineBasedFrameDecoder;
    import io.netty.handler.codec.string.StringDecoder;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.nio.charset.Charset;
    
    public class MultiHandlerServer {
        private static final Logger logger = LoggerFactory.getLogger(MultiHandlerServer.class);
    
        final int port;
    
        public MultiHandlerServer(final int port) {
            this.port = port;
        }
    
        public void run() throws InterruptedException {
            final NioEventLoopGroup bossGroup = new NioEventLoopGroup();
            final NioEventLoopGroup workerGroup = new NioEventLoopGroup();
            try {
    
                final ServerBootstrap serverBootstrap = new ServerBootstrap()
                        .group(bossGroup, workerGroup)
                        .channel(NioServerSocketChannel.class)
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            protected void initChannel(SocketChannel ch) throws Exception {
                                ch.pipeline().addLast(
                                        new LineBasedFrameDecoder(8192),
                                        new StringDecoder(Charset.forName("UTF-8")),
                                        new MultiHandler01(), new MultiHandler02());
                            }
                        });
    
                final ChannelFuture future = serverBootstrap.bind(port).sync();
                future.channel().closeFuture().sync();
            } finally {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
        }
    
        public static void main(String[] args) throws InterruptedException {
            final MultiHandlerServer client = new MultiHandlerServer(8080);
            client.run();
        }
    }
    
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundMessageHandlerAdapter;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     */
    class MultiHandler01 extends ChannelInboundMessageHandlerAdapter<String> {
        private Logger logger = LoggerFactory.getLogger(MultiHandler01.class);
    
        MultiHandler01() {
        }
    
        @Override
        public void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
            logger.info(String.format("Handler01 receive message: %s", msg));
            ctx.nextInboundMessageBuffer().add(msg);
            ctx.fireInboundBufferUpdated();
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            logger.error("Exception caught: %s", ctx.channel().remoteAddress(), cause);
            ctx.close();
        }
    }
    
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundMessageHandlerAdapter;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     */
    class MultiHandler02 extends ChannelInboundMessageHandlerAdapter<String> {
        private Logger logger = LoggerFactory.getLogger(MultiHandler02.class);
    
        MultiHandler02() {
        }
    
        @Override
        public void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
            logger.info(String.format("Handler02 receive message: %s", msg));
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            logger.error("Exception caught: %s", ctx.channel().remoteAddress(), cause);
            ctx.close();
        }
    }
    
    导入io.netty.channel.ChannelHandlerContext;
    导入io.netty.channel.ChannelInboundMessageHandlerAdapter;
    导入org.slf4j.Logger;
    导入org.slf4j.LoggerFactory;
    /**
    */
    类MultiHandler01扩展ChannelInboundMessageHandlerAdapter{
    私有记录器Logger=LoggerFactory.getLogger(MultiHandler01.class);
    MultiHandler01(){
    }
    @凌驾
    public void messageReceived(ChannelHandlerContext ctx,String msg)引发异常{
    logger.info(String.format(“Handler01接收消息:%s”,msg));
    ctx.nextInboundMessageBuffer().add(msg);
    ctx.fireInboundBufferUpdated();
    }
    @凌驾
    public void exceptionCaught(ChannelHandlerContext ctx,可丢弃原因)引发异常{
    logger.error(“捕获的异常:%s”,ctx.channel().remoteAddress(),原因);
    ctx.close();
    }
    }
    
    MultiHandler02.java

    import io.netty.bootstrap.ServerBootstrap;
    import io.netty.channel.ChannelFuture;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.channel.socket.nio.NioServerSocketChannel;
    import io.netty.handler.codec.LineBasedFrameDecoder;
    import io.netty.handler.codec.string.StringDecoder;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.nio.charset.Charset;
    
    public class MultiHandlerServer {
        private static final Logger logger = LoggerFactory.getLogger(MultiHandlerServer.class);
    
        final int port;
    
        public MultiHandlerServer(final int port) {
            this.port = port;
        }
    
        public void run() throws InterruptedException {
            final NioEventLoopGroup bossGroup = new NioEventLoopGroup();
            final NioEventLoopGroup workerGroup = new NioEventLoopGroup();
            try {
    
                final ServerBootstrap serverBootstrap = new ServerBootstrap()
                        .group(bossGroup, workerGroup)
                        .channel(NioServerSocketChannel.class)
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            protected void initChannel(SocketChannel ch) throws Exception {
                                ch.pipeline().addLast(
                                        new LineBasedFrameDecoder(8192),
                                        new StringDecoder(Charset.forName("UTF-8")),
                                        new MultiHandler01(), new MultiHandler02());
                            }
                        });
    
                final ChannelFuture future = serverBootstrap.bind(port).sync();
                future.channel().closeFuture().sync();
            } finally {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
        }
    
        public static void main(String[] args) throws InterruptedException {
            final MultiHandlerServer client = new MultiHandlerServer(8080);
            client.run();
        }
    }
    
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundMessageHandlerAdapter;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     */
    class MultiHandler01 extends ChannelInboundMessageHandlerAdapter<String> {
        private Logger logger = LoggerFactory.getLogger(MultiHandler01.class);
    
        MultiHandler01() {
        }
    
        @Override
        public void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
            logger.info(String.format("Handler01 receive message: %s", msg));
            ctx.nextInboundMessageBuffer().add(msg);
            ctx.fireInboundBufferUpdated();
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            logger.error("Exception caught: %s", ctx.channel().remoteAddress(), cause);
            ctx.close();
        }
    }
    
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundMessageHandlerAdapter;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     */
    class MultiHandler02 extends ChannelInboundMessageHandlerAdapter<String> {
        private Logger logger = LoggerFactory.getLogger(MultiHandler02.class);
    
        MultiHandler02() {
        }
    
        @Override
        public void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
            logger.info(String.format("Handler02 receive message: %s", msg));
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            logger.error("Exception caught: %s", ctx.channel().remoteAddress(), cause);
            ctx.close();
        }
    }
    
    导入io.netty.channel.ChannelHandlerContext;
    导入io.netty.channel.ChannelInboundMessageHandlerAdapter;
    导入org.slf4j.Logger;
    导入org.slf4j.LoggerFactory;
    /**
    */
    类MultiHandler02扩展ChannelInboundMessageHandlerAdapter{
    私有记录器Logger=LoggerFactory.getLogger(MultiHandler02.class);
    MultiHandler02(){
    }
    @凌驾
    public void messageReceived(ChannelHandlerContext ctx,String msg)引发异常{
    logger.info(String.format(“Handler02接收消息:%s”,msg));
    }
    @凌驾
    public void exceptionCaught(ChannelHandlerContext ctx,可丢弃原因)引发异常{
    logger.error(“捕获的异常:%s”,ctx.channel().remoteAddress(),原因);
    ctx.close();
    }
    }
    
    试试这个:

    ctx.nextInboundMessageBuffer().add(msg)
    
    Javadoc:

    Interface ChannelHandlerContext
    MessageBuf<Object>  nextInboundMessageBuffer()
        Return the MessageBuf of the next ChannelInboundMessageHandler in the pipeline.
    
    接口ChannelHandlerContext
    MessageBuf nextInboundMessageBuffer()
    返回管道中下一个ChannelInboundMessageHandler的MessageBuf。
    
    Netty 4多处理器示例:

    MultiHandlerServer.java

    import io.netty.bootstrap.ServerBootstrap;
    import io.netty.channel.ChannelFuture;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.channel.socket.nio.NioServerSocketChannel;
    import io.netty.handler.codec.LineBasedFrameDecoder;
    import io.netty.handler.codec.string.StringDecoder;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.nio.charset.Charset;
    
    public class MultiHandlerServer {
        private static final Logger logger = LoggerFactory.getLogger(MultiHandlerServer.class);
    
        final int port;
    
        public MultiHandlerServer(final int port) {
            this.port = port;
        }
    
        public void run() throws InterruptedException {
            final NioEventLoopGroup bossGroup = new NioEventLoopGroup();
            final NioEventLoopGroup workerGroup = new NioEventLoopGroup();
            try {
    
                final ServerBootstrap serverBootstrap = new ServerBootstrap()
                        .group(bossGroup, workerGroup)
                        .channel(NioServerSocketChannel.class)
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            protected void initChannel(SocketChannel ch) throws Exception {
                                ch.pipeline().addLast(
                                        new LineBasedFrameDecoder(8192),
                                        new StringDecoder(Charset.forName("UTF-8")),
                                        new MultiHandler01(), new MultiHandler02());
                            }
                        });
    
                final ChannelFuture future = serverBootstrap.bind(port).sync();
                future.channel().closeFuture().sync();
            } finally {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
        }
    
        public static void main(String[] args) throws InterruptedException {
            final MultiHandlerServer client = new MultiHandlerServer(8080);
            client.run();
        }
    }
    
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundMessageHandlerAdapter;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     */
    class MultiHandler01 extends ChannelInboundMessageHandlerAdapter<String> {
        private Logger logger = LoggerFactory.getLogger(MultiHandler01.class);
    
        MultiHandler01() {
        }
    
        @Override
        public void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
            logger.info(String.format("Handler01 receive message: %s", msg));
            ctx.nextInboundMessageBuffer().add(msg);
            ctx.fireInboundBufferUpdated();
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            logger.error("Exception caught: %s", ctx.channel().remoteAddress(), cause);
            ctx.close();
        }
    }
    
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundMessageHandlerAdapter;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     */
    class MultiHandler02 extends ChannelInboundMessageHandlerAdapter<String> {
        private Logger logger = LoggerFactory.getLogger(MultiHandler02.class);
    
        MultiHandler02() {
        }
    
        @Override
        public void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
            logger.info(String.format("Handler02 receive message: %s", msg));
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            logger.error("Exception caught: %s", ctx.channel().remoteAddress(), cause);
            ctx.close();
        }
    }
    
    impo