❤️ 个人主页: 水滴技术
🌸 订阅专栏: 成功解决 BUG 合集
🚀 支持水滴: 点赞👍 + 收藏⭐ + 留言💬
问题描述
使用 SpringBoot 集成 Netty 时,报如下错误:
io.netty.channel.ChannelPipelineException: ***Handler is not a @Sharable handler, so can't be added or removed multiple times.
at io.netty.channel.DefaultChannelPipeline.checkMultiplicity(DefaultChannelPipeline.java:600) [netty-transport-4.1.82.Final.jar:4.1.82.Final]
at io.netty.channel.DefaultChannelPipeline.addLast(DefaultChannelPipeline.java:202) [netty-transport-4.1.82.Final.jar:4.1.82.Final]
at io.netty.channel.DefaultChannelPipeline.addLast(DefaultChannelPipeline.java:381) [netty-transport-4.1.82.Final.jar:4.1.82.Final]
at ***ChannelInitializer.initChannel(GeneralChannelInitializer.java:27) ~[classes/:na]
at ***ChannelInitializer.initChannel(GeneralChannelInitializer.java:15) ~[classes/:na]
at io.netty.channel.ChannelInitializer.initChannel(ChannelInitializer.java:129) [netty-transport-4.1.82.Final.jar:4.1.82.Final]
at io.netty.channel.ChannelInitializer.handlerAdded(ChannelInitializer.java:112) [netty-transport-4.1.82.Final.jar:4.1.82.Final]
at io.netty.channel.AbstractChannelHandlerContext.callHandlerAdded(AbstractChannelHandlerContext.java:938) [netty-transport-4.1.82.Final.jar:4.1.82.Final]
at io.netty.channel.DefaultChannelPipeline.callHandlerAdded0(DefaultChannelPipeline.java:609) [netty-transport-4.1.82.Final.jar:4.1.82.Final]
at io.netty.channel.DefaultChannelPipeline.access$100(DefaultChannelPipeline.java:46) [netty-transport-4.1.82.Final.jar:4.1.82.Final]
at io.netty.channel.DefaultChannelPipeline$PendingHandlerAddedTask.execute(DefaultChannelPipeline.java:1463) [netty-transport-4.1.82.Final.jar:4.1.82.Final]
at io.netty.channel.DefaultChannelPipeline.callHandlerAddedForAllHandlers(DefaultChannelPipeline.java:1115) [netty-transport-4.1.82.Final.jar:4.1.82.Final]
at io.netty.channel.DefaultChannelPipeline.invokeHandlerAddedIfNeeded(DefaultChannelPipeline.java:650) [netty-transport-4.1.82.Final.jar:4.1.82.Final]
at io.netty.channel.AbstractChannel$AbstractUnsafe.register0(AbstractChannel.java:514) [netty-transport-4.1.82.Final.jar:4.1.82.Final]
at io.netty.channel.AbstractChannel$AbstractUnsafe.access$200(AbstractChannel.java:429) [netty-transport-4.1.82.Final.jar:4.1.82.Final]
at io.netty.channel.AbstractChannel$AbstractUnsafe$1.run(AbstractChannel.java:486) [netty-transport-4.1.82.Final.jar:4.1.82.Final]
at io.netty.util.concurrent.AbstractEventExecutor.runTask$$$capture(AbstractEventExecutor.java:174) [netty-common-4.1.82.Final.jar:4.1.82.Final]
at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java) [netty-common-4.1.82.Final.jar:4.1.82.Final]
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute$$$capture(AbstractEventExecutor.java:167) [netty-common-4.1.82.Final.jar:4.1.82.Final]
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java) [netty-common-4.1.82.Final.jar:4.1.82.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:470) [netty-common-4.1.82.Final.jar:4.1.82.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:569) [netty-transport-4.1.82.Final.jar:4.1.82.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) [netty-common-4.1.82.Final.jar:4.1.82.Final]
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) [netty-common-4.1.82.Final.jar:4.1.82.Final]
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) [netty-common-4.1.82.Final.jar:4.1.82.Final]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_251]
原因分析
错误信息的意思是自定义的 Handler 不是 @Sharable ,所以不能多次添加或删除。这是为什么呢,看了下源码找到了答案。
注释的大体意思是:
可以将带该注释的ChannelHandler
的同一个实例多次添加到一个或多个ChannelPipeline
,而不存在竞争条件。
如果未指定该注解,每次添加ChannelPipeline
时必须创建一个新的ChannelHandler
实例,因为它具有成员变量等非共享状态。
解决方案
有两种解决方案:
方案一:增加@ChannelHandler.Sharable
注解
在自定义的 Handler 上增加
@ChannelHandler.Sharable
注解。
方案二:
在 pipelin 中改为
new
创建实例。
热门专栏
👍 《Java 教程:从入门到精通》
👍 《MySQL 教程:从入门到精通》