Java 在netty 4.0中选择simplehandler或executor

Java 在netty 4.0中选择simplehandler或executor,java,netty,Java,Netty,考虑以下服务器引导代码: ChannelFuture f; ServerBootstrap b = new ServerBootstrap(); try { b.group(new NioEventLoopGroup(), new NioEventLoopGroup()) .channel(NioServerSocketChannel.class) .localAddress(1234) .childOption(

考虑以下服务器引导代码:

ChannelFuture f;
ServerBootstrap b = new ServerBootstrap();

try {
    b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
            .channel(NioServerSocketChannel.class)
            .localAddress(1234)
            .childOption(ChannelOption.TCP_NODELAY, true)
            .childHandler(new MyChannelInitializer(new DefaultEventExecutorGroup(10)));

    f = b.bind().sync();
    f.channel().closeFuture().sync();
}
MyChannelInitializer.java

public class MyChannelInitializer  extends ChannelInitializer<SocketChannel> {
    private EventExecutorGroup executorGroup;

    public MyChannelInitializer(EventExecutorGroup _executorGroup) {
        executorGroup = _executorGroup;
    }

    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.nulDelimiter()));
        pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
        pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));

        // and then business logic.
        pipeline.addLast(this.executorGroup, "handler", new MyHandshakeHandler());
    }    
}
公共类MyChannelInitializer扩展了ChannelInitializer{
私人事件执行组执行组;
公共MyChannelInitializer(EventExecutorGroup\u executorGroup){
executorGroup=_executorGroup;
}
@凌驾
public void initChannel(SocketChannel ch)引发异常{
ChannelPipeline=通道管道();
pipeline.addLast(“framer”,新的DelimiterBasedFrameDecoder(8192,Delimiters.nulDelimiter());
pipeline.addLast(“解码器”,新的StringDecoder(CharsetUtil.UTF_8));
addLast(“编码器”,新的StringEncoder(CharsetUtil.UTF_8));
//然后是业务逻辑。
pipeline.addLast(this.executorGroup,“handler”,新的MyHandshakeHandler());
}    
}
现在-MyHandshakeHandler()侦听消息,这些消息需要与数据库交互

在我们继续之前,上面的代码是正确的方法吗?(即-我使用EventExecutorGroup处理此阻塞类型的处理程序的方式)

现在假设它是正确的,我的问题是——虽然MyHandshakeHandler()确实需要与数据库交互,但这只在与客户端的初始协商期间和频道关闭时发生。剩下的时间——即握手完成后和通道关闭前——只需要跳出不需要数据库的ping/pong/heartbeat/keepalive类型的消息。因此,A)应该是一个单独的处理程序(我们称之为“MyPingHandler”),在MyHandshakeHandler之前添加到管道中,还是B)我应该将该逻辑添加到MyHandshakeHandler中

如果A)如何阻止消息进一步传播,以避免不必要地调用MyHandsheHandler,除非它是通道关闭事件(即channelInactive())?另外一点是,如果MyPingHandler只是在握手完成后才添加到管道中,这样就不会不必要地调用它了

如果B)我不理解EventExecutorGroup在本案中的目的。这些连接是此服务器将为其供电的唯一类型。。。。因此,对我来说,设置一组专门的线程(将用于每个处理程序)而不仅仅是使用默认值似乎很奇怪。因此,如果B是解决方法——对于这种特定情况——我是否应该在不使用EventExecutorGroup的情况下将处理程序添加到管道中(如果不是,为什么不是)?

答案是(A)

要将事件传播到下一个处理程序,通常需要调用
ctx.fireXXX()
方法。如果不调用其中任何一个,事件将不会传播。所以,在您的情况下,在大多数情况下,您可以通过不调用
ctx.fireXXX()
方法在握手完成后吞下事件


此外,还可以动态配置管道。您可以随时在管道中添加或删除处理程序。因此,您可以在握手结束后删除握手处理程序,也可以在握手结束后添加ping处理程序(或者两者都添加)。

谢谢-不过,我相信入站消息是通过覆盖messageReceived()接收的,而不是通过事件接收的。因此,不调用ctx.fireXXX()是否仍会阻止管道中的下一个处理程序?