Java Apache camel netty自定义编码器和解码器示例
Apache camel netty tcp组件doc()说 编码器Java Apache camel netty自定义编码器和解码器示例,java,tcp,apache-camel,netty,Java,Tcp,Apache Camel,Netty,Apache camel netty tcp组件doc()说 编码器 A custom ChannelHandler class that can be used to perform special marshalling of outbound payloads. Must override org.jboss.netty.channel.ChannelDownStreamHandler. 译码器 A custom ChannelHandler class that can be used
A custom ChannelHandler class that can be used to perform special marshalling of outbound payloads. Must override org.jboss.netty.channel.ChannelDownStreamHandler.
译码器
A custom ChannelHandler class that can be used to perform special marshalling of inbound payloads. Must override org.jboss.netty.channel.ChannelUpStreamHandler.
你能给我举个例子说明在重写类中如何/怎么做吗。我需要一个自定义的tcp编码器/解码器来读/写字节。这个类和它的超类都是编码器,您可以使用它作为示例: netty页面上的示例中使用了“使用多个编解码器”标题中的其他类,您可以查看源代码以了解它们如何使用接口
如果不能做到这一点,最好看看netty项目和编码器的单元测试。在netty文档中,有用于编码器和解码器的ChannelHandler代码。从文件中:
ChannelHandlerFactory lengthDecoder = ChannelHandlerFactories.newLengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4);
StringDecoder stringDecoder = new StringDecoder();
registry.bind("length-decoder", lengthDecoder);
registry.bind("string-decoder", stringDecoder);
LengthFieldPrepender lengthEncoder = new LengthFieldPrepender(4);
StringEncoder stringEncoder = new StringEncoder();
registry.bind("length-encoder", lengthEncoder);
registry.bind("string-encoder", stringEncoder);
List<ChannelHandler> decoders = new ArrayList<ChannelHandler>();
decoders.add(lengthDecoder);
decoders.add(stringDecoder);
List<ChannelHandler> encoders = new ArrayList<ChannelHandler>();
encoders.add(lengthEncoder);
encoders.add(stringEncoder);
registry.bind("encoders", encoders);
registry.bind("decoders", decoders);
我建议你先退一步,用textline=true和allowDefaultCodec=false运行netty流,看看netty通信是否正常。然后交给编码器/解码器部分。创建一个简单注册表并将其传递给上下文:
SimpleRegistry simpleRegistry = new SimpleRegistry();
simpleRegistry.put("stringEncoder", new StringEncoder());
simpleRegistry.put("stringDecoder", new StringDecoder());
CamelContext context = new DefaultCamelContext(simpleRegistry);
还有一个关于《骆驼行动手册》的小例子,源代码免费提供:
SimpleRegistry simpleRegistry = new SimpleRegistry();
simpleRegistry.put("stringEncoder", new StringEncoder());
simpleRegistry.put("stringDecoder", new StringDecoder());
CamelContext context = new DefaultCamelContext(simpleRegistry);