首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >服务器模式下的Apache Camel netty4生产者

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

Stack Overflow用户
提问于 2018-05-31 19:23:17
回答 1查看 726关注 0票数 2

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

代码语言:javascript
复制
<from>
    <...>
</from>
<to>
    <endpoint:uriRouteEndpoint uri="netty4:tcp://127.0.0.1:12345"/>
</to>

当有消息要发送时,这里的netty端点充当TCP客户端,懒惰地启动到指定套接字的连接。

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

EN

回答 1

Stack Overflow用户

发布于 2018-06-01 03:46:29

当然,使用Content Enricher EIP绝对可以做到这一点。您可以使用pollEnrich创建等待输入的池化消费者。

我已经为演示创建了单元测试。

代码语言:javascript
复制
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());
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50623165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档