This folder contains a number of examples:
ConnectTime.java
- how to build a listener that will track connect time.NatsPub.java
- publishes messagesNatsPubMany.java
- publishes many messagesNatsReply.java
- sends a reply when a request is receivedNatsReq.java
- sends a request and prints the replyNatsReqFuture.java
- sends a request async using a futureNatsSub.java
- reads messages synchronouslyNatsSubDispatch.java
- uses asynchronous dispatching for incoming messagesNatsSubQueue.java
- subscribes on a queue for message load balancingNatsSubQueueFull.java
- fuller publish and queue subscribe example
In the io.nats.examples.jetstream
package...
NatsJsPub.java
- publish JetStream messagesNatsJsPubAsync.java
- publish JetStream messages asynchronouslyNatsJsPubVersusCorePub.java
- publish JetStream messages versus core publish to the same stream.NatsJsPubWithOptionsUseCases
- publish JetStream with examples on using publish optionsNatsJsPullSubBatchSize.java
- pull subscription example specifying batch size and manual handlingNatsJsPullSubBatchSizeUseCases.java
- pull subscription example specifying batch size with examples of manual handling various cases of available messagesNatsJsPullSubExpiresIn.java
- pull subscription example specifying expiration and manual handlingNatsJsPullSubExpiresInUseCases.java
- pull subscription example specifying expiration with examples of manual handling various cases of available messagesNatsJsPullSubFetch.java
- pull subscription example using fetch list macro functionNatsJsPullSubFetchUseCases.java
- pull subscription example using fetch list macro function with examples of various cases of available messagesNatsJsPullSubIterate.java
- pull subscription example using iterate macro functionNatsJsPullSubIterateUseCases.java
- pull subscription example using iterate macro function with examples of various cases of available messagesNatsJsPullSubNoWaitUseCases.java
- pull subscription example specifying no wait with examples of manual handling various cases of available messagesNatsJsPushSubBasicAsync.java
- push subscribing to read messages asynchronously and manually acknowledge messages.NatsJsPushSubBasicSync.java
- push subscribing to read messages synchronously and manually acknowledges messages.NatsJsPushSubBindDurable.java
- push subscribing with the bind optionsNatsJsPushSubDeliverSubject.java
- push subscribing with a deliver subject and how the subject can be read as a regular Nats MessageNatsJsPushSubFilterSubject.java
- push subscribing with a filter on the subjects.NatsJsPushSubFlowControl.java
-NatsJsPushSubHeartbeat.java
-NatsJsPushSubQueueDurable.java
- push subscribing to read messages in a load balance queue using a durable consumer.
NatsJsManageConsumers.java
- demonstrate the management of consumersNatsJsManageStreams.java
- demonstrate the management of streamsNatsJsMirrorSubUseCases.java
-NatsJsPrefix.java
- demonstrate connecting on an account that uses a custom prefix
NatsKeyValueFull.java
-
autobench
- 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.jsmulti
- a multi-faceted tool that allows you to do a variety of publishing and subscribing to both core NATS and JetStream subjects. Please see the JsMultiTool README
ExampleArgs.java
- Helper to manage command line arguments.ExampleAuthHandler.java
- Example of an auth handler.ExampleUtils.java
- Miscellaneous utils used to start or in running examples.NatsJsUtils.java
- Miscellaneous utils specific to JetStream examples.
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 both the client library and the examples to be compiled and then the jars be used in the classpath. When you build locally,
-SNAPSHOT
is appended to the version. See the project README for specifics on building these. -
When you run, if you supply an unknown parameter or miss a required parameter, the usage string will be shown and the program will exit.
-
If you purposefully want to see the usage string run the program with
-h
,-help
,-u
,-usage
. -
All examples require a server url, but it's always optional to provide one as part of the command. If not supplied, the program will use
nats://localhost:4222
-
If you want to run the program from an ide, you can take advantage of the ide runner to provide arguments. You can also just insert some code before the arguments are processed to set the arguments directly. For example;
args = "-arg1 myArg1 -arg2 myArg2".split(" "); args = new String[] {"-arg1", "myArg1", "-arg2", "myArg2"};
In the examples, the usage will show java <program> -cp <classpath> ...
Make sure you add both the client library and examples into the classpath. For example:
java -cp build/libs/jnats-2.14.1-SNAPSHOT.jar:build/libs/jnats-2.14.1-SNAPSHOT-examples.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/truststore.jks -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.14.1-SNAPSHOT.jar:build/libs/jnats-2.14.1-SNAPSHOT-examples.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 the NATS server.
nats-server --conf src/test/resources/tls.conf
As well as one with the verify flag set.
nats-server --conf src/test/resources/tlsverify.conf
which will require client certificates.