Configure Message Acknowledgement with the JMS Connector
Anypoint Connector for JMS (JMS Connector) provides different message acknowledgement modes to configure for the Ack, Consume, On New Message, and Recovery session operations:
-
Automatic acknowledgement on success
-
Immediate acknowledgement
-
Manual acknowledgement
The following examples illustrate how to configure each message acknowledgement mode.
Configure Automatic Acknowledgement on Success Mode
Configure the automatic acknowledgement on success mode in the On New Message source by setting the Acknowledge Mode field to either one of these modes:
-
AUTO
Automatically acknowledges (ACK) the received message only if the flow execution completes successfully; otherwise, the message is not acknowledged and is redelivered. -
DUPS_OK
Like AUTO mode, automatically acknowledges (ACK) the received message only if the flow execution completes successfully, but leads to duplicate messages if the message is redelivered before the ACK is performed.
In the following example, you configure the AUTO acknowledgement mode:
-
In Studio, select the On New Message source from your flow.
-
In the On New Message configuration screen, in Destination, specify the name of the destination from which to consume the message, for example,
#[vars.destination]
. -
From the Acknowledge Mode menu, select AUTO.
In the XML editor, the <jms:listener>
and ackMode
configurations look like this:
<jms:listener config-ref="JMS_Config" destination="#[vars.destination]" ackMode="AUTO"/>
Configure Immediate Acknowledgement Mode
Configure the immediate acknowledgement mode in the On New Message source by using the Acknowledge Mode field, or in the Consume operation by using the Ack mode field. Set the fields to IMMEDIATE. This mode automatically acknowledges (ACK) the message after it is consumed and before to any application processing of the message.
The result of automatically acknowledging the message is that the message won’t be redelivered if any error occurs during message processing, requiring application logic like a "dead letter" queue to manage errors without losing messages.
In the following example, you configure the IMMEDIATE acknowledgement mode for the Consume operation:
-
In Studio, select the Consume operation from your flow.
-
In the Consume configuration screen, in Destination, specify the name of the destination from which to consume the message, for example,
#[vars.destination]
. -
From the Ack mode menu, select IMMEDIATE.
In the XML editor, the <jms:consume>
and ackMode
configurations look like this:
Configure Manual Acknowledgement Mode
Configure the manual acknowledgement mode in the On New Message source by using the Acknowledge Mode field, or in the Consume operation by using the Ack mode field. Set these fields to MANUAL. This mode delegates all responsibility for performing message acknowledgement (ACK) to the application logic.
Every message received by either the On New Message or Consume operations includes an acknowledgement ID in the Mule message attributes, that identifies the message uniquely for a given connection.
The acknowledgement ID that identifies the message is then passed to the Ack operation in the Ack id field.
In the following example, you configure the MANUAL acknowledgement mode for the Consume operation:
-
In Studio, select the Consume operation from your flow.
-
In the Consume configuration screen, in Destination, specify the name of the destination from which to consume the message, for example,
openTickets
. -
From the Ack mode menu, select MANUAL.
-
Drag a Logger component to the right of the Consume operation.
-
Set the Message field to
#[payload]
. -
Drag an Ack operation to the right of the Logger component.
-
Set the Ack id field to the acknowledgement ID of the message session to recover, for example,
#[attributes.ackId]
.
In the XML editor, the <jms:consume>
, ackMode
, and ackId
configurations look like this:
<flow name="consumerWithManualAck">
<jms:consume config-ref="JMS_config" destination="openTickets" ackMode="MANUAL"/>
<!--Do message processing-->
<logger message="#[payload]">
<jms:ack ackId="#[attributes.ackId]"/>
</flow>
Configure Manual Session Recovery
With manual acknowledgement mode, all messages received but not acknowledged are not redelivered by the broker. If there is an error during the message processing, you must manually recover all the messages in the session that have to be redelivered by using the Recover session operation. The connection used to recover the session has to be the same as the connection for receiving the message.
In the following example, you configure the MANUAL acknowledgement mode and how to recover the session:
-
In Studio, select the Consume operation from your flow.
-
In the Consume configuration screen, in Destination, specify the name of the destination from which to consume the message, for example,
${destination}
. -
Set the Ack mode field to MANUAL.
-
Drag a Logger component to the right of the Consume operation.
-
Set the Message field to
#[payload]
. -
Drag an Ack operation to the right of the Logger component.
-
Set the Ack id field to the acknowledgement ID of the message session to recover, for example,
#[vars.consumedMessage.attributes.ackId]
. -
In your flow, expand the Error handling section by clicking the small arrow.
-
Drag an On Error Propagate component in the Error handling section.
-
Drag a Recover session operation inside the On Error Propagate component.
-
Set the Ack id field to
#[vars.consumedMessage.attributes.ackId]
.
In the XML editor, the <jms:consume>
, ackMode
, and ackId
configurations look like this:
<flow name="consumerWithManualAck">
<jms:consume config-ref="JMS_config" destination="${destination}"
ackMode="MANUAL" target="consumedMessage" targetValue="#[message]"/>
<!--Do message processing-->
<logger message="#[payload]">
<jms:ack ackId="#[vars.consumedMessage.attributes.ackId]"/>
<error-handler>
<on-error-continue>
<!--In case of error, recover the session-->
<jms:recover-session ackId="#[vars.consumedMessage.attributes.ackId]"/>
</on-error-continue>
</error-handler>
</flow>