229.5.4. 複数のコーデックの使用
場合によっては、エンコーダーとデコーダーのチェーンを netty パイプラインに追加する必要がある場合があります。multpile codecs を camel netty エンドポイントに追加するには、'encoders' および 'decoders' uri パラメーターを使用する必要があります。'encoder' および 'decoder' パラメーターと同様に、パイプラインに追加する必要のある参照(ChannelUpstreamHandlers および ChannelDownstreamHandler のリスト)を指定するために使用されます。エンコーダーが指定されている場合は、デコーダーやデコーダーパラメーターと同様にエンコーダーパラメーターが無視されることに注意してください。
共有不可能なエンコーダー/デコーダーの使用についてさらに詳しくお読みください。
エンドポイントの作成時に解決できるようにコーデックのリストを Camel のレジストリーに追加する必要があります。
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);
Spring のネイティブコレクションサポートを使用して、アプリケーションコンテキストでコーデックリストを指定することができます。
<util:list id="decoders" list-class="java.util.LinkedList"> <bean class="org.apache.camel.component.netty4.ChannelHandlerFactories" factory-method="newLengthFieldBasedFrameDecoder"> <constructor-arg value="1048576"/> <constructor-arg value="0"/> <constructor-arg value="4"/> <constructor-arg value="0"/> <constructor-arg value="4"/> </bean> <bean class="io.netty.handler.codec.string.StringDecoder"/> </util:list> <util:list id="encoders" list-class="java.util.LinkedList"> <bean class="io.netty.handler.codec.LengthFieldPrepender"> <constructor-arg value="4"/> </bean> <bean class="io.netty.handler.codec.string.StringEncoder"/> </util:list> <bean id="length-encoder" class="io.netty.handler.codec.LengthFieldPrepender"> <constructor-arg value="4"/> </bean> <bean id="string-encoder" class="io.netty.handler.codec.string.StringEncoder"/> <bean id="length-decoder" class="org.apache.camel.component.netty4.ChannelHandlerFactories" factory-method="newLengthFieldBasedFrameDecoder"> <constructor-arg value="1048576"/> <constructor-arg value="0"/> <constructor-arg value="4"/> <constructor-arg value="0"/> <constructor-arg value="4"/> </bean> <bean id="string-decoder" class="io.netty.handler.codec.string.StringDecoder"/>
その後、bean 名は netty エンドポイント定義で、コンマ区切りのリストとして、または List(例: List)を含めることができます。
from("direct:multiple-codec").to("netty4:tcp://localhost:{{port}}?encoders=#encoders&sync=false"); from("netty4:tcp://localhost:{{port}}?decoders=#length-decoder,#string-decoder&sync=false").to("mock:multiple-codec");
または XML を使用します。
<camelContext id="multiple-netty-codecs-context" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct:multiple-codec"/> <to uri="netty4:tcp://localhost:5150?encoders=#encoders&sync=false"/> </route> <route> <from uri="netty4:tcp://localhost:5150?decoders=#length-decoder,#string-decoder&sync=false"/> <to uri="mock:multiple-codec"/> </route> </camelContext>