2016-07-06 133 views
0

我有一个使用TCP套接字连接与COBOL服务器进行通信的骆驼路由。这条路线基本上是这样的:如何让Netty客户端读取整个响应

<route> 
     <from uri="direct:sendMessage" /> 
     <log message="Sending message to server: ${body}" /> 
     <to uri="netty4:tcp://hostname:1234?requestTimeout=2000&amp;encoding=windows-1252" /> 
     <log message="Received response from server: ${body}" /> 
</route> 

调用以下异常这条路的结果:

Failed delivery for (MessageId: ID-vmuxfusepoc01-34999-1467706512020-14-2 on ExchangeId: ID-vmuxfusepoc01-34999-1467706512020-14-1). Exhausted after delivery attempt: 1 caught: io.netty.handler.codec.TooLongFrameException: Adjusted frame length exceeds 1048576: 1008807217 - discarded 

Message History 
--------------------------------------------------------------------------------------------------------------------------------------- 
RouteId    ProcessorId   Processor                  Elapsed (ms) 
[sendMessage  ] [to51    ] [direct:sendMessage               ] [  697] 
[sendMessage  ] [log60    ] [log                   ] [   0] 
[sendMessage  ] [to53    ] [netty4:tcp://jadev:7616?requestTimeout=2000&encoding=windows-1252    ] [  691] 

Exchange 
--------------------------------------------------------------------------------------------------------------------------------------- 
Exchange[ 
    Id     ID-vmuxfusepoc01-34999-1467706512020-14-1 
    ExchangePattern  InOut 
    Headers    {breadcrumbId=ID-vmuxfusepoc01-34999-1467706512020-14-2, CamelHttpMethod=POST, CamelHttpPath=, CamelHttpQuery=null, CamelHttpServletRequest=(POST /message)@927654619 [email protected], CamelHttpServletResponse=HTTP/1.1 500 
Content-Type: text/plain;charset=ISO-8859-1 

, CamelHttpUri=/message, CamelHttpUrl=http://localhost:8282/message, CamelRedelivered=false, CamelRedeliveryCounter=0, CamelServletContextPath=/message, Content-Length=690, Content-Type=null, Host=localhost:8282} 
    BodyType   String 
    Body    uid:dev2016070412325814024903532ºS:bookinfoºbkd0001:017953491ºcmn0001:wdedul01 
] 

Stacktrace 
--------------------------------------------------------------------------------------------------------------------------------------- 

io.netty.handler.codec.TooLongFrameException: Adjusted frame length exceeds 1048576: 1008807217 - discarded 
    at io.netty.handler.codec.LengthFieldBasedFrameDecoder.fail(LengthFieldBasedFrameDecoder.java:501)[io.netty:netty-codec:4.0.27.Final] 
    at io.netty.handler.codec.LengthFieldBasedFrameDecoder.failIfNecessary(LengthFieldBasedFrameDecoder.java:477)[io.netty:netty-codec:4.0.27.Final] 
    at io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:403)[io.netty:netty-codec:4.0.27.Final] 
    at io.netty.handler.codec.serialization.ObjectDecoder.decode(ObjectDecoder.java:69)[io.netty:netty-codec:4.0.27.Final] 
    at io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:343)[io.netty:netty-codec:4.0.27.Final] 
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:315)[io.netty:netty-codec:4.0.27.Final] 
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:229)[io.netty:netty-codec:4.0.27.Final] 
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:339)[io.netty:netty-transport:4.0.27.Final] 
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:324)[io.netty:netty-transport:4.0.27.Final] 
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:847)[io.netty:netty-transport:4.0.27.Final] 
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131)[io.netty:netty-transport:4.0.27.Final] 
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)[io.netty:netty-transport:4.0.27.Final] 
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)[io.netty:netty-transport:4.0.27.Final] 
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382)[io.netty:netty-transport:4.0.27.Final] 
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)[io.netty:netty-transport:4.0.27.Final] 
    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:111)[io.netty:netty-common:4.0.27.Final] 
    at java.lang.Thread.run(Thread.java:745)[:1.7.0_101] 

添加textline=true到网状URL请求是正确的,但只有服务器的第一线发送后Netty向Camel返回响应。服务器返回约100行文本,然后关闭连接。

我试过使用StringEncoder和StringDecoder,但这给了我'textline = true'的结果。

我的问题是:如何配置Netty将服务器的整个响应作为字符串返回。消息分隔符无关紧要,因为在服务器关闭连接之前,只会返回1条消息。

回答

0

由于您不能保证整个消息将在一次操作中被读取,您将需要实现自己的ByteToMessageDecoder,它了解使用的帧。