Search in sources :

Example 1 with NatsConsumer

use of org.apache.camel.component.nats.NatsConsumer in project wildfly-camel by wildfly-extras.

the class NatsIntegrationTest method testNatsRoutes.

@Test
public void testNatsRoutes() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    try {
        camelctx.addRoutes(new RouteBuilder() {

            @Override
            public void configure() throws Exception {
                from("nats:test").id("nats-route").to("mock:result");
            }
        });
        NatsComponent nats = camelctx.getComponent("nats", NatsComponent.class);
        nats.setServers(TestUtils.getDockerHost() + ":4222");
        MockEndpoint to = camelctx.getEndpoint("mock:result", MockEndpoint.class);
        to.expectedMessageCount(1);
        camelctx.start();
        // Make sure the consumer has subscribed to the topic before sending messages
        NatsConsumer consumer = (NatsConsumer) camelctx.getRoute("nats-route").getConsumer();
        int count = 0;
        while (!consumer.isActive()) {
            Thread.sleep(500);
            count += 1;
            if (count > 10) {
                throw new IllegalStateException("Gave up waiting for nats consumer to subscribe to topic");
            }
        }
        Options options = new Options.Builder().server("nats://" + TestUtils.getDockerHost() + ":4222").build();
        Connection connection = Nats.connect(options);
        final byte[] payload = "test-message".getBytes(StandardCharsets.UTF_8);
        connection.publish("test", payload);
        to.assertIsSatisfied(5000);
        Exchange exchange = to.getExchanges().get(0);
        Assert.assertNotNull(exchange);
        Message message = exchange.getMessage();
        String body = message.getBody(String.class);
        Assert.assertEquals("test-message", body);
    } finally {
        camelctx.close();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) Options(io.nats.client.Options) RouteBuilder(org.apache.camel.builder.RouteBuilder) Message(org.apache.camel.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) RouteBuilder(org.apache.camel.builder.RouteBuilder) Connection(io.nats.client.Connection) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) NatsComponent(org.apache.camel.component.nats.NatsComponent) Exchange(org.apache.camel.Exchange) NatsConsumer(org.apache.camel.component.nats.NatsConsumer) Test(org.junit.Test)

Aggregations

Connection (io.nats.client.Connection)1 Options (io.nats.client.Options)1 CamelContext (org.apache.camel.CamelContext)1 Exchange (org.apache.camel.Exchange)1 Message (org.apache.camel.Message)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 NatsComponent (org.apache.camel.component.nats.NatsComponent)1 NatsConsumer (org.apache.camel.component.nats.NatsConsumer)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 Test (org.junit.Test)1