简体   繁体   English

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

[英]Apache camel netty custom encoder and decoder sample

Apache camel netty tcp component doc( http://camel.apache.org/netty.html ) says, Apache camel netty tcp组件doc( http://camel.apache.org/netty.html )说,

encoder 编码器

A custom ChannelHandler class that can be used to perform special marshalling of outbound payloads. Must override org.jboss.netty.channel.ChannelDownStreamHandler.

decoder 解码器

A custom ChannelHandler class that can be used to perform special marshalling of inbound payloads. Must override org.jboss.netty.channel.ChannelUpStreamHandler.

Could you please point me an example on how/what to do in overriding class. 您能给我指出一个关于在上课时如何做/如何做的例子。 I want a custom tcp encoder/decoder to read/write bytes. 我希望自定义tcp编码器/解码器读取/写入字节。

This class and it's super class are encoders and you can use it as an example: org.jboss.netty.handler.codec.string.StringEncoder 该类及其超类是编码器,您可以使用它作为示例: org.jboss.netty.handler.codec.string.StringEncoder

There are other classes used in the examples on the netty page from the "Using multiple codecs" heading which you can look at the source code for to see how they use the interface. 在netty页面上的示例中,“使用多个编解码器”标题中还使用了其他类,您可以查看源代码以了解它们如何使用该接口。

Failing that it's best to look at the netty project and look at the unit tests for the encoders. 如果没有,最好查看netty项目并查看编码器的单元测试。

In the netty documentation there is code for using the ChannelHandler for encoders and decoders. 在netty文档中,有用于将ChannelHandler用于编码器和解码器的代码。 From the documentation: 从文档中:

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

And then you refer to the encoder/decoder: 然后参考编码器/解码器:

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

I would suggest you first take a step back and run your netty flow with textline=true and allowDefaultCodec=false just to see that your netty communication is working. 我建议您先退后一步,使用textline = true和allowDefaultCodec = false运行您的净值流,以查看您的净值通信是否正常。 Then hand the encoder/decoder part. 然后移交给编码器/解码器部分。

Create a SimpleRegistry and pass it to the CamelContext: 创建一个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