java.lang.IllegalArgumentException:处理程序名称重复:netty http 服务器中的解码器

java.lang.IllegalArgumentException: Duplicate handler name: decoder in netty http server

我正在尝试创建一个 netty-jersey http 应用程序,代码在 github 分支重复处理程序 here.

当我 运行 主程序 NettyJerseyDemoApp 时,代码编译并且 运行 没问题 NettyJerseyDemoApp

当我使用任何 http 请求 curl http://localhost:8003/hellonetty 或在浏览器中调用此应用程序时,出现以下异常 -

    Nov 26, 2018 12:22:04 PM io.netty.channel.ChannelInitializer exceptionCaught
    WARNING: Failed to initialize a channel. Closing: [id: 0xcbb67225, L:/127.0.0.1:8003 - R:/127.0.0.1:62568]
    java.lang.IllegalArgumentException: Duplicate handler name: decoder
        at io.netty.channel.DefaultChannelPipeline.checkDuplicateName(DefaultChannelPipeline.java:1101)
        at io.netty.channel.DefaultChannelPipeline.filterName(DefaultChannelPipeline.java:302)
        at io.netty.channel.DefaultChannelPipeline.addLast(DefaultChannelPipeline.java:210)
        at io.netty.channel.DefaultChannelPipeline.addLast(DefaultChannelPipeline.java:201)
        at com.demo.netty.jersey.JaxRsServerChannelPipelineFactory.initChannel(JaxRsServerChannelPipelineFactory.java:41)
        at com.demo.netty.jersey.JaxRsServerChannelPipelineFactory.initChannel(JaxRsServerChannelPipelineFactory.java:12)
        at io.netty.channel.ChannelInitializer.initChannel(ChannelInitializer.java:115)
        at io.netty.channel.ChannelInitializer.handlerAdded(ChannelInitializer.java:107)
        at io.netty.channel.DefaultChannelPipeline.callHandlerAdded0(DefaultChannelPipeline.java:637)
        at io.netty.channel.DefaultChannelPipeline.access[=11=]0(DefaultChannelPipeline.java:46)
        at io.netty.channel.DefaultChannelPipeline$PendingHandlerAddedTask.execute(DefaultChannelPipeline.java:1487)
        at io.netty.channel.DefaultChannelPipeline.callHandlerAddedForAllHandlers(DefaultChannelPipeline.java:1161)
        at io.netty.channel.DefaultChannelPipeline.invokeHandlerAddedIfNeeded(DefaultChannelPipeline.java:686)
        at io.netty.channel.AbstractChannel$AbstractUnsafe.register0(AbstractChannel.java:510)
        at io.netty.channel.AbstractChannel$AbstractUnsafe.access0(AbstractChannel.java:423)
        at io.netty.channel.AbstractChannel$AbstractUnsafe.run(AbstractChannel.java:482)
        at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:464)
        at io.netty.util.concurrent.SingleThreadEventExecutor.run(SingleThreadEventExecutor.java:884)
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
        at java.lang.Thread.run(Thread.java:748)

我的频道初始化逻辑在here

我不确定为什么会出现此错误。我该如何解决这个异常?

问题出在这段代码中:

@Override
protected void initChannel(SocketChannel ch) {
    pipeline = ch.pipeline();
    pipeline.addLast("encoder", new HttpResponseEncoder());
    pipeline.addLast("decoder", new HttpRequestDecoder());
    pipeline.addLast("aggregator", new HttpObjectAggregator(1024));
    pipeline.addLast("jerseyHandler", jerseyHandler);

}

同一个 JaxRsServerChannelPipelineFactory 实例用于所有接受的连接 (Channels),因此代码不是线程安全的。这里的问题是,您可以存储对 ChannelPipeline 的引用,但随后会同时访问它,这可能会导致您多次添加相同的处理程序。