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