To Consume JMS Messages

The Consume operation in the JMS connector provides ability to consume a Message at any given time of the flow, from any given Destination.

Consuming A Message

The syntax to consume a message from a Queue is:

<jms:consume config-ref="JMS_config" destination="#[vars.destination]"/>

The operation above consumes the first available message in the Queue identified by the destination, and then converts it to a MuleMessage resulting in the following structure:

  • The message’s content as payload.

  • The message’s metadata in the message attributes.

Once received, the Message will be immediately ACKed by default. If you want to control the ACK of the Message after some processing, then ackMode should be set to MANUAL For more information regarding a Message ACK, check the How To Handle Message Acknowledgement.

Waiting for a Message

By default, the maximum wait time is configured in 10 seconds, producing a JMS:TIMEOUT error if no message is available in that period. If order to configure a timeout that fits better your use case, customize the maximumWait and maximumWaitUnit parameters.

In order to wait indefinitely for a Message to arrive, the maximumWait value has to be set to -1. In this case, no TIMEOUT error will be thrown.

Polling for a Message

When polling for a Message with a certain frequency, you don’t want the consumer to block waiting for the Message, but instead just try to consume it if one is present. In this cases, the maximumWait can be set to 0, indicating that no waiting should occur, and neither a JMS:TIMEOUT should be thrown if no Message is available in the destination. The output of the consume operation will be null when no Message is present for consumption and no error is thrown.

Filtering Incoming messages

The JMS connector provides full support for filtering which Messages should be consumed based on the standard JMS selector language.

For example, if you have a priority Queue with Messages that need to be processed faster than the others, we can do:

<jms:consume config-ref="JMS_config" destination="openTickets" selector="JMSPriority=8"/>

When a selector is configured, only the Messages matching it will be received, ignoring if any other Message is in the destination.

Mime Types and Encoding

The JMS Connector does its best to auto determine a Message’s mime type (contentType) based on the MM_MESSAGE_CONTENT_TYPE property. However, there are cases in which that best guess is not enough, and you need first-hand knowledge of the Message’s content.

In such cases, you can force that content type to a particular value by using the contentType parameter.

The same process works for encoding. By default, the connector will assume that the runtime’s default encoding matches the one in the Message if no other information is provided. You can set this by using the encoding parameter.

Consuming A Message Using Topic Subscriptions

Consuming messages from a Topic destination is very similar to consuming them from a Queue, but with the extra functionality of being able to use Topic subscriptions to share state between consumers. The syntax for consuming messages from a Topic is:

<jms:consume config-ref="JMS_config" destination="#[vars.destination]">

All the consume operation parameters remain the same, but now you can handle the subscription configuration in the topic-consumer parameter.

For details on how to configure the Topic subscriptions go to Using Topic Subscriptions.

Incoming Message Metadata

As stated before, each Message received will consist of two parts:

  • The payload, containing the content of the Message

  • The attributes, containing metadata regarding the Message

This Metadata has three parts that map all the information available in a JMS Message:

  • AckId

  • Headers

  • Properties

Check the JMS Reference for details about the Attributes structure.

Was this article helpful?

💙 Thanks for your feedback!

View on GitHub