Contact Free trial Login

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, before processing them.

Acknowledging the message automatically when it’s received means that the message isn’t redelivered if an error occurs during processing. To manage errors without losing messages, use a dead letter queue.

If an error occurs during message processing using IMMEDIATE mode, an ANYPOINT-MQ:ACKING error is thrown.

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.

By default, the connector honors the time defined at the queue level (Message Lock Default TTL value) in the Anypoint Platform MQ console.

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.

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:

<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>

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>

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.