Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

Netty中childAttr方法设置的值为什么被所有连接共享了?

Netty版本:4.1.6.Final

服务端启动代码如下

final ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap
                .group(boosGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 1024)
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                .childOption(ChannelOption.TCP_NODELAY, true)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) {
                        //心跳监测
                        ch.pipeline().addLast(new IMIdleStateHandler());
                        ch.pipeline().addLast(new ServerHandler());
                    }
                })

                //设置各个连接的指令发送队列
                //问题1:
                .childAttr(Attributes.INSTRUCT_QUEUE, new LinkedList<>())
                .childAttr(Attributes.SENDED, false)
                //问题2:
                .childAttr(Attributes.TEST, "chanmufeng" + System.currentTimeMillis())

问题背景:服务端向硬件设备发送问询指令,由于硬件反馈的格式固化,因此只能遵循一问一答的线性问询方式。为此,我想为每个链接设置一个指令队列,系统定时向队列中添加问询指令(系统中被我抽象成了13个QueryPacket对象)

现在遇到的问题是:

  • 1.在public void channelRead(ChannelHandlerContext ctx, Object msg)函数中打印每个链接的ctx.channel().attr(Attributes.INSTRUCT_QUEUE).get().size()队列长度,实验发现,不同客户端建立链接之后,队列长度会在原来的基础上进行累加,并没有达到各自独立
  • 问题2中我为每个链接设立了一个Attributes.TEST属性,打印发现,每个链接的该属性值对应的都是一样的

请各位不吝赐教,急切求助!!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...