Message retry

更新时间:
复制 MD 格式

This topic describes the message retry mechanism and configuration methods for SOFAStack Message Queue.

Retry for ordered messages

For ordered messages, when a consumer fails to consume a message, Message Queue automatically retries delivering the message at 1 second intervals. This can block message consumption in the application. Therefore, when you use ordered messages, you must ensure that your application can monitor and handle consumption failures promptly to avoid blocking.

Retry for unordered messages

For unordered messages, such as normal, scheduled, delayed, and transactional messages, a message retry is triggered when the return status indicates a consumption failure.

Retries for unordered messages are supported only for clustering consumption. The broadcast consumption mode does not support retries. This means that if consumption fails, the message is not retried, and the consumer proceeds to the next message.

Note

The following content applies only to unordered messages.

Number of retries

By default, Message Queue retries each message up to 16 times. The intervals between retries are as follows:

Retry attempt

Interval since last retry

Retry attempt

Interval since last retry

1

10 seconds

9

7 minutes

2

30 seconds

10

8 minutes

3

1 minute

11

9 minutes

4

2 minutes

12

10 minutes

5

3 minutes

13

20 minutes

6

4 minutes

14

30 minutes

7

5 minutes

15

1 hour

8

6 minutes

16

2 hours

If a message is not consumed after 16 retries, it is no longer delivered. Based on the default retry intervals, a message that consistently fails to be consumed is retried 16 times over a period of 4 hours and 46 minutes. After this period, the message is not delivered again.

Note

The message ID remains the same regardless of the number of retries.

Configuration

Configure retries after consumption failure

For clustering consumption, to retry a message after a consumption failure, you must explicitly configure this behavior in your message listener implementation. You can use one of the following 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: If the message processing logic throws an exception, the message is retried.
        doConsumeMessage(message);
        // Method 1: Return Action.ReconsumeLater to retry the message.
        return Action.ReconsumeLater;
        // Method 2: Return null to retry the message.
        return null;
        // Method 3: Throw an exception directly to retry the message.
        throw new RuntimeException("Consumer Message exception");
    }
}

Configure no retry after consumption failure

For clustering consumption, to prevent a message from being retried after a consumption failure, you can catch any potential exceptions in your consumption logic and return Action.CommitMessage. 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){
            // Catch all exceptions in the consumption logic and return Action.CommitMessage.
            return Action.CommitMessage;
        }
        // If the message is processed normally, return Action.CommitMessage.
        return Action.CommitMessage;
    }
}

Customize the maximum number of retries

Message Queue lets you set the maximum number of retries when a consumer starts. The retry interval policy is as follows:

  • If the maximum number of retries is 16 or less, the retry intervals are the same as the default intervals.

  • If the maximum number of retries is greater than 16, the interval for the 17th retry and all subsequent retries is 2 hours.

You can configure it as follows:

  • SOFABOOT

    // Set the maximum number of retries to 20 for the corresponding Group ID.
    @MessageConsumer(group = "GID_xxx", topic = "TP_xxx", maxRetryTimes = 20)
  • Non-SOFABOOT

    Properties properties = new Properties();
    // Set the maximum number of retries to 20 for the corresponding Group ID.
    properties.put(PropertyKeyConst.MAX_RECONSUME_TIMES, "20");
    Consumer consumer = OMS.builder().driver("sofamq").createConsumer(properties);
Note

  • The setting for the maximum number of retries applies to all consumer instances that use the same Group ID.

  • If you set MaxReconsumeTimes for only one of several consumer instances that use the same Group ID, the configuration applies to all instances in that group.

  • The configuration is applied by overwriting. The configuration of the last consumer instance to start overwrites the configurations of previously started instances.

Get the number of retries

After a consumer receives a message, you can retrieve the number of retries for the message as follows:

public class MessageListenerImpl implements MessageListener {
    @Override
    public Action consume(Message message, ConsumeContext context) {
        // Get the number of retries for the message.
        System.out.println(message.getReconsumeTimes());
        return Action.CommitMessage;
    }
}