All Products
Search
Document Center

Message retry

Last Updated: Sep 13, 2019

Retries for ordered messages

For ordered messages, when the consumer failed to consume a message, MQ will automatically retry sending the message continuously at an interval of one second. This may lead to the occurrence of 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 mode. In broadcasting consumption mode, if message consumption fails, failed messages are not re-consumed, and new messages are consumed instead.

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

Number of retries

MQ allows a maximum of 16 retries for each message by default, and the intervals 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 the 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.

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

Configuration method

Configure retries after a message consumption failure

In clustering consumption mode, a message retry is expected after the message consumption failure, which needs to be configured 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. //Mode 1: Return Action.ReconsumeLater, and the message will be retried.
  7. return Action.ReconsumeLater;
  8. //Mode 2: Return null, and the message will be retried.
  9. return null;
  10. //Mode 3: Directly throw an exception, and the message will be retried.
  11. throw new RuntimeException("Consumer Message exception");
  12. }
  13. }

Disable retries after a message consumption failure

In clustering consumption mode, 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

  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. //Capture all exceptions in the consumption logic, and return Action.CommitMessage;
  8. return Action.CommitMessage;
  9. }
  10. //The message processing is normal, and Action.CommitMessage is directly returned
  11. return Action.CommitMessage;
  12. }
  13. }

Customize the maximum number of retries

To customize the log configuration of the MQ client, you must upgrade the TCP Java SDK to 1.2.2 or later.

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

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

Note:

  • 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 last started consumer instance will overwrite the configuration of previously started instances.

Obtain 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. //Obtain the number of retries
  5. System.out.println(message.getReconsumeTimes());
  6. return Action.CommitMessage;
  7. }
  8. }