Run the NATS messaging system Server from your Java code.
Current Release: 2.0.1 Current Snapshot: 2.0.2-SNAPSHOT
Useful for running unit or integration tests on the localhost.
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 - the path set via
NatsRunnerUtils.setServerPath
(deprecated, prefer setting in builder) 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());
...
}
For more complicated setup, use the NatsServerRunnerBuilder
String[] customInserts = new String[] {
"server_name=srv1",
"cluster {",
" name: testcluster",
" listen: 127.0.0.1:4222",
" routes: [",
" nats-route://127.0.0.2:4222",
" nats-route://127.0.0.3:4222",
" ]",
"}",
""
};
NatsServerRunner.Builder builder = NatsServerRunner.builder()
.port(4567)
.debugLevel(DebugLevel.DEBUG_TRACE)
.jetstream(true)
.executablePath("/this/run/only/nats-server")
.configFilePath("/mypath/custom.conf")
.configInserts(customInserts);
try (NatsServerRunner server = builder.build()) {
}
catch (Exception e) {
throw new RuntimeException(e);
}
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 3 methods available:
static {
NatsServerRunner.setDefaultOutputSupplier(ConsoleOutput::new);
NatsServerRunner.setDefaultOutputLevel(Level.WARNING);
NatsServerRunner.setPreferredServerPath("/path/to/nats-server")
}
Unless otherwise noted, the NATS source files are distributed under the Apache Version 2.0 license found in the LICENSE file.