Apache camel netty tcp组件doc(http://camel.apache.org/netty.html)说,
编码器
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编码器/解码器来读/写字节。
答案 0 :(得分:1)
这个类及它的超类是编码器,你可以用它作为例子:org.jboss.netty.handler.codec.string.StringEncoder
在“使用多个编解码器”标题的netty页面上的示例中使用了其他类,您可以查看源代码以了解它们如何使用该接口。
如果没有,最好查看netty项目并查看编码器的单元测试。
答案 1 :(得分:1)
在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);
然后你参考编码器/解码器:
from("netty4:tcp://localhost:{{port}}?decoders=#length-decoder,#string-decoder&sync=false")
我建议您先退后一步,使用textline = true和allowDefaultCodec = false运行您的netty流程,看看您的网络通信是否正常工作。然后交出编码器/解码器部分。
答案 2 :(得分:0)
创建一个SimpleRegistry并将其传递给CamelContext:
SimpleRegistry simpleRegistry = new SimpleRegistry();
simpleRegistry.put("stringEncoder", new StringEncoder());
simpleRegistry.put("stringDecoder", new StringDecoder());
CamelContext context = new DefaultCamelContext(simpleRegistry);