JMS Egress

Nstream provides a Java Message Service (JMS) Adapter library that greatly facilitates publishing to JMS topics and queues. This guide demonstrates how to start a message producer and publish messages from Web Agents using minimal boilerplate.




implementation 'io.nstream:nstream-adapter-jms:4.6.9'




Nstream provides the JmsPublishingPatch which with configuration and extension can publish state to JMS topics and queues.


Here we give a full example of the configuration required to prepare a JmsPublishingPatch - we will discuss the implementation of the MyJmsPublishingAgent in the next section.

# server.recon
provisions: {
  @provision("activeMqConnectionFactory") {
    class: "nstream.adapter.jms.ConnectionFactoryProvision"
    def: {
      "connectionFactoryClass": "org.apache.activemq.ActiveMQConnectionFactory"
      "brokerURL": "tcp://localhost:61616"
  @provision("activeMqConnection") {
    class: "nstream.adapter.jms.ConnectionProvision"
    def: {
      connectionFactoryProvisionName: "activeMqConnectionFactory"

vehicle: @fabric {
  @plane(class: "nstream.adapter.runtime.AppPlane")
  @node {
    uri: "/jms"
    @agent(class: "vehicle.jms.MyJmsPublishingAgent") {
      jmsEgressConf: @jmsEgressSettings {
        connectionProvisionName: "activeMqConnection",
        destination: "topic://vehicles",
        contentTypeOverride: "json"



A ConnectionFactory provision must be configured in order to create connections - see above for a simple example. connectionFactoryClass is a required property of the provision and must be the class name of the ConnectionFactory for the JMS provider you are using. Providing the ConnectionFactory class follows the Java Bean naming convention then properties of the ConnectionFactory can also be set here - such as brokerURL in the above example.

By referencing the ConnectionFactory provision, Connection provisions can now be created, although a single connection will often be sufficient per server, for use in agent configurations.


The destination JMS topic or queue can be configured in the jmsEgressConf as seen above. Topic and queue names are prepended with topic:// and queue:// respectively.


Using configuration to handle connections removes the need for most boilerplate in the implementation of the agent. Extending the JmsPublishingPatch exposes the publish(Value value) method which will publish the given value to the configured JMS destination. The only two implementation decisions now are what and when to publish - we give two examples in the following sections.

Publish On Event

The simplest method of publication is to publish any event or update received by a lane. All we need do is call the publish(Value value) method from the appropriate callback of the lane.

public class MyJmsPublishingAgent extends JmsPublishingPatch {
  CommandLane<Value> publish = this.<Value>commandLane()

Note: The didSet callback on a ValueLane is just as valid.


Another common approach to publication would be to publish the state of the agent periodically. In this case we must manage a timer, making use of the scheduling methods provided. The scheduling code is added in the callback of assignDestination to avoid attempting to publish before the connection has been created.

public class MyJmsPublishingAgent extends JmsPublishingPatch {

  private TimerRef timerRef;

  ValueLane<Value> state = this.<Value>valueLane();

  private void publishState() {

  protected void stagePublication() {
    assignDestination(this.egressSettings.destination(), () -> {
      info(nodeUri() + ": successfully staged producer for publication");
      this.timerRef = scheduleAtFixedRate(() -> this.timerRef, 1000, 60000, this::publishState);

Nstream is licensed under the Redis Source Available License 2.0 (RSALv2).