is not a @Sharable handler解决方法

昨天在写编码器的时候,因为是和spring整合,因此在使用编码的时候用Autowired自动注入

@Autowired
private ProtocolDecoder protocolDecoder ;

@Autowired
private ProtocolEncoder protocolEncoder;

结果在多个客户端连接(其实不是多客户端的问题)的时候导致一直在报错,如下

io.netty.channel.ChannelPipelineException: com.sim.server.game.net.coder.decoder.ProtocolDecoder is not a @Sharable handler, so can't be added or removed multiple times.

于是我就自作聪明的将ProtocolDecoder上加了个@Sharable注解,结果在启动的时候就报错了。

Caused by: java.lang.IllegalStateException: ChannelHandler com.sim.server.game.net.coder.decoder.ProtocolDecoder is not allowed to be shared

最后的解决方法是,不要使用单例了,每次添加handler的时候直接new

        pipeline.addLast("decoder",new ProtocolDecoder() );
        pipeline.addLast("encoder",new ProtocolEncoder()) ;

当然如果是在ChannelInitializer的子类报错说is not a @Sharable handler,一般情况加上@Sharable注解即可。

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值