服务器模式下的 Apache Camel netty4 生产者



我在生产者模式下使用Apache Camel netty4组件,端点配置如下:

<from>
<...>
</from>
<to>
<endpoint:uriRouteEndpoint uri="netty4:tcp://127.0.0.1:12345"/>
</to>

当有消息要发送时,此处的netty端点充当TCP客户端,延迟启动与指定套接字的连接。

有没有一个简单的解决方案可以让它充当TCP服务器,即等到客户端软件启动并建立TCP连接后再发送消息?

当然,这绝对是内容丰富器EIP可以实现的。您可以使用pollEnrich创建等待输入的池化使用者。


我已经创建了用于演示的单元测试。

public class NettyEnrich extends CamelTestSupport {
@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:in")
.pollEnrich("netty4:tcp://127.0.0.1:12345")
.to("mock:out");
}
};
}
@Test
public void test() throws Exception{
MockEndpoint mockEndpoint = getMockEndpoint("mock:out");
mockEndpoint.setExpectedCount(1);
template.asyncSendBody("direct:in",""); //direct:in is now waiting for connection from netty client
template.sendBody("netty4:tcp://127.0.0.1:12345", "Hello from TCP"); //Initialize connection to resume direct:in
mockEndpoint.assertIsSatisfied();
Assert.assertEquals("Hello from TCP", mockEndpoint.getExchanges().get(0).getIn().getBody());
}
}

最新更新