2016-07-06 137 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,它瞭解使用的幀。