这是我的骆驼路线的一部分
<bean class="io.netty.handler.codec.string.StringEncoder" id="stringEncoder">
<constructor-arg value="UTF-8"/>
</bean>
<camelContext xmlns="http://camel.apache.org/schema/spring" id="camelContext">
<route>
<from uri="direct:startMessageSend"/>
<to uri="netty4:udp://localhost:5000?encoder=#stringEncoder&sync=false"/>
</route>
</camelContext>
当我将一个简单的字符串发送到此端点时。
String udpMsgBody = "hello world!";
template.sendBody(udpMsgBody);
我得到以下例外。
Caused by: java.lang.UnsupportedOperationException: unsupported message type: DefaultAddressedEnvelope (expected: DatagramPacket, AddressedEnvelope<ByteBuf, SocketAddress>, ByteBuf)
at io.netty.channel.socket.nio.NioDatagramChannel.filterOutboundMessage(NioDatagramChannel.java:324)
at io.netty.channel.AbstractChannel$AbstractUnsafe.write(AbstractChannel.java:697)
at io.netty.channel.DefaultChannelPipeline$HeadContext.write(DefaultChannelPipeline.java:1114)
at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:705)
at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:763)
at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:698)
at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:102)
at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:705)
at io.netty.channel.AbstractChannelHandlerContext.access$1900(AbstractChannelHandlerContext.java:32)
at io.netty.channel.AbstractChannelHandlerContext$AbstractWriteTask.write(AbstractChannelHandlerContext.java:980)
at io.netty.channel.AbstractChannelHandlerContext$WriteAndFlushTask.write(AbstractChannelHandlerContext.java:1032)
at io.netty.channel.AbstractChannelHandlerContext$AbstractWriteTask.run(AbstractChannelHandlerContext.java:965)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:357)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:111)
at java.lang.Thread.run(Thread.java:744)
但是,当我将Exchange主体作为byteBuf发送时,我没有异常,并且通过UDP发送消息。
ByteBuf bbuffer = Unpooled.copiedBuffer(udpMsgBody.getBytes());
template.sendBody(udpMsgBody);
我的问题是即使我使用正确的StringEncoder,为什么我不能通过netty4发送字符串?
注意我使用的是camel 2.15.2