Java Nats Server

Nats Server for testing which contains the original Nats
Index
Variants
| Repositories | Framework | Maven Central | Contains |
|---|---|---|---|
| Java-Nats-Server | Plain Java | Original Nats Server | |
| Java-Nats-Server-JUnit | JUnit 5 | Java-Nats-Server | |
| Java-Nats-Server-Embedded | Spring Boot | Java-Nats-Server | |
| Java-Nats-Streaming-Server | Plain Java | Original Nats Streaming Server (deprecation-notice) | |
| Java-Nats-Streaming-Server-Embedded | Spring Boot | Java-Nats-Streaming-Server (deprecation-notice) |
Configuration properties
- Configs can be found
under NatsConfig
or NatsStreamingConfig
- The available config keys are based on the default
NATS_VERSIONversion of the current artefact
- The available config keys are based on the default
- The properties must start with the prefix
NATS_(e.gNATS_CLUSTER_ID) nats.propertiescan 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
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
Streaminge.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
- Supports spring config
- Configuration Properties are overwriting the spring properties
@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
- Supports spring config
- Configuration Properties are overwriting the spring properties
@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