To create a project with NATS support using the Micronaut CLI, supply the nats feature to the features flag.
$ mn create-app my-nats-app --features nats
This will create a project with the minimum necessary configuration for NATS.
Messaging Application
The Micronaut CLI can generate messaging applications. This will create a Micronaut app with NATS support, and without an HTTP server (although you can add one if you desire). The profile also provides a couple commands for generating NATS consumers and producers.
To create a NATS messaging application, use the the following command:
As you’d expect, you can start the application with ./gradlew run (for Gradle) or ./mvnw compile exec:exec (Maven). The application will (with the default config) attempt to connect to NATS at nats://localhost:4222, and will continue to run without starting up an HTTP server. All communication to/from the service will take place via NATS producers and/or consumers.
Within the new project, you can now run the NATS specific code generation commands:
$ mn create-nats-producer Message| Rendered template Producer.java to destination src/main/java/my/nats/app/MessageProducer.java$ mn create-nats-listener Message| Rendered template Listener.java to destination src/main/java/my/nats/app/MessageListener.java
4 NATS Quick Start
To add support for NATS.io to an existing project, you should first add the Micronaut NATS configuration to your build configuration. For example:
To create a NATS Producer that sends messages you can simply define an interface that is annotated with @NatsClient.
For example the following is a trivial @NatsClient interface:
At compile time Micronaut will produce an implementation of the above interface. You can retrieve an instance of ProductClient either by looking up the bean from the ApplicationContext or by injecting the bean with @Inject:
NATS also supports a fail over connection strategy where the first server that connects successfully will be used among a list of servers. To use this option in Micronaut, simply supply a list of host:port addresses.
When the configuration option nats.servers is used, no other options underneath nats are read; for example nats.username.
If you need to setup TLS, it can be configured this way:
6 NATS Producers
The example in the quick start presented a trivial definition of an interface that be implemented automatically for you using the @NatsClient annotation.
The implementation that powers @NatsClient (defined by the NatsIntroductionAdvice class) is, however, very flexible and offers a range of options for defining NATS clients.
6.1 Defining @NatsClient Methods
All methods that publish messages to NATS must meet the following conditions:
The method must reside in an interface annotated with @NatsClient.
The method or a method parameter must be annotated with @Subject.
The method must contain an argument representing the body of the message.
Important
If a body argument cannot be found, an exception will be thrown.
Note
In order for all of the functionality to work as designed in this guide your classes must be compiled with the parameters flag set to true. If your application was created with the Micronaut CLI, then that has already been configured for you.
Important
Unless a reactive type is returned from the publishing method, the action is blocking.
6.1.1 Publishing Parameters
All options are available to be set for publishing messages. The publish method is used by the NatsIntroductionAdvice to publish messages and all arguments can be set through annotations or method arguments.
6.1.1.1 Subject
If you need to specify the subject of the message, apply the @Subject annotation to the method or an argument of the method. Apply the annotation to the method itself if the value is static for every execution. Apply the annotation to an argument of the method if the value should be set per execution.
Producer Connection
If multiple Nats servers have been configured, the name of the server can be set in the @Subject annotation to designate which connection should be used to publish messages.
Note
The connection option is also available to be set on the @NatsClient annotation.
Queues
Tip
The NATS server will route the message to the queue and select a message receiver.
6.1.1.2 Headers
Headers can be set on the message with the @MessageHeader annotation applied to the method or an argument of the method. Apply the annotation to the method itself if the value is static for every execution. Apply the annotation to an argument of the method if the value should be set per execution.
6.1.1.3 Message Body
Most examples up to this point have been using a byte[] as the body type for simplicity. This library supports most standard Java types and JSON serialization (using Jackson) by default. The functionality is extensible and it is possible to add support for additional types and serialization strategies. See the section on Message Serialization/Deserialization for more information.
7 NATS Consumers
The quick start section presented a trivial example of what is possible with the @NatsListener annotation.
The implementation that powers @NatsListener (defined by the NatsConsumerAdvice class) is, however, very flexible and offers a range of options for consuming NATS message.
7.1 Defining @NatsListener Methods
All methods that consume messages from NATS must meet the following conditions:
The method must reside in a class annotated with @NatsListener.
In order for all of the functionality to work as designed in this guide your classes must be compiled with the parameters flag set to true.
If your application was created with the Micronaut CLI, then that has already been configured for you.
7.1.1 Consumer Parameters
The createDispatcher method is used by the NatsConsumerAdvice to consume messages. Some of the options can be directly configured through annotations.
Important
In order for the consumer method to be invoked, all arguments must be satisfied. To allow execution of the method with a null value, the argument must be declared as nullable. If the arguments cannot be satisfied, the message will be rejected.
7.1.1.1 Subject
A @Subject annotation is required for a method to be a consumer of messages from Nats. Simply apply the annotation to the method and supply the name of the subject you would like to listen to.
Queue Support
Subscribers may specify queue groups at subscription time. When a message is published to the group, NATS will deliver it to a one-and-only-one subscriber.
Important
Queue groups do not persist messages. If no listeners are available, the message is discarded.
Other Options
If multiple Nats servers have been configured, the name of the server can be set in the @Subject annotation to designate which connection should be used to listen for messages.
Note
The connection option is also available to be set on the @NatsListener annotation.
7.1.1.2 Headers
Headers can be retrieved with the @MessageHeader annotation applied to the arguments of the method.
7.1.1.3 Nats Types
Arguments can also be bound based on their type. Several types are supported by default and each type has a corresponding NatsTypeArgumentBinder. The argument binders are covered in detail in the section on Custom Parameter Binding.
There is only type that is supported for retrieving data about the Message.
7.1.1.4 Message Body
Most examples up to this point have been using a byte[] as the body type for simplicity. This library supports most standard Java types and JSON deserialization (using Jackson) by default. The functionality is extensible and it is possible to add support for additional types and deserialization strategies. See the section on Message Serialization/Deserialization for more information.
7.1.1.5 Custom Parameter Binding
Default Binding Functionality
Consumer argument binding is achieved through an ArgumentBinderRegistry that is specific for binding consumers from Nats messages. The class responsible for this is the NatsBinderRegistry.
The registry supports argument binders that are used based on an annotation applied to the argument or the argument type. All argument binders must implement either NatsAnnotatedArgumentBinder or NatsTypeArgumentBinder. The exception to that rule is the NatsDefaultBinder which is used when no other binders support a given argument.
When an argument needs bound, the Message is used as the source of all of the available data. The binder registry follows a small sequence of steps to attempt to find a binder that supports the argument.
Search the annotation based binders for one that matches any annotation on the argument that is annotated with @Bindable.
Search the type based binders for one that matches or is a subclass of the argument type.
Return the default binder.
The default binder binds the body of the message to the argument.
Custom Binding
To inject your own argument binding behavior, it is as simple as registering a bean. The existing binder registry will inject it and include it in the normal processing.
Annotation Binding
A custom annotation can be created to bind consumer arguments. A custom binder can then be created to use that annotation and the Message to supply a value for the argument. The value may in fact come from anywhere, however for the purposes of this documentation, the replyTo in the message is used.
The annotation can now be used on the argument in a consumer method.
A custom binder can be created to support any argument type. For example the following class could be created to bind values from the headers. This functionality could allow the work of retrieving and converting the headers to occur in a single place instead of multiple times in your code.
A type argument binder can then be created to create the ProductInfo instance to bind to your consumer method argument.
8 Request-Reply (RPC)
This library supports RPC through the usage of Request-Reply. Both blocking and non blocking variations are supported.
The following is an example direct reply to where the consumer is converting the body to upper case and replying with the converted string.
Client Side
The "client side" in this case starts by publishing a message. A consumer somewhere will then receive the message and reply with a new value.
Important
In order for the publisher to assume RPC should be used instead of just completing when the publish is confirmed, the data type must not be Void. In both cases above, the data type is String.
Server Side
The "server side" in this case starts with the consumption of a message, and then a new message is published by returning the result
Note
If the reply publish fails for any reason, the original message will be rejected.
Important
RPC consumer methods must never return a reactive type. Because the resulting publish needs to occur on the same thread and only a single item can be emitted, there is no value in doing so.
9 Message Serialization/Deserialization (SerDes)
The serialization and deserialization of message bodies is handled through instances of NatsMessageSerDes. The ser-des (Serializer/Deserializer) is responsible for both serialization and deserialization of Nats message bodies into the message body types defined in your clients and consumers methods.
The ser-des are managed by a NatsMessageSerDesRegistry. All ser-des beans are injected in order into the registry and then searched for when serialization or deserialization is needed. The first ser-des that returns true for supports-java.lang.Class- is returned and used.
By default, standard Java lang types and JSON format (with Jackson) are supported. You can supply your own ser-des by simply registering a bean of type NatsMessageSerDes. All ser-des implement the Ordered interface, so custom implementations can come before, after, or in between the default implementations.
9.1 Custom SerDes
A custom serializer/deserializer would be necessary to support custom data formats. In the section on Custom Consumer Binding an example was demonstrated that allowed binding a ProductInfo type from the headers of the message. If instead that object should represent the body of the message with a custom data format, you could register your own serializer/deserializer to do so.
In this example a simple data format of the string representation of the fields are concatenated together with a pipe character.
Tip
Because the getOrder method was not overridden, the default order of 0 is used. All default ser-des have a lower precedent than the default order which means this ser-des will be checked before the others.
10 NATS Health Indicator
This library comes with a health indicator for applications that are using the management module in Micronaut. See the Health Endpoint documentation for more information about the endpoint itself.
The information reported from the health indicator is under the nats key.
To disable the NATS health indicator entirely, add endpoints.health.nats.enabled: false.
11 Jetstream
Jetstream is built-in distributed persistence system built into Nats.io which enables new functionalities like
fault-tolerance
replication
exactly-once semantics
replay policies
retention policy and limits
streaming
11.1 Streams
Streams are 'message stores', each stream defines how messages are stored and what the limits (duration, size, interest) of the retention are.
Streams consume normal NATS subjects, any message published on those subjects will be captured in the defined storage system.
You can do a normal publish to the subject for unacknowledged delivery, though it’s better to use the JetStream publish calls instead as the JetStream server will reply with an acknowledgement that it was successfully stored.
The example in the quick start presented a trivial definition of an interface that be implemented automatically for you using the @JetstreamClient annotation.
The implementation that powers @JetstreamClient (defined by the JetStreamIntroductionAdvice class) is, however, very flexible and offers a range of options for defining Jetstream clients.
The @JetstreamClient extends the default @NatsClient and is based on the same methods.
So you can still use all header and subject functionalities as you already know.
@JetstreamClient have a special extension for the options you want to publish.
11.1.3 Consumer
A consumer is a stateful view of a stream. It acts as interface for clients to consume a subset of messages stored in a stream and will keep track of which messages were delivered and acknowledged by clients.
Unlike with core NATS which provides an at most once delivery guarantee of a message, a consumer can provide an at least once delivery guarantee. This is achieved by the combination of published messages being persisted to the stream as well as the consumer tracking delivery and acknowledgement of each individual message as clients receive and process them. JetStream consumers support multiple kinds of acknowledgements and multiple acknowledgement policies. They will take care of automatically re-deliver un-acked (or 'nacked') messages up to a user specified maximum number of delivery attempts (there is an advisory being emitted when a message reaches this limit).
Consumers can be push-based where messages will be delivered to a specified subject or pull-based which allows clients to request batches of messages on demand. The choice of what kind of consumer to use depends on the use-case but typically in the case of a client application that needs to get their own individual replay of messages from a stream you would use an 'ordered push consumer'. If there is a need to process messages and easily scale horizontally, you would use a 'pull consumer'.
In addition to the choice of being push or pull, a consumer can also be ephemeral or durable. A consumer is considered durable when an explicit name is set on the Durable field when creating the consumer, otherwise it is considered ephemeral. Durables and ephemeral behave exactly the same except that an ephemeral will be automatically cleaned up (deleted) after a period of inactivity, specifically when there are no subscriptions bound to the consumer. By default, durables will remain even when there are periods of inactivity (unless InactiveThreshold is set explicitly).
11.1.3.1 Push based
A push consumer is where the server is in control and sends messages to the client. It can be made durable or ephemeral based on your use case.
Push consumers are very similiar to the already known @NatsListener. Let’s look at a quick example.
11.1.3.2 Pull based
A pull consumer allows you to control when the server sends the client messages.
JetSteam, the persistence layer of NATS, doesn’t just allow for higher qualities of service and features associated with 'streaming', but it also enables some functionalities not found in messaging systems.
One such feature is the Key/Value store functionality, which allows client applications to create 'buckets' and use them as immediately consistent, persistent associative arrays.
You can use KV buckets to perform the typical operations you would expect from an immediately consistent key/value store:
put: associate a value with a key
get: retrieve the value associated with a key
delete: clear any value associated with a key
purge: clear all the values associated with all keys
create: associate the value with a key only if there is currently no value associated with that key (i.e. compare to null and set)
update: compare and set (aka compare and swap) the value for a key
keys: get a copy of all the keys (with a value or operation associated to it)
You can set limits for your buckets, such as:
- the maximum size of the bucket
- the maximum size for any single value
- a TTL: how long the store will keep values for
Finally, you can even do things that typically can not be done with a Key/Value Store:
watch: watch for changes happening for a key, which is similar to subscribing (in the publish/subscribe sense) to the key: the watcher receives updates due to put or delete operations on the key pushed to it in real-time as they happen
watch all: watch for all the changes happening on all the keys in the bucket
history: retrieve a history of the values (and delete operations) associated with each key over time (by default the history of buckets is set to 1, meaning that only the latest value/operation is stored)
Nats.io provides an KeyValue interface for the usage of Key/Value Stores.
To use it, just inject your Key/Value Store as follows:
11.3 Object Store (Experimental)
Warning
Experimental Preview
The Object Store allows you to store data of any (i.e. large) size by implementing a chunking mechanism, allowing you to for example store and retrieve files (i.e. the object) of any size by associating them with a path and a file name (i.e. the key). You obtain a ObjectStoreManager object from your JetStream context.