netty4注意事项

1 bytebuf已经被池化管理,在netty自带的decode、encode中自己会申请和释放bytebuf.

   在外面使用bytebuf时,需要注意分配bytebuf时,还需要进行手工调用释放操作。

2 nett4的ServerBootstrap设置option采用的是childOption,而netty3直接使用setOption

3 IdleStateHandler.

   netty4使用的读超时、写超时,没有使用时间轮,直接使用的是EventExecutor.

   netty3使用了时间轮。

4 Netty4: MessageToMessageDecoder

   Nett3: OneTonOneDecoder


4 channel

   netty4中channel使用的是metadata,而netty3使用的是attachment.

5 ServerBootstrap

   中的handler只处理tcp linsten和accept消息的处理。

   childHandle只处理和每一个客户端连接socket。

   tcp socket原理请参照http://wenwen.soso.com/z/q157314275.htm

6 码流日志

   io.netty.handler.logging.LoggingHandler


7 在channel的pipline共用的对象需要声明sharable.否则会无法注册通道

   i.e

  ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(group)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 10)
                .option(ChannelOption.SO_REUSEADDR, true)
                .option(ChannelOption.TCP_NODELAY, true)
                .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder(65536, 12, 2, 2, 0, false))
                                .addLast("packageCodec", codec)
                                .addLast("packageHandler", tcpMessageHandlerManager);
                    }

                });

  则codec和tcpMessageHandlerManager均 要声明@ChannelHandler.Sharable

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值