繁体   English   中英

Apache camel netty定制编码器和解码器示例

[英]Apache camel netty custom encoder and decoder sample

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编码器/解码器读取/写入字节。

该类及其超类是编码器,您可以使用它作为示例: org.jboss.netty.handler.codec.string.StringEncoder

在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);

然后参考编码器/解码器:

from("netty4:tcp://localhost:{{port}}?decoders=#length-decoder,#string-decoder&sync=false")

我建议您先退后一步,使用textline = true和allowDefaultCodec = false运行您的净值流,以查看您的净值通信是否正常。 然后移交给编码器/解码器部分。

创建一个SimpleRegistry并将其传递给CamelContext:

SimpleRegistry simpleRegistry = new SimpleRegistry();
simpleRegistry.put("stringEncoder", new StringEncoder());
simpleRegistry.put("stringDecoder", new StringDecoder());
CamelContext context = new DefaultCamelContext(simpleRegistry);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM