JMS with Ballerina lang: Part 4

Advanced JMS concepts

Ayesh Almeida
3 min readMay 21, 2024

(This article is written using Ballerina Swan Lake 2201.9.0)

In this article we will cover several advanced JMS concepts and how to implement them using Ballerina language.

Using different types of messages

JMS supports various message types, such as text, map, object, bytes, and streams. In the Ballerina JMS connector, we specifically support text messages, map messages, and bytes messages, as they cover the majority of use cases.

Text Message

A text message contains a string of characters as its payload. It is commonly used for sending textual data between JMS applications. Text messages are suitable for scenarios where simple text-based communication is required.

Map Message

A map message contains a set of name-value pairs, where each value can be of a different type. This message type is useful for representing structured data with key-value semantics. Map messages are often used when sending complex data structures between JMS applications.

Bytes Message

A bytes message contains a stream of uninterpreted bytes as its payload. This message type provides flexibility for sending binary data, such as images or files, between JMS applications. Bytes messages are suitable for scenarios where raw binary data needs to be transmitted without any interpretation or modification.

Using async message listener

A jms:Service can be used to connect to a JMS provider and receive messages asynchronously using a jms:Listener. A jms:Listener can be initialized by providing the connection configurations and the relevant jms:Destination.

When a new message arrives in the specified jms:Destination , the onMessage method of the jms:Service is triggered, passing along the message payload. This mechanism offers a non-blocking asynchronous approach to receive messages from a JMS provider.

Using manual message acknowledgement

In JMS, manual message acknowledgement refers to the process where the application explicitly signals that a message has been successfully received and processed. Unlike automatic acknowledgement, where the JMS provider automatically acknowledges the receipt of a message once it has been delivered, manual acknowledgement requires the application to call the acknowledge method on the received message.

Using (local) transactions

A transaction is a sequence of operations performed as a single logical unit of work. The primary purpose of using transactions is to ensure data integrity and consistency.

In JMS, both message producers and message consumers support transactions. A transaction is initiated by creating a jms:Session in SESSION_TRANSACTED mode and and is concluded by calling either the commit or rollback method of the jms:Session .

When using a JMS producer, transactions ensure that a group of messages sent to the JMS provider are delivered as a single unit. If any message fails to be delivered, all messages within that transaction are rolled back.

When using a JMS consumer, transactions ensure that a group of messages received from the JMS provider are processed as a single unit. If the processing of any message fails, all messages within that transaction are rolled back, and the messages can be re-delivered.

Using request-reply messaging

Request-reply messaging is a pattern used in messaging systems where a client sends a request message to a service and waits for a reply message. This pattern is used when a response is required from the service to proceed with the next steps in the client’s processing. This is typically used the point-to-point (queue) messaging model. In this model, messages are sent to a specific queue, and a single consumer reads and processes the messages. The reply message is usually sent back to a queue created by the client.

Producer will send the request message to the request queue, by specifying the reply queue in the replyTo field (which will be set as the JMSReplyTo header).

Consumer will listen to the request queue, and replies to the reply queue specified in the message.

Using a correlation ID is a good practice in JMS request-reply messaging to match requests with their corresponding replies, especially when multiple requests are in-flight simultaneously.

In this article we discussed how to implement several advanced messaging concepts with Ballerina JMS connector. In the next article we will see how we can implement a real-world use cases using Ballerina JMS connector.

--

--

Ayesh Almeida

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