edit-icon download-icon

Message retry

Last Updated: Jul 03, 2018

Retries for ordered messages

For ordered messages, when the consumer failed to consume a message, MQ will automatically retry sending the message continuously with an interval of one second. This may lead to the occurance of consumption blocking. Therefore, when you use ordered messages, make sure that your application can monitor and handle the message consumption failure timely, so that the consumption blocking can be avoided.

Retries for unordered messages

For unordered messages (normal,scheduled, delayed, and transactional messages), when the consumer failed to consume the messages, message retry can be achieved by setting return status.

MQ message retry is only applicable to clustering consumption mode, and is not supported in broadcast mode. That is, when the consumption fails, the failed message will not be retried, and a new message will be consumed.

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

Number of retries

MQ allows 16 retries for each message by default, and the intervals for each retry is as follows:

Retry number Interval Retry number Interval
1 10 s 9 7 min
2 30 s 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 min
8 6 min 16 2 h

If the message fails after 16 retries, it will not be delivered. If calculated strictly according to the intervals above, there will be 16 retries in 4 h and 46 min for a failed message, after which the message will not be delivered.

Note: No matter how many retries are performed for a message, the message ID will not be changed.

Configuration Method

Configure retries after consumption failure

In clustering consumption mode, to enable message retry after message consumption failure, you need to make explicit configurations in the implementation of the message listener interface (three methods available):

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

Sample code

  1. public class MessageListenerImpl implements MessageListener {
  2. @Override
  3. public Action consume(Message message, ConsumeContext context) {
  4. //Method 3: The message process logic throws an exception, and the message will be retried.
  5. doConsumeMessage(message);
  6. //Method 1: Return Action.ReconsumeLater, and the message will be retried.
  7. return Action.ReconsumeLater;
  8. //Mthod 2: Return null, and the message will be retried.
  9. return null;
  10. //Method 3: Directly throw an exception, and the message will be retried.
  11. throw new RuntimeException("Consumer Message exceotion");
  12. }
  13. }

Disable retries after consumption failure

In clustering consumption mode, to disable message retries after consumption failure, you need to catch exceptions that might be thrown in the consumption logic, and return Action.CommitMessage. Then the message will not be retried. Sample code as follows:

  1. public class MessageListenerImpl implements MessageListener {
  2. @Override
  3. public Action consume(Message message, ConsumeContext context) {
  4. try {
  5. doConsumeMessage(message);
  6. } catch (Throwable e) {
  7. //Catch all exceptions in the consumption logic, and return Action.CommitMessage;
  8. return Action.CommitMessage;
  9. }
  10. //Message processing is normal, directly return Action.CommitMessage;
  11. return Action.CommitMessage;
  12. }
  13. }

Customize the maximum number of message retries

To customize log configuration of the MQ client, please update to TCP Java SDK v1.2.2 or a later version.

MQ allows you to set the maximum number of retries when the consumer is started, and the retry intervals are based on the following policies:

  • If the maximum number of retries is smaller than or equal to 16, the retry intervals are as described in the table above.
  • If the maximum number of retries is larger than 16, each of the intervals exceeding 16 is 2 hours.

The configuration method is as follows:

  1. Properties properties = new Properties();
  2. // Configure the maximum number of message retries of corresponding Consumer ID to 20.
  3. properties.put(PropertyKeyConst.MaxReconsumeTimes,"20");
  4. Consumer consumer =ONSFactory.createConsumer(properties);

Note:

  • The configuration of maximum retry numbers applies to all consumer instances with the same consumer ID.
  • If MaxReconsumeTimes is set for only one of the two consumer instances with the same consumer ID, the configuration applies to both consumer instances.
  • The configuration will apply by overwriting, that is, the last started Consumer instance will overwrite the configuration of previously started instances.

Catch the number of retries

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

  1. public class MessageListenerImpl implements MessageListener {
  2. @Override
  3. public Action consume(Message message, ConsumeContext context) {
  4. //Catch the number of retries
  5. System.out.println(message.getReconsumeTimes());
  6. return Action.CommitMessage;
  7. }
  8. }
Thank you! We've received your feedback.