io.nats.streaming.Options Java Examples
The following examples show how to use
io.nats.streaming.Options.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FHIRNotificationNATSPublisher.java From FHIR with Apache License 2.0 | 4 votes |
/** * Performs any required initialization to allow us to publish events to the channel. */ private void init(String clusterId, String channelName, String clientId, String servers, Properties tlsProps) { log.entering(this.getClass().getName(), "init"); SSLContext ctx = null; try { this.channelName = channelName; if (log.isLoggable(Level.FINER)) { log.finer("ClusterId: " + clusterId); log.finer("Channel name: " + channelName); log.finer("ClientId: " + clientId); log.finer("Servers: " + servers); } // Make sure that the properties file contains the expected properties. if (clusterId == null || channelName == null || clientId == null || servers == null || servers.length() == 0) { throw new IllegalStateException("Config property missing from the NATS connection properties."); } if (Boolean.parseBoolean(tlsProps.getProperty("useTLS"))) { // Make sure that the tls properties are set. if (tlsProps.getProperty("truststore") == null || tlsProps.getProperty("truststorePass") == null || tlsProps.getProperty("keystore") == null || tlsProps.getProperty("keystorePass") == null) { throw new IllegalStateException("TLS config property missing from the NATS connection properties."); } ctx = createSSLContext(tlsProps); } // Create the NATS client connection options io.nats.client.Options.Builder builder = new io.nats.client.Options.Builder(); builder.maxReconnects(-1); builder.connectionName(channelName); builder.servers(servers.split(",")); if (ctx != null) { builder.sslContext(ctx); } io.nats.client.Options natsOptions = builder.build(); // Create the NATS connection and the streaming connection Connection nc = Nats.connect(natsOptions); Options streamingOptions = new Options.Builder().natsConn(nc).build(); sc = NatsStreaming.connect(clusterId, clientId, streamingOptions); // Create the publish callback acb = new AckHandler() { @Override public void onAck(String nuid, Exception ex) { log.finer("Received ACK for guid: " + nuid); if (ex != null && log.isLoggable(Level.SEVERE)) { log.log(Level.SEVERE, "Error in server ack for guid " + nuid + ": " + ex.getMessage(), ex); } } }; // Register this NATS implementation as a "subscriber" with our Notification Service. // This means that our "notify" method will be called when the server publishes an event. service.subscribe(this); log.info("Initialized NATS publisher for channel '" + channelName + "' using servers: '" + servers + "'."); } catch (Throwable t) { String msg = "Caught exception while initializing NATS publisher."; log.log(Level.SEVERE, msg, t); throw new IllegalStateException(msg, t); } finally { log.exiting(this.getClass().getName(), "init"); } }
Example #2
Source File: NatsStreamingTransporter.java From moleculer-java with MIT License | 4 votes |
@Override public void connect() { try { // Create NATS client options Options.Builder builder = new Options.Builder(); if (clientId != null && !clientId.isEmpty()) { builder.clientId(clientId); } else { builder.clientId(nodeID); } if (clusterId != null && !clusterId.isEmpty()) { builder.clusterId(clusterId); } else { builder.clusterId("test-cluster"); } if (connectTimeout != null) { builder.connectWait(connectTimeout); } if (ackTimeout != null) { builder.pubAckWait(ackTimeout); } if (discoverPrefix != null) { builder.discoverPrefix(discoverPrefix); } if (maxPubAcksInFlight != null) { builder.maxPubAcksInFlight(maxPubAcksInFlight); } if (pingInterval != null) { builder.pingInterval(pingInterval); } if (pingsMaxOut != null) { builder.maxPingsOut(pingsMaxOut); } if (traceConnection != null) { builder.traceConnection(); } // Set server URLs String natsUrl = url; if (natsUrl.indexOf(':') == -1) { natsUrl = natsUrl + ":4222"; } if (natsUrl.indexOf("://") == -1) { natsUrl = "nats://" + natsUrl; } builder.natsUrl(natsUrl); // Set event listeners builder.connectionListener(this); builder.errorListener(this); builder.connectionLostHandler(this); // Connect to NATS server disconnect(); started.set(true); StreamingConnectionFactory factory = new StreamingConnectionFactory(builder.build()); client = factory.createConnection(); 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); } }
Example #3
Source File: Subscriber.java From stan.java with Apache License 2.0 | 4 votes |
private void run() throws Exception { Options opts = null; if (url != null) { opts = new Options.Builder().natsUrl(url).build(); } final CountDownLatch done = new CountDownLatch(1); final CountDownLatch start = new CountDownLatch(1); final AtomicInteger delivered = new AtomicInteger(0); Thread hook = null; try (final StreamingConnection sc = NatsStreaming.connect(clusterId, clientId, opts)) { try { final Subscription sub = sc.subscribe(subject, qgroup, new MessageHandler() { public void onMessage(Message msg) { try { start.await(); } catch (InterruptedException e) { /* NOOP */ } System.out.printf("[#%d] Received on [%s]: '%s'\n", delivered.incrementAndGet(), msg.getSubject(), msg); if (delivered.get() == count) { done.countDown(); } } }, builder.build()); hook = new Thread() { public void run() { System.err.println("\nCaught CTRL-C, shutting down gracefully...\n"); try { if (durable == null || durable.isEmpty() || unsubscribe) { sub.unsubscribe(); } sc.close(); } catch (Exception e) { e.printStackTrace(); } done.countDown(); } }; Runtime.getRuntime().addShutdownHook(hook); System.out.printf("Listening on [%s], clientID=[%s], qgroup=[%s] durable=[%s]\n", sub.getSubject(), clientId, sub.getQueue(), sub.getOptions().getDurableName()); start.countDown(); done.await(); if (durable == null || durable.isEmpty() || unsubscribe) { sub.unsubscribe(); } sc.close(); } finally { Runtime.getRuntime().removeShutdownHook(hook); } } }