Java Nats Server
Nats Server for testing which contains the original Nats
Index
Variants
Configuration properties
- Configs can be found
under NatsConfig
or NatsStreamingConfig
- The available config keys are based on the default
NATS_VERSION
version of the current artefact
- The properties must start with the prefix
NATS_
(e.g NATS_CLUSTER_ID
)
nats.properties
can be created optionally to configure the Nats Server
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
- Java
Nats:
- Java Streaming
Nats:
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
- Nats has more than 50 configs are available
- NatsStreaming has more than 90 configs are available
- Nats Steaming: classes and methods uses suffix
Streaming
e.g. NatsStreaming
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
- Port -1 = random port
- keepAlive = nats starts only one time in the whole test context
@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
@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
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