io.nats.streaming.Message Java Examples

The following examples show how to use io.nats.streaming.Message. 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: NatsStreamingTransporter.java    From moleculer-java with MIT License 4 votes vote down vote up
@Override
public void onMessage(Message msg) {

	// Send messages into the shared executor
	received(msg.getSubject(), msg.getData());
}
 
Example #3
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);
        }
    }
}