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