ByteArrayDecoder不是@Sharable处理程序,因此无法多次添加或删除

时间:2015-12-03 14:16:02

标签: apache-camel netty

我正在尝试运行简单的echo服务器进行性能测试。我为我的purporse设置了netty4 tcp端点和ByteArrayDecoder。一切都很好,而只有一个/一个soket被创建。当我想连接第二个客户端,或重新连接第一个客户端时,我不断得到以下错误:

2015-12-03 14:58:08,218 | WARN  | yServerTCPWorker | ChannelInitializer               | 175 - io.netty.common - 4.0.27.Final | Failed to initialize a channel. Closing: [id: 0xe9f9fb16, /127.0.0.1:6056
3 => /127.0.0.1:1542]
io.netty.channel.ChannelPipelineException: io.netty.handler.codec.bytes.ByteArrayDecoder is not a @Sharable handler, so can't be added or removed multiple times.
    at io.netty.channel.DefaultChannelPipeline.checkMultiplicity(DefaultChannelPipeline.java:464)[178:io.netty.transport:4.0.27.Final]
    at io.netty.channel.DefaultChannelPipeline.addLast0(DefaultChannelPipeline.java:136)[178:io.netty.transport:4.0.27.Final]
    at io.netty.channel.DefaultChannelPipeline.addLast(DefaultChannelPipeline.java:129)[178:io.netty.transport:4.0.27.Final]
    at io.netty.channel.DefaultChannelPipeline.addLast(DefaultChannelPipeline.java:120)[178:io.netty.transport:4.0.27.Final]
    at org.apache.camel.component.netty4.DefaultServerInitializerFactory.addToPipeline(DefaultServerInitializerFactory.java:118)[83:org.apache.camel.camel-netty4:2.16.0]
    at org.apache.camel.component.netty4.DefaultServerInitializerFactory.initChannel(DefaultServerInitializerFactory.java:100)[83:org.apache.camel.camel-netty4:2.16.0]
    at io.netty.channel.ChannelInitializer.channelRegistered(ChannelInitializer.java:69)[178:io.netty.transport:4.0.27.Final]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRegistered(AbstractChannelHandlerContext.java:162)[178:io.netty.transport:4.0.27.Final]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRegistered(AbstractChannelHandlerContext.java:148)[178:io.netty.transport:4.0.27.Final]
    at io.netty.channel.DefaultChannelPipeline.fireChannelRegistered(DefaultChannelPipeline.java:734)[178:io.netty.transport:4.0.27.Final]
    at io.netty.channel.AbstractChannel$AbstractUnsafe.register0(AbstractChannel.java:450)[178:io.netty.transport:4.0.27.Final]
    at io.netty.channel.AbstractChannel$AbstractUnsafe.access$100(AbstractChannel.java:378)[178:io.netty.transport:4.0.27.Final]
    at io.netty.channel.AbstractChannel$AbstractUnsafe$1.run(AbstractChannel.java:424)[178:io.netty.transport:4.0.27.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:357)[175:io.netty.common:4.0.27.Final]
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)[178:io.netty.transport:4.0.27.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:111)[175:io.netty.common:4.0.27.Final]
    at java.lang.Thread.run(Thread.java:745)[:1.8.0_25]

我正在与ByteArrayDecoder打交道,无论如何都发现它@Sharable。虽然我创建的工厂应该返回ByteArrayDecoder的新实例,但它也没有帮助。我比较了Karaf上依赖模块的版本,看起来它们是相同的。

在我的蓝图下面

<!--bean id="decoder" class="io.netty.handler.codec.bytes.ByteArrayDecoder"/-->

<!--bean id="decoder" class="com.company.feature.ChannelHandlerFactoryByteArrayDecoder" factory-method="newChannelHandler"/-->
<bean id="factory" class="com.company.feature.ChannelHandlerFactoryByteArrayDecoder" />
<bean id="decoder" class="io.netty.handler.codec.bytes.ChannelInboundHandlerAdapter" factory-ref="factory" factory-method="newChannelHandler"/>

<bean id="process" class="com.company.feature.Process"/>

<camelContext id="camel_netty_tcp_test" xmlns="http://camel.apache.org/schema/blueprint" allowUseOriginalMessage="false">  

    <route id="featureRoute">
        <from uri="{{feature.in_route}}"/>
        <process ref="process"/>
        <log message="Received"/>
    </route>
</camelContext>

我使用的工厂类

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import org.apache.camel.component.netty4.ChannelHandlerFactory;
import io.netty.channel.ChannelHandler;

public class ChannelHandlerFactoryByteArrayDecoder implements ChannelHandlerFactory {

public ChannelHandler newChannelHandler() {
  return (ChannelHandler) new ByteArrayDecoder();
}

public void handlerAdded(ChannelHandlerContext chc) throws Exception {
  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

public void handlerRemoved(ChannelHandlerContext chc) throws Exception {
  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

public void exceptionCaught(ChannelHandlerContext chc, Throwable thrwbl) throws Exception {
  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}

1 个答案:

答案 0 :(得分:2)

为了避免此错误,我现在从ByteArrayDecoder类继承并按如下方式实现。

import io.netty.channel.ChannelHandler;
import io.netty.handler.codec.bytes.ByteArrayDecoder;


@ChannelHandler.Sharable
public class MyByteArrayDecoder extends ByteArrayDecoder {

}

在工厂中重新获得返回类型后,一切都开始工作。