【问题标题】:Netty 4 read/write in handler multiple timesNetty 4在处理程序中多次读/写
【发布时间】:2013-07-03 18:23:27
【问题描述】:

我是 Netty 的新手,我决定从 4.0.0 开始,因为我认为它应该更好,因为它更新。我的服务器应用程序应该从 gps 设备接收数据,过程是这样的 - 首先我收到 2 个字节,这是设备 imei 的长度,然后我收到具有该长度的 imei,然后我应该发送 0x01 到设备,如果我想接受它的数据。在我的应答设备使用 AVL 协议向我发送 gps 数据后。现在我的服务器在没有 Netty 的情况下工作,我想将其更改为与 netty 一起工作。 这就是我所做的:

我已经创建了这样的服务器类

public class BusDataReceiverServer {

private final int port;
private final Logger LOG = LoggerFactory.getLogger(BusDataReceiverServer.class);

public BusDataReceiverServer(int port) {
    this.port = port;
}

public void run() throws Exception {
    LOG.info("running thread");
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try{
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new BusDataReceiverInitializer());
        b.bind(port).sync().channel().closeFuture().sync();
    }catch (Exception ex){
        LOG.info(ex.getMessage());
    }
    finally {
        LOG.info("thread closed");
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

public static void main(String[] args) throws Exception {
    new BusDataReceiverServer(3129).run();
}

}

并创建了初始化类

public class BusDataReceiverInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();

        pipeline.addLast("imeiDecoder", new ImeiDecoder());
        pipeline.addLast("busDataDecoder", new BusDataDecoder());
        pipeline.addLast("encoder", new ResponceEncoder());

        pipeline.addLast("imeiHandler", new ImeiReceiverServerHandler());
        pipeline.addLast("busDataHandler", new BusDataReceiverServerHandler());
    }
}

然后我创建了解码器和编码器以及 2 个处理程序。我的imeiDecoder 和编码器以及ImeiReceiverServerHandler 正在工作。这是我的 ImeiReceiverServerHandler

public class ImeiReceiverServerHandler extends ChannelInboundHandlerAdapter {

    private final Logger LOG = LoggerFactory.getLogger(ImeiReceiverServerHandler.class);

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageList<Object> msgs) throws Exception {
        MessageList<String> imeis = msgs.cast();
        String imei = imeis.get(0);

        ctx.write(Constants.BUS_DATA_ACCEPT);
        ctx.fireMessageReceived(msgs);

    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);    //To change body of overridden methods use File | Settings | File Templates.
    }
}

现在,接受后我不明白如何继续接收 gps 数据并将其转发给处理程序 BusDataReceiverServerHandler。 如果有人可以帮助我或可以为我提供有用的文档,我将非常感激。或者,如果可以使用 Netty 3 做到这一点,我也将不胜感激。

【问题讨论】:

    标签: java networking netty


    【解决方案1】:

    我没有使用过 Netty 4,所以我不确定我的答案是否 100% 准确或在 Netty 4 中做事的最佳方式,但您需要做的是跟踪连接/客户端会话的状态以便知道何时将消息转发给您的第二个处理程序。

    例如

    private enum HandlerState { INITIAL, IMEI_RECEIVED; }
    private HandlerState state = HandlerState.INITIAL;
    
    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageList<Object> msgs) throws Exception
    {
    
        if (state == HandlerState.INITIAL)
        {
            MessageList<String> imeis = msgs.cast();
            String imei = imeis.get(0);
    
            ctx.write(Constants.BUS_DATA_ACCEPT);
            state = HandlerState.IMEI_RECEIVED;
        } else
        {
            // Forward message to next handler...
            // Not sure exactly how this is done in Netty 4
            // Maybe: ctx.fireMessageReceived(msgs);
            // Or maybe it is:
            // ctx.nextInboundMessageBuffer().add(msg);
            // ctx.fireInboundBufferUpdated();
    
            // I believe you could also remove the IMEI handler from the
            // pipeline instead of having it keep state, if it is not going to do anything
            // further.
        }
    
    }
    

    因此,要么在处理程序中跟踪状态,要么在处理程序完成后将其从管道中删除,如果它不会被进一步使用。跟踪状态时,您可以将状态保留在处理程序本身中(如上所示),或者将状态变量保留在上下文/属性映射中(但是在 netty 4 中完成)。

    不将状态保留在处理程序本身的原因是,如果您要使处理程序可共享(一个实例跨多个通道使用)。没有必要这样做,但如果您有大量并发通道,可能会节省一些资源。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 2013-03-06
      • 1970-01-01
      • 2014-10-07
      • 1970-01-01
      • 2015-06-07
      相关资源
      最近更新 更多