JMS with Ballerina lang: Part 3

Writing your first Ballerina JMS application

Ayesh Almeida
3 min readMay 21, 2024

(This article is written using Ballerina Swan Lake 2201.9.0)

In this article, we will cover how to write your first JMS application in Ballerina. We will use ActiveMQ Classic as the JMS provider, and you will need to set up the broker to test the application.

JMS connection

As we discussed previously Connection objects represents an active connection to the JMS provider. We could initialize a jms:Connection by providing the initialContextFactory and the provierUrl .

  • initialContextFactory : JMS provider specific initial context factory
  • providerUrl : JMS provider URL used to configure a connection

JMS session

jms:Session is a single-threaded context used to send and receive messages. We could initialize a jms:Session using a jms:Connection instance.

When initializing a jms:Session we could provide jms:AcknowledgementMode as an argument. There are four session acknowledgement modes available.

SESSION_TRANSACTED

Indicates that the session will use a local transaction which may subsequently be committed or rolled back by calling the session’s `commit` or `rollback` methods.

AUTO_ACKNOWLEDGE (default)

Indicates that the session automatically acknowledges a client’s receipt of a message either when the session has successfully returned from a call to `receive` or when the message listener the session has called to process the message successfully returns.

CLIENT_ACKNOWLEDGE

Indicates that the client acknowledges a consumed message by calling the MessageConsumer’s or Caller’s `acknowledge` method. Acknowledging a consumed message acknowledges all messages that the session has consumed.

DUPS_OK_ACKNOWLEDGE

Indicates that the session to lazily acknowledge the delivery of messages. This is likely to result in the delivery of some duplicate messages if the JMS provider fails, so it should only be used by consumers that can tolerate duplicate messages. Use of this mode can reduce session overhead by minimizing the work the session does to prevent duplicates.

JMS message producer

A jms:MessageProducer is used to send messages to a JMS destination. We can initialize a jms:MessageProducer either by specifying a jms:Destination or without providing one.

JMS message consumer

A jms:MessageConsumer is used to receive messages from a JMS destination. We could initialize a jms:MessageConsumer by providing a jms:Destination .

Sending messages

If the jms:MessageProducer is initialized with a jms:Destination , we should use send method to publish messages. Otherwise, we should use sendTo method.

Receiving messages

We use the receive method or the receiveNoWait method of jms:MessageConsumer to receive messages from a jms:Destination. The key difference between these two methods is that the receive method waits for a specified timeout before returning, whereas the receiveNoWait method returns immediately. The default timeout for the receive method is 10 seconds.

In addition to the above, you also need to specify a driver (or a JMS implementation) to connect to the ActiveMQ broker. The Ballerina ActiveMQ driver can be used for this purpose.

In this article we discussed how to write the first JMS based application using Ballerina language. In the next article we will see how we can implement some complex JMS concepts using Ballerina language.

--

--

Ayesh Almeida

A Tech Enthusiast who wants to build simple solutions for complex problems.