简体   繁体   中英

Netty4 - TCP Server - basic testing using Camel

I am a beginner learning Camel and trying to run netty4 on camel using apache blueprint. I am creating a TCP server using netty :

  <camelContext xmlns="http://camel.apache.org/schema/blueprint">
    <route id="timerToLog">
      <from uri="netty4:tcp://localhost:5150"/>
      <log message="The message contains ${body}"/>
      <to uri="mock:result"/>
    </route>
  </camelContext>

Everything works fine when I run camel using mvn:

[         Blueprint Extender: 1] TCPNettyServerBootstrapFactory INFO  ServerBootstrap binding to localhost:5150
[         Blueprint Extender: 1] NettyConsumer                  INFO  Netty consumer bound to: localhost:5150
[         Blueprint Extender: 1] BlueprintCamelContext          INFO  Route: timerToLog started and consuming from: Endpoint[tcp://localhost:5150]

I connect to the tcp server using and send data using Hercules ( even tried windows telnet ) and as soon as I send a "Hello" ascii text, the connection gets closed with the following error:

[d #0 - NettyEventExecutorGroup] NettyConsumer                  WARN  Closing channel as an exception was thrown from Netty. Caused by: [io.netty.hand
ler.codec.TooLongFrameException - Adjusted frame length exceeds 1048576: 1212501072 - discarded]
io.netty.handler.codec.TooLongFrameException: Adjusted frame length exceeds 1048576: 1212501072 - discarded
        at io.netty.handler.codec.LengthFieldBasedFrameDecoder.fail(LengthFieldBasedFrameDecoder.java:499)
        at io.netty.handler.codec.LengthFieldBasedFrameDecoder.failIfNecessary(LengthFieldBasedFrameDecoder.java:477)
        at io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:403) ... 

I am only sending a "Hello" over the socket and still it says frame length exceeded.. ! I know am missing something very basic. Please bear with me and help :)

According to the Camel Netty4 Component documentation if the textline parameter of the the url is NULL then it installs and uses a default encoder/decoder. This is the equivalent of using textline=false which is the default behavior.

In your example you have this for the netty4 component:

 <from uri="netty4:tcp://localhost:5150"/>

Thus you are using the default encoder/decoders which probably is not a text line based encoder/decoder. YOu can probably find which is the default encoder in the documentation but I think you probably can learn more than me by finding that information.

Remember that Netty uses the concept of encoders and decoders to send/receive data to and from clients and servers. These decoders define how Netty should read the raw data from the socket. If you are unfamiliar with this there is tons of documentation on Netty available.

Lets look at how to implement a simple text line based protocol encoder/decoder like you want to achieve.

<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route id="timerToLog">
  <from uri="netty4:tcp://localhost:5150?textline=true"/>
  <log message="The message contains ${body}"/>
  <to uri="mock:result"/>
</route>

All you were missing was the encoder bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM