Java Netty4-TCP服务器-使用Camel进行基本测试
我是一名初学者,正在学习Camel,并尝试使用ApacheBlueprint在Camel上运行netty4。 我正在使用netty创建TCP服务器:Java Netty4-TCP服务器-使用Camel进行基本测试,java,maven,tcp,apache-camel,netty,Java,Maven,Tcp,Apache Camel,Netty,我是一名初学者,正在学习Camel,并尝试使用ApacheBlueprint在Camel上运行netty4。 我正在使用netty创建TCP服务器: <camelContext xmlns="http://camel.apache.org/schema/blueprint"> <route id="timerToLog"> <from uri="netty4:tcp://localhost:5150"/> <log m
<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>
我使用Hercules连接到tcp服务器,并使用Hercules发送数据(甚至尝试了windows telnet),一旦发送“Hello”ascii文本,连接就会关闭,出现以下错误:
[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) ...
我只是通过套接字发送了一个“Hello”,但它仍然显示超出了帧长度!
我知道我遗漏了一些非常基本的东西。请接受我的帮助:)根据文档,若url的textline参数为空,则安装并使用默认编码器/解码器。这相当于使用默认行为textline=false
在您的示例中,netty4组件有以下内容:
<from uri="netty4:tcp://localhost:5150"/>
因此,您使用的默认编码器/解码器可能不是基于文本行的编码器/解码器。您可能会在文档中找到默认的编码器,但我认为通过查找这些信息,您可能会学到比我更多的东西
请记住,Netty使用编码器和解码器的概念向客户机和服务器发送/接收数据。这些解码器定义Netty从套接字读取原始数据的方式。如果您不熟悉这一点,Netty上有大量可用的文档
让我们看看如何实现一个简单的基于文本行的协议编码器/解码器,就像您想要实现的那样
<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>
您所缺少的只是编码器位。根据文档,若url的textline参数为空,则它将安装并使用默认编码器/解码器。这相当于使用默认行为textline=false
在您的示例中,netty4组件有以下内容:
<from uri="netty4:tcp://localhost:5150"/>
因此,您使用的默认编码器/解码器可能不是基于文本行的编码器/解码器。您可能会在文档中找到默认的编码器,但我认为通过查找这些信息,您可能会学到比我更多的东西
请记住,Netty使用编码器和解码器的概念向客户机和服务器发送/接收数据。这些解码器定义Netty从套接字读取原始数据的方式。如果您不熟悉这一点,Netty上有大量可用的文档
让我们看看如何实现一个简单的基于文本行的协议编码器/解码器,就像您想要实现的那样
<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>
你所缺少的只是编码器位