This folder contains a number of examples:
NatsDispatch.java
- uses asynchronous dispatching for incoming messagesNatsPub.java
- publishes a messagesNatsQSub.java
- subscribes on a queue for message load balancingNatsReply.java
- sends a reply when a request is receivedNatsReq.java
- sends a request and prints the replyNatsSub.java
- reads messages synchronouslyautobench
- benchmarks the current system/setup in several scenariosbenchmark
- benchmark that supports multiple threadsstan
- A larger example that implements a server that can respond on multiple subjects, and several clients that send requests on those various subjects.stability
- a small producer and subscriber that run forever printing some status every so often. These are intended for long running tests without burning the CPU.
All of these examples take the server URL on the command line, which means that you can use the tls
and opentls
schemas to test over a secure connection.
The examples require the client library and the examples to be compiled. See the readme.md for specifics on building these.
java -cp build/libs/jnats-2.0.0.jar:build/libs/jnats-examples-2.0.0.jar io.nats.examples.NatsPub nats://localhost:4222 test "hello world"
To see how queues split messages, run the NatsQSub
in multiple windows and then run NatsPub
. Messages should be distributed between the clients. On the other hand, if you run NatsSub
in multiple shells and then run NatsPub
you will get the message at all subscribers.
A set of sample certificates are provided in the repo for testing. These use the highly secure password password
. To run with the full client and trust default keystore you can use command line arguments to set System properties.
java -Djavax.net.ssl.keyStore=src/test/resources/keystore.jks -Djavax.net.ssl.keyStorePassword=password -Djavax.net.ssl.trustStore=src/test/resources/cacerts -Djavax.net.ssl.trustStorePassword=password io.nats.examples.NatsPub tls://localhost:4443 test "hello world"
To run with the completely unverified client:
java -cp build/libs/jnats-2.0.0.jar:build/libs/jnats-examples-2.0.0.jar io.nats.examples.NatsSub opentls://localhost:4443 test 3
There are a set tls configuration for the server in the test files that can be used to run gnatsd.
gnatsd --conf src/test/resources/tls.conf
As well as one with the verify flag set.
gnatsd --conf src/test/resources/tlsverify.conf
which will require client certificates.