NATS Sink
Provided by: "Apache Software Foundation"
Support Level for this Kamelet is: "Stable"
Publishes each message to a subject on a NATS server, or to a JetStream stream that stores the messages.
Use it to send events or commands to services that listen on NATS, such as microservices or edge devices. Use nats-source to receive them.
Input: the body is the payload, as text or bytes. A Map is sent as its Java text form, such as {a=1, b=x}, so turn it into JSON text first, for example with json-serialize-action. Headers of the message, such as Content-Type, are sent as NATS headers.
Output: the message is unchanged.
Good to know: a plain NATS subject stores nothing: only the receivers subscribed at that moment get the message, unless a JetStream stream takes in the subject. With jetstreamEnabled, the sink waits until the stream that takes in the subject has stored the message, and sending fails with 503 No Responders Available For Request when no stream takes it in. The stream must exist already, for example created by nats-source. On a server without JetStream, jetstreamEnabled is ignored and the message is published as plain NATS. It connects with TLS when secure is true. The certificate of the server is checked against the certificate authorities that Java trusts. To trust a private one with Camel JBang or Camel Main, set the application properties camel.ssl.enabled=true, camel.ssl.trustStore, camel.ssl.trustStorePassword and camel.component.nats.useGlobalSslContextParameters=true: that trust store then replaces the one of Java for every NATS connection of the application that has secure set. The integration fails to start when no server can be reached, or when the server refuses the login. It publishes the message and does not wait for a reply, even when the incoming message expects one, as one from webhook-source does. Headers that start with CamelNats are removed from the message first, so they cannot change what the Kamelet does.
Configuration Options
The following table summarizes the configuration options available for the nats-sink Kamelet:
| Property | Name | Description | Type | Default | Example |
|---|---|---|---|---|---|
Servers | Required The addresses of one or more NATS servers, as | string | nats://localhost:4222 | ||
Subject | Required The NATS subject to publish the messages to. | string | orders.created | ||
Credentials File | A NATS credentials file ( | string | file:/etc/nats/user.creds | ||
JetStream Async | How the sink waits for JetStream to confirm that it stored a message. Either way it waits before it sends the next message. When the server does not answer, sending fails after about 2 seconds when false, and after 2 to 8 seconds when true. Only used when | boolean | true | ||
JetStream Enabled | If true, each message is published to JetStream, and the sink waits until the stream that takes in the subject in | boolean | false | ||
JetStream Stream Name | Not used by the sink: the message is stored by the stream that takes in the subject in | string | ORDERS | ||
Secure (TLS) | If true, it connects with TLS, which a server with TLS in its configuration requires; without it, such a server refuses the connection with | boolean |
Dependencies
At runtime, the nats-sink Kamelet relies upon the presence of the following dependencies:
-
camel:nats
-
camel:kamelet
Camel JBang usage
Prerequisites
-
You’ve installed JBang.
-
You have executed the following command:
jbang app install camel@apache/camel Supposing you have a file named route.yaml with this content:
- route:
from:
uri: "kamelet:timer-source"
parameters:
period: 10000
message: 'test'
steps:
- to:
uri: "kamelet:nats-sink" You can now run it directly through the following command
camel run route.yaml NATS Sink Kamelet Description
NATS Messaging System
This Kamelet integrates with NATS, a high-performance messaging system designed for cloud-native applications, IoT messaging, and microservices architectures.
High Performance
NATS provides extremely high throughput and low latency messaging, making it suitable for real-time applications and high-frequency trading systems.
Subject-Based Messaging
Uses NATS subject-based messaging where messages are published to subjects (similar to topics) for efficient message routing and delivery.
Cloud-Native Design
NATS is designed for cloud-native environments with features like:
-
Horizontal scaling capabilities
-
Fault tolerance and high availability
-
Multi-tenancy support
-
Service mesh integration
Login
For a server that needs a user and password, put them in the address in servers, as nats://user:password@host:4222, or put a token there, as nats://token@host:4222.
For a server with decentralized authentication, such as Synadia Cloud, log in with a NATS credentials file (.creds), which holds a user JWT and the NKey seed of the user. Set its location in credentialsFilePath, as file: followed by the path of the file. The nsc generate creds command makes one:
nsc generate creds --account APP --name app-user > app-user.creds TLS with a Private Certificate Authority
With secure: true the Kamelet connects with TLS, which a server with TLS in its configuration requires, and checks the certificate of the server against the certificate authorities that Java trusts. When the server has a certificate from a private or self-signed authority, put the certificate of that authority in a trust store file (JKS or PKCS12):
keytool -importcert -alias nats-ca -file ca.pem -keystore truststore.p12 -storetype PKCS12 -storepass changeit The Kamelet has no option for the trust store. Set it in the application properties, as the global TLS settings of Camel, and tell the NATS component to use them. With Camel JBang or Camel Main, in application.properties:
camel.ssl.enabled=true
camel.ssl.trustStore=file:/etc/nats/truststore.p12
camel.ssl.trustStorePassword=changeit
camel.component.nats.useGlobalSslContextParameters=true That trust store then replaces the one of Java for every NATS connection of the application that has secure set. An address that starts with tls:// also connects with TLS, but it uses that trust store only when secure is true.
Example with TLS and a Credentials File
- route:
from:
uri: "kamelet:timer-source"
parameters:
message: "hello"
period: 5000
steps:
- to:
uri: "kamelet:nats-sink"
parameters:
servers: "tls://nats.example.com:4222"
topic: "orders.created"
secure: true
credentialsFilePath: "file:/etc/nats/app-user.creds" JetStream
A plain NATS subject stores nothing: only the receivers subscribed at that moment get the message. A JetStream stream stores the messages of the subjects it takes in. With jetstreamEnabled: true, the Kamelet waits until that stream has stored each message, and sending fails with 503 No Responders Available For Request when no stream takes in the subject in topic. The stream must exist already: create it with nats-source and jetstreamEnabled, or with the nats command line tool:
nats stream add ORDERS --subjects "orders.created" --defaults The sink does not use jetstreamName: the message goes to the stream that takes in the subject, whatever its name.
- route:
from:
uri: "kamelet:timer-source"
parameters:
message: "hello"
period: 5000
steps:
- to:
uri: "kamelet:nats-sink"
parameters:
servers: "nats://localhost:4222"
topic: "orders.created"
jetstreamEnabled: true