当有消息要发送时,这里的netty端点充当TCP客户端,懒惰地启动到指定套接字的连接。有没有一种简单的解决方案可以让它充当TC">
我在生产者模式下使用Apache Camel netty4组件,端点配置如下:
<from>
<...>
</from>
<to>
<endpoint:uriRouteEndpoint uri="netty4:tcp://127.0.0.1:12345"/>
</to>
当有消息要发送时,这里的netty端点充当TCP客户端,懒惰地启动到指定套接字的连接。
有没有一种简单的解决方案可以让它充当TCP服务器,即等待客户端软件发起并建立TCP连接后再发送消息?
发布于 2018-05-31 19:46:29
当然,使用Content Enricher 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());
}
}
https://stackoverflow.com/questions/50623165
复制