Useful for running unit or integration tests on the localhost.
JDK Version
This project uses Java 8 Language Level api, but builds jars compiled with and targeted for Java 8, 17, 21 and 25.
It creates different artifacts for each. All have the same group id io.nats and the same version but have different artifact names.
Java Target Level
Artifact Id
Maven Central
1.8
jnats-server-runner
17
jnats-server-runner-jdk17
21
jnats-server-runner-jdk21
25
jnats-server-runner-jdk25
Executable
By default, the server is found in your path in this order:
the executablePath set in the builder
the path found in the nats_server_path environment variable
nats-server somewhere in the machine’s path.
For simple setup, constructors work well
try (NatsServerRunner server = new NatsServerRunner()) {
System.out.println("Server running on port: " + server.getPort())
Connection c = Nats.connect(server.getURI());
...
}
Builder
For more complicated setup, use the NatsServerRunnerBuilder
If you want to run multiple instances of the server, for instance, in unit tests, you can do
some setup statically to reduce the code in the builders. A simple way to do this would be
to have a static initializer in a base test class or something that is called before all uses.
There are several methods available in NatsRunnerUtils and can be overridden from the builder,
except setting the server path.
static {
NatsRunnerUtils.setDefaultOutputSupplier(ConsoleOutput::new);
NatsRunnerUtils.setDefaultOutputLevel(Level.SEVERE); // will reduce the output, nice for tests
NatsRunnerUtils.setDefaultProcessAliveCheckTries(10);
NatsRunnerUtils.setDefaultProcessAliveCheckWait(100);
NatsRunnerUtils.setDefaultConnectValidateTries(3);
NatsRunnerUtils.setDefaultConnectValidateTimeout(100); // milliseconds
NatsRunnerUtils.setDefaultOutputThreadProvider(myOutputThreadProvider);
NatsRunnerUtils.setManualStartPort(1234); // the port used to initialize the port number for auto generated port numbers
NatsRunnerUtils.setDefaultLocalhostHost(LocalHost.name);
NatsRunnerUtils.setPreferredServerPath("/path/to/nats-server");
}
nats-server path
To start the NATS server, the program must know the way to run the nats-server executable.
By default, it assumes it is in the path and just tries nats-server. You can tell the program
where to find the server in two ways.
The program uses the set path first if it was set.
If it was not set, it tries the environment path if it was set.
Last, if neither was set it uses the default.
Dependency Management
The JNATS Server Runner is available in the Maven central repository,
and can be imported as a standard dependency in your build.gradle or pom.xml file,
The examples shown use the Jdk 8 version. To use other versions, change the artifact id.
Java Nats Server Runner
Run the NATS messaging system Server from your Java code.
Useful for running unit or integration tests on the localhost.
JDK Version
This project uses Java 8 Language Level api, but builds jars compiled with and targeted for Java 8, 17, 21 and 25. It creates different artifacts for each. All have the same group id
io.natsand the same version but have different artifact names.jnats-server-runnerjnats-server-runner-jdk17jnats-server-runner-jdk21jnats-server-runner-jdk25Executable
By default, the server is found in your path in this order:
executablePathset in the buildernats_server_pathenvironment variablenats-serversomewhere in the machine’s path.For simple setup, constructors work well
Builder
For more complicated setup, use the
NatsServerRunnerBuilderStatic Settings
If you want to run multiple instances of the server, for instance, in unit tests, you can do some setup statically to reduce the code in the builders. A simple way to do this would be to have a static initializer in a base test class or something that is called before all uses. There are several methods available in
NatsRunnerUtilsand can be overridden from the builder, except setting the server path.nats-server path
To start the NATS server, the program must know the way to run the
nats-serverexecutable. By default, it assumes it is in the path and just triesnats-server. You can tell the program where to find the server in two ways.NatsRunnerUtils.setPreferredServerPathstaticallynats_server_pathenvironment variable.The program uses the set path first if it was set. If it was not set, it tries the environment path if it was set. Last, if neither was set it uses the default.
Dependency Management
The JNATS Server Runner is available in the Maven central repository, and can be imported as a standard dependency in your
build.gradleorpom.xmlfile, The examples shown use the Jdk 8 version. To use other versions, change the artifact id.Gradle
If you need the latest and greatest before Maven central updates, you can use:
If you need a snapshot version, you must add the url for the snapshots and change your dependency.
Maven
If you need the absolute latest, before it propagates to maven central, you can use the repository:
If you need a snapshot version, you must enable snapshots and change your dependency.
License
Unless otherwise noted, the NATS source files are distributed under the Apache Version 2.0 license found in the LICENSE file.