This topic describes the message retry mechanism and configuration method of Message Queue for Apache RocketMQ.

Retries for ordered messages

For ordered messages, when a consumer failed to consume a message, Message Queue for Apache RocketMQ automatically retries sending the message continuously at an interval of one second. This may lead to consumption blocking. Therefore, when you use ordered messages, ensure that the application can monitor and handle the consumption failures promptly to prevent blocking of message consumption.

Retries for unordered messages

Unordered messages include normal, scheduled, delayed, and transactional messages. When a consumer fails to consume such messages, you can set the returned status to achieve the same effect as message retry.

Unordered message retry takes effect only in clustering consumption pattern. In broadcasting consumption pattern, if message consumption fails, failed messages are not re-consumed, and new messages are consumed instead.

Notice Note: The following information is applicable to unordered messages only.

Number of retries

Message Queue for Apache RocketMQ allows a maximum of 16 retries for each message by default, and the interval for each retry is as follows:

Retry number Interval Retry number Interval
1 10s 9 7 min
2 30s 10 8 min
3 1 min 11 9 min
4 2 min 12 10 min
5 3 min 13 20 min
6 4 min 14 30 min
7 5 min 15 1 h
8 6 min 16 2 h

If a message fails after 16 retries, it will not be delivered. Strictly according to the intervals above, if the consumption of a message continuously fails, there will be 16 retries within 4 hours and 46 minutes, after which the message will not be delivered.

Notice The message ID of a retried message never changes regardless of the number of retries.

Configuration method

  • Configure retries after a message consumption failure

    In clustering consumption pattern, a message retry is expected after the message consumption failure, which needs to be configured in the implementation of the MessageListener interface in one of three methods:

    • Return Action.ReconsumeLater (recommended).
    • Return null.
    • Throw an exception.

    Sample code

    public class MessageListenerImpl implements MessageListener {
    
        @Override
        public Action consume(Message message, ConsumeContext context) {
            //Method 3: The message processing logic throws an exception, and the message will be retried.
            doConsumeMessage(message);
            //Mode 1: Return Action.ReconsumeLater, and the message will be retried.
            return Action.ReconsumeLater;
            //Mode 2: Return null, and the message will be retried.
            return null;
            //Mode 3: Directly throw an exception, and the message will be retried.
            throw new RuntimeException("Consumer Message exceotion");
        }
    }
  • Disable retries after a message consumption failure

    In clustering consumption pattern, no message retry is expected after a message consumption failure. The possible exception thrown by the consumption logic needs to be captured and Action.CommitMessage is returned. After that, the message will not be retried.

    Sample code

    public class MessageListenerImpl implements MessageListener {
    
        @Override
        public Action consume(Message message, ConsumeContext context) {
            try {
                doConsumeMessage(message);
            } catch (Throwable e) {
                //Capture all exceptions in the consumption logic, and return Action.CommitMessage.
                return Action.CommitMessage;
            }
            //The message processing is normal, and Action.CommitMessage is directly returned.
            return Action.CommitMessage;
        }
    }
  • Customize the maximum number of retries
    Note To customize the log configuration of Message Queue for Apache RocketMQ, you must upgrade the TCP Java SDK version to 1.2.2 or later.

    Message Queue for Apache RocketMQ allows you to set the maximum number of retries when the consumer is started. The retry interval complies with the following policies:

    • If the maximum number of retries is less than or equal to 16, the retry interval is as described in the preceding table.
    • If the maximum number of retries is greater than 16, the interval of the 17th or later retry is 2 hours.

    The configuration method is as follows:

    Properties properties = new Properties();
    //Set the maximum number of message retries for the corresponding group ID to 20.
    properties.put(PropertyKeyConst.MaxReconsumeTimes,"20");
    Consumer consumer =ONSFactory.createConsumer(properties);
    Notice
    • The configuration of the maximum number of message retries applies to all consumer instances with the same group ID.
    • If the maximum number of message retries is set for only one of the two consumer instances with the same group ID, the configuration applies to both consumer instances.
    • The configuration takes effect by overwriting, that is, the configurations of the last started consumer instance will overwrite those of the previously started instances.

Retrieve the number of retries

Upon receiving the message, the consumer can retrieve the number of retries with the following method:

public class MessageListenerImpl implements MessageListener {

    @Override
    public Action consume(Message message, ConsumeContext context) {
        //Retrieve the number of retries.
        System.out.println(message.getReconsumeTimes());
        return Action.CommitMessage;
    }
}