2
votes

I am using Apache Camel netty4 component in a producer mode with endpoint configured like this:

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

When there is a message to send the netty endpoint here acts as a TCP client lazily initiating connection to the socket specified.

Is there a simple solution to make it act as a TCP server instead, i.e. to wait untill the TCP connection is initiated and established by a client software before sending messages?

1

1 Answers

0
votes

Sure, this is definitely possible with Content Enricher EIP. You can use pollEnrich to create pooling consumer waiting for inputs.


I have created unit test for demonstration.

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());
    }
}