<flow name="consumerWithManualAck">
<scheduler>
<scheduling-strategy >
<fixed-frequency />
</scheduling-strategy>
</scheduler>
<!-- Consume a message -->
<anypoint-mq:consume destination="${destination}"
acknowledgementMode="MANUAL"
config-ref="Anypoint_MQ_Config"/>
<!-- Save the ackToken for future acknowledgment -->
<set-variable variableName="currentAckToken"
value="#[attributes.ackToken]"/>
<!-- Do the required processing of the message -->
<flow-ref name="process-message"/>
<!-- Use the ackToken to ACK the message -->
<anypoint-mq:ack ackToken="#[vars.currentAckToken]"
config-ref="Anypoint_MQ_Config" />
</flow>
Anypoint MQ ACK and NACK Operations - Mule 4
Acknowledging a message (ACK) informs the Anypoint MQ broker that the message has been processed and must be removed from the queue to prevent redelivery. Not acknowledging a message (NACK) tells the broker to not process the message and to return the message to the queue for redelivery to any available consumer.
Acknowledgment Modes
The Anypoint MQ connector provides three acknowledgment modes:
-
Immediate
Use Immediate when you want to acknowledge messages as your app consumes them, before processing them.
-
Automatic
Use Automatic to acknowledge a received message automatically only if the flow execution finishes successfully.
-
Manual
Use Manual if you want the app logic to control message acknowledgment.
Immediate Acknowledgment
Use the IMMEDIATE
acknowledgment mode to acknowledge messages as your app consumes them and before processing them.
When Anypoint MQ acknowledges a message, it removes the message from the queue and doesn’t redeliver it if an error occurs during processing.
If the app restarts between being acknowledged and dispatched, the message might not be available again because it was deleted from the queue.
To prevent message deletion until after processing, use the AUTO
or MANUAL
acknowledgment mode instead.
If an error occurs during message processing using IMMEDIATE
mode, Anypoint MQ throws an ANYPOINT-MQ:ACKING
error.
To manage errors without losing messages, use a dead letter queue.
Automatic Acknowledgment
Use the AUTO
acknowledgment mode to acknowledge a received message automatically only if the flow execution finishes successfully.
If an error occurs during flow execution, the message is not acknowledged ('NACK') and the message is returned to the queue for redelivery.
Manual Acknowledgment
Use the MANUAL
acknowledgment mode to delegate all responsibility for message acknowledgment to the app logic.
With this configuration, every message received has an ackToken
value that identifies this message uniquely for a given connection.
Save this ackToken
value to use later as input to the ACK or NACK operations.
Acknowledgment Timeout
When using either AUTO
or MANUAL
acknowledgment modes, you can use the acknowledgementTimeout
parameter to control how long the message remains in-flight waiting to be acknowledged, before automatically returning to the queue.
The acknowledgementTimeout
timer begins when the message is consumed.
If acknowledgementTimeout
is not set, Anypoint MQ Connector uses two minutes.
If you need a different TTL, you must define it explicitly in the acknowledgementTimeout
parameter of the subscriber.
When using acknowledgementTimeout
, consider expected app-time processing, including time for unexpected delays, such as external systems delay and app back-pressure due to high load. For example, if you expect the consumed message to be processed in 10 seconds, set acknowledgementTimeout
to a minimum of 15 seconds.
When using the For this reason, don’t use |
Acknowledgment Timeout — AUTO Acknowledgement Mode
After a subscriber consumes a message, a timer starts, indicating how long the message has been in-flight for the broker waiting for acknowledgment. If the flow processing does not end with a success or failure before the acknowledgementTimeout
limit is reached, the message automatically returns to the queue for redelivery.
Acknowledgment Timeout — MANUAL Acknowledgement Mode
When consuming a message using MANUAL
acknowledgment mode by either the Subscriber or Consume operations, the app must execute either an ACK or NACK operation using the message ackToken
before the acknowledgementTimeout
limit is reached. If neither operation executes with the given token before the timeout, the message automatically returns to the queue for redelivery. Any attempt to acknowledge the message after the timeout limit fails due to an expired token.
ACK Operation — MANUAL Acknowledgement Mode
Acknowledging a message (ACK) informs the broker that the message has been processed and must be removed from the queue to prevent redelivery.
When performing the ACK operation using Manual mode, you must use the acknowledgment token (ackToken
) to identify the message. It is available in the ackToken
field of the message attributes:
If a non-void operation is invoked during message processing, the payload and attributes of the Mule message are modified. To perform an ACK operation after processing, you must save ackToken
in a variable.
To save attributes to use later, use the target
and targetValue
parameters to store the whole message in a variable:
<flow name="consumerWithManualAck">
<scheduler>
<scheduling-strategy >
<fixed-frequency />
</scheduling-strategy>
</scheduler>
<!-- Consume a message -->
<anypoint-mq:consume destination="${destination}"
acknowledgementMode="MANUAL"
config-ref="Anypoint_MQ_Config"
target="mqMessage"
targetValue="#[message]"/>
<!--Do any message processing-->
<jms:publish-consume destination="#[vars.mqMessage.attributes.targetDestination]"
config-ref="JMS_Config">
<jms:message>
<jms:body>#[vars.mqMessage.payload]</jms:body>
</jms:message>
</jms:publish-consume>
<!-- Use the ackToken to ACK the message -->
<anypoint-mq:ack ackToken="#[vars.mqMessage.attributes.ackToken]"
config-ref="Anypoint_MQ_Config" />
</flow>
NACK Operation
Not acknowledging (NACK) a message informs the broker that the message was not processed successfully and commands the broker to return the message to the queue for redelivery to any available consumer.
The acknowledgment token ackToken
is a unique identifier for the message that you must use when executing the NACK operation. It is available in the ackToken
field of the message attributes:
<flow name="consumerWithManualAck">
<scheduler>
<scheduling-strategy >
<fixed-frequency />
</scheduling-strategy>
</scheduler>
<!-- Consume a message -->
<anypoint-mq:consume destination="${destination}"
acknowledgementMode="MANUAL"
config-ref="Anypoint_MQ_Config"
target="mqMessage"
targetValue="#[message]"/>
<!--Do message processing -->
<logger message="#[payload]"/>
<!-- Use the ackToken to NACK the message -->
<anypoint-mq:nack ackToken="#[vars.mqMessage.attributes.ackToken]"
config-ref="Anypoint_MQ_Config" />
</flow>