簡體   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