io.nats.streaming.StreamingConnection Java Examples

The following examples show how to use io.nats.streaming.StreamingConnection. 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: NatsStreamingBenchmarkDriver.java    From openmessaging-benchmark with Apache License 2.0 6 votes vote down vote up
@Override public CompletableFuture<BenchmarkConsumer> createConsumer(String topic, String subscriptionName,
    ConsumerCallback consumerCallback) {
    Subscription sub;
    StreamingConnection streamingConnection;
    String clientId = "ConsumerInstance" + getRandomString();
    try {
        streamingConnection = NatsStreaming.connect(clusterId, clientId, optsBuilder.build());
        sub = streamingConnection.subscribe(topic, subscriptionName, new MessageHandler() {
            @Override public void onMessage(Message message) {
                consumerCallback.messageReceived(message.getData(), message.getTimestamp());
            }
        }, subBuilder.build());
    } catch (Exception e) {
        log.warn("nats streaming create consumer exception", e);
        return null;
    }
    return CompletableFuture.completedFuture(new NatsStreamingBenchmarkConsumer(streamingConnection, sub, topic));
}
 
Example #2
Source File: NATSStreamObservableQueue.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public void connect() {
    try {
        StreamingConnection temp = fact.createConnection();
        logger.info("Successfully connected for " + queueURI);
        
        temp.getNatsConnection().setReconnectedCallback((event) ->
                logger.warn("onReconnect. Reconnected back for " + queueURI));
        temp.getNatsConnection().setDisconnectedCallback((event ->
                logger.warn("onDisconnect. Disconnected for " + queueURI)));
        
        conn = temp;
    } catch (Exception e) {
        logger.error("Unable to establish nats streaming connection for " + queueURI, e);
        throw new RuntimeException(e);
    }
}
 
Example #3
Source File: NatsStreamingBenchmarkConsumer.java    From openmessaging-benchmark with Apache License 2.0 4 votes vote down vote up
public NatsStreamingBenchmarkConsumer(StreamingConnection streamingConnection, Subscription sub, String topic) {
    this.sub = sub;
    this.topic = topic;
    this.unsubscribe = false;
    this.streamingConnection = streamingConnection;
}
 
Example #4
Source File: NatsStreamingBenchmarkProducer.java    From openmessaging-benchmark with Apache License 2.0 4 votes vote down vote up
public NatsStreamingBenchmarkProducer (StreamingConnection natsStreamingPublisher, String topic) {
    this.natsStreamingPublisher = natsStreamingPublisher;
    this.topic = topic;
}
 
Example #5
Source File: NatsStreamingTransporter.java    From moleculer-java with MIT License 4 votes vote down vote up
@Override
public void connectionLost(StreamingConnection conn, Exception ex) {
	if (debug) {
		logger.warn("NATS connection lost!", ex);
	}
}
 
Example #6
Source File: Subscriber.java    From stan.java with Apache License 2.0 4 votes vote down vote up
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);
        }
    }
}