View on GitHub

nats-server

Nats server for testing which contains the original Nats server

Java Nats Server

logo

Donate

Nats Server for testing which contains the original Nats

Index

Variants

Repositories Framework Maven Central Contains
Java-Nats-Server Plain Java "MVN Central" "Latest Change" Original Nats Server
Java-Nats-Server-JUnit JUnit 5 "MVN Central" "Latest Change" Java-Nats-Server
Java-Nats-Server-Embedded Spring Boot "MVN Central" "Latest Change" Java-Nats-Server
Java-Nats-Streaming-Server Plain Java "MVN Central" "Latest Change" Original Nats Streaming Server (deprecation-notice)
Java-Nats-Streaming-Server-Embedded Spring Boot "MVN Central" "Latest Change" Java-Nats-Streaming-Server (deprecation-notice)

Configuration properties

Configuration priority

1) Custom Arguments 2) Java DSL config 3) Property File (default nats.properties) 4) Environment Variables (*1) 5) Default Config

Common methods

Getter

Name Description
url nats server URL from bind to host address
pid process id (-1 == not started)
port port (-1 == not started && random port)
debug true if “DV”, “DVV” or “DEBUG” is set
version version of nats server
config Get config map
binary Path to binary file
pidFile Path to PID file
getValue Get resolved config for a key
jetstream true if Jetstream is enabled
configFile custom nats config file
downloadUrl Download URL
configPropertyFile custom property config file

Others

Name Description
start() Starts the nats server
close() Stops the nats server

Java Nats Example

Example auto closable

public class MyNatsTest {

    public static void main(final String[] args) {
        try (final var nats = new Nats()) {
            //DO SOMETHING...
        }
    }
}

Example with diverse configs

public class MyNatsTest {

    public static void main(final String[] args) {
        final Nats nats = natsBuilder()
                .port(-1)
                .debug(false)
                .jetStream(true)
                .autostart(true)
                .timeoutMs(10000)
                .version("3.0.0")
                .version(NatsVersion.V3_0_0)
                .configFile("/etc/usr/nats/nats.cfg")
                .configPropertyFile("/etc/usr/nats/nats.properties")
                .customArgs("--optionalArg1=123", "--optionalArg2=456")
                .logger(Logger.getLogger("MyCustomLogger"))
                .logLevel(System.Logger.Level.DEBUG)
                .config(USER, "my_optional_user")
                .config(PASS, "my_optional_password")
                .config(
                        NATS_BINARY_PATH.toString(), "optional/ready/to/use/nats/file",
                        NATS_DOWNLOAD_URL.toString(), "optional/nats/download/url",
                        NATS_CONFIG_FILE.toString(), "optional/config/file",
                        NATS_ARGS.toString(), "--optionalArg1=123 && --optionalArg2=456",
                        NATS_SYSTEM.toString(), "optional_download_suffix"
                ).nats();
        nats.close();
    }
}

Java Nats JUnit Example

"MVN Central" "Latest Change"


@JUnitNatsServer(
        port = 4680,
        keepAlive = true,
        timeoutMs = 10000,
        version = "3.0.0",
        configFile = "my.properties",
        downloadUrl = "https://example.com",
        binaryFile = "/tmp/natsserver",
        config = {"ADDR", "localhost"}
)
class NatsServerFirstTest {

    final NatsServer natsServer = getNatsServer();

    @Test
    void natsServerShouldStart() {
        assertThat(natsServer, is(notNullValue()));
    }
}

Java Nats Spring Example

"MVN Central" "Latest Change"


@SpringBootTest
@RunWith(SpringRunner.class)
@EnableNatsServer(port = 4222, config = {"user", "admin", "pass", "admin"})
public class SomeTest {
    //[...]
}
nats:
  server:
    hb_fail_count: 3
nats.server.hb_fail_count=3

Java Nats Streaming Spring Example

"MVN Central" "Latest Change"

Deprecation Notice


@SpringBootTest
@RunWith(SpringRunner.class)
@EnableNatsStreamingServer(port = 4222, config = {"user", "admin", "pass", "admin"})
public class SomeTest {
    //[...]
}
nats:
  streaming:
    server:
      hb_fail_count: 3
nats.streaming.server.hb_fail_count=3