Details
-
Bug
-
Status: Resolved
-
Minor
-
Resolution: Fixed
-
None
-
None
-
Unknown
Description
Recently I tried to set up a simple TCP proxy relaying any byte stream leveraging the Netty component, just like:
///usr/bin/env jbang "$0" "$@" ; exit $? //DEPS org.apache.camel:camel-bom:4.2.0@pom //DEPS org.apache.camel:camel-core //DEPS org.apache.camel:camel-main //DEPS org.apache.camel:camel-netty //DEPS org.apache.camel:camel-support import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.netty.ChannelHandlerFactories; import org.apache.camel.main.Main; import org.apache.camel.support.DefaultRegistry; class proxy { public static void main(String... args) throws Exception { Main main = new Main(); main.configure().addRoutesBuilder(new RouteBuilder() { public void configure() throws Exception { DefaultRegistry registry = (DefaultRegistry) getContext().getRegistry(); registry.bind("bytesDecoder", ChannelHandlerFactories.newByteArrayDecoder("tcp")); registry.bind("bytesEncoder", ChannelHandlerFactories.newByteArrayEncoder("tcp")); from("netty:tcp://0.0.0.0:1234?decoders=#bytesDecoder&encoders=#bytesEncoder") .to("netty:tcp://localhost:5678?decoders=#bytesDecoder&encoders=#bytesEncoder") ; } }); main.run(); } }
But sending data through this proxy fails with the following messages, and the client doesn't get any response:
WARNING: Failed to initialize a channel. Closing: [id: 0x45bcede1] 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:600) at io.netty.channel.DefaultChannelPipeline.addLast(DefaultChannelPipeline.java:202) at io.netty.channel.DefaultChannelPipeline.addLast(DefaultChannelPipeline.java:195) at org.apache.camel.component.netty.DefaultClientInitializerFactory.addToPipeline(DefaultClientInitializerFactory.java:102) at org.apache.camel.component.netty.DefaultClientInitializerFactory.initChannel(DefaultClientInitializerFactory.java:72) ...
This is because ChannelHandlerFactories wraps ByteArrayDecoder with ShareableChannelHandlerFactory, but it's not actually sharable (doesn't have a @ChannelHandler.Sharable annotation).
A factory class that returns a new ByteArrayDecoder instance for each newByteArrayDecoder() call is required here, as the document explained.
Attachments
Issue Links
- links to