Java API for the NATs messaging system.

nats logo

NATS Server is a simple, high performance open source messaging system for cloud native applications, IoT messaging, and microservices architectures. This Java API opens the server to Java applications. The API is performant, supporting millions of messages a second, and easy to use.

Publishing can be accomplished in just a few lines of code:

    Connection nc = Nats.connect("nats://localhost:4222");
    nc.publish(subject, message.getBytes(StandardCharsets.UTF_8));

As can subscribing:

    Connection nc = Nats.connect("nats://localhost:4222");
    Subscription sub = nc.subscribe(subject);

    Message msg = sub.nextMessage(Duration.ofSeconds(1));

More advanced applications can organize message handling behind threaded Dispatchers:

    Connection nc = Nats.connect("nats://localhost:4222");
    
    Dispatcher d = nc.createDispatcher((msg) -> {
        System.out.printf("Received message \"%s\" on subject \"%s\"\n", 
                                new String(msg.getData(), StandardCharsets.UTF_8), 
                                msg.getSubject());
    });
    d.subscribe(subject);

A powerful Options class is provided to configure advanced connections.

    Options o = new Options.Builder().server("nats://hostname:4222").secure().reconnectBufferSize(300).build();
    Connection nc = Nats.connect(o);

This package is implemented with Java 8 features. It is not designed for Android applications, in particular the networking code while striving for performance and effeciency is not designed for mobile scenarios.

Packages 
Package Description
io.nats.client
The Java NATS client is encapsulated into this single package io.nats.client.