Search in sources :

Example 1 with Options

use of io.nats.client.Options 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)

Example 2 with Options

use of io.nats.client.Options in project moleculer-java by moleculer-java.

the class NatsTransporter method connect.

// --- CONNECT ---
@Override
public void connect() {
    try {
        // Create NATS client options
        Options.Builder builder = new Options.Builder();
        if (secure) {
            builder.secure();
        }
        if (username != null && password != null && !username.isEmpty() && !password.isEmpty()) {
            builder.userInfo(username.toCharArray(), password.toCharArray());
        }
        if (sslContext != null) {
            builder.sslContext(sslContext);
        }
        if (noRandomize) {
            builder.noRandomize();
        }
        builder.maxPingsOut(maxPingsOut);
        builder.pingInterval(Duration.ofMillis(pingInterval));
        builder.connectionTimeout(Duration.ofMillis(connectionTimeout));
        if (verbose) {
            builder.verbose();
        }
        builder.bufferSize(bufferSize);
        builder.authHandler(authHandler);
        if (noEcho) {
            builder.noEcho();
        }
        if (opentls) {
            builder.opentls();
        }
        if (pedantic) {
            builder.pedantic();
        }
        if (advancedStats) {
            builder.turnOnAdvancedStats();
        }
        if (utf8Support) {
            builder.supportUTF8Subjects();
        }
        if (oldRequestStyle) {
            builder.oldRequestStyle();
        }
        builder.connectionListener(this);
        builder.noReconnect();
        // Set server URLs
        for (String url : urls) {
            if (url.indexOf(':') == -1) {
                url = url + ":4222";
            }
            if (url.indexOf("://") == -1) {
                url = "nats://" + url;
            }
            builder.server(url);
        }
        // Connect to NATS server
        disconnect();
        started.set(true);
        Options options = builder.build();
        client = Nats.connect(options);
        dispatcher = client.createDispatcher(this);
        logger.info("NATS pub-sub connection estabilished.");
        connected();
    } catch (Exception cause) {
        String msg = cause.getMessage();
        if (msg == null || msg.isEmpty()) {
            msg = "Unable to connect to NATS server!";
        } else if (!msg.endsWith("!") && !msg.endsWith(".")) {
            msg += "!";
        }
        logger.warn(msg, cause);
    }
}
Also used : Options(io.nats.client.Options)

Aggregations

Options (io.nats.client.Options)2 Connection (io.nats.client.Connection)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