io.nats.streaming.AckHandler Java Examples
The following examples show how to use
io.nats.streaming.AckHandler.
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"); } }