RocketMQ message retry

Retry of sequence message

For sequential messages, when the consumer fails to consume the message, the message queue RocketMQ will automatically retry the message continuously (each time interval is 1 second). At this time, the application will block the message consumption. Therefore, when using sequential messages, you must ensure that the application can monitor and handle consumption failures in time to avoid blocking.

Retry of out-of-order messages

For out-of-order messages (normal, timed, delayed, transactional messages), when the consumer fails to consume the message, you can set the return status to achieve the result of the message retry.

The retry of out-of-order messages only takes effect for the cluster consumption mode; the broadcast mode does not provide the failure retry feature, that is, after the consumption fails, the failed message will not be retried, and new messages will continue to be consumed.

number of retries

The message queue RocketMQ allows each message to be retried up to 16 times by default, and the interval between each retry is as follows:

Retry for the first time Time between last retry Retry for the first time Time between 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 the message still fails after 16 retries, the message will no longer be delivered. If calculated in strict accordance with the above retry interval, a message will be retried 16 times in the next 4 hours and 46 minutes under the premise that the consumption has been failed. Messages will not be retried for delivery after this time range. .

Note: No matter how many times a message is retried, the Message ID of these retry messages will not change.

Configuration method

After the consumption fails, retry the configuration mode. In the
cluster consumption mode, the message is expected to be retried after the message consumption fails. It needs to be explicitly configured in the implementation of the message listener interface (choose one of the three methods):

  • Return to Action.ReconsumeLater (recommended)
  • Return Null
  • Throw an exception
public class MessageListenerImpl implements MessageListener {
    
    
    @Override
    public Action consume(Message message, ConsumeContext context) {
    
    
        //处理消息
        doConsumeMessage(message);
        //方式1:返回 Action.ReconsumeLater,消息将重试
        return Action.ReconsumeLater;
        //方式2:返回 null,消息将重试
        return null;
        //方式3:直接抛出异常, 消息将重试
        throw new RuntimeException("Consumer Message exceotion");
    }
}

After the consumption fails, do not retry the configuration mode. In the
cluster consumption mode, the message is expected not to be retried after the message fails. It is necessary to capture the exceptions that may be thrown in the consumption logic, and finally return to Action.CommitMessage. This message will not be retried afterwards. .

public class MessageListenerImpl implements MessageListener {
    
    
    @Override
    public Action consume(Message message, ConsumeContext context) {
    
    
        try {
    
    
            doConsumeMessage(message);
        } catch (Throwable e) {
    
    
            //捕获消费逻辑中的所有异常,并返回 Action.CommitMessage;
            return Action.CommitMessage;
        }
        //消息处理正常,直接返回 Action.CommitMessage;
        return Action.CommitMessage;
    }
}

Customize the maximum number of retries for
messages The RocketMQ message queue allows the maximum number of retries to be set when the Consumer starts. The retry interval will follow the following strategy:

  • The maximum number of retries is less than or equal to 16, and the retry interval is the same as described in the above table.
  • The maximum number of retries is greater than 16 times, and the retry interval for more than 16 times is 2 hours each time.
Properties properties = new Properties();
//配置对应 Group ID 的最大消息重试次数为 20 次
properties.put(PropertyKeyConst.MaxReconsumeTimes,"20");
Consumer consumer =ONSFactory.createConsumer(properties);

note:

  • The setting of the maximum number of message retries is valid for all Consumer instances under the same Group ID.
  • If MaxReconsumeTimes is set for only one of the two Consumer instances under the same Group ID, the configuration will take effect for both Consumer instances.
  • The configuration takes effect by overwriting, that is, the last consumer instance started will overwrite the configuration of the previous startup instance

Get the number of message retries
After receiving the message, the consumer can get the number of message retries as follows:

public class MessageListenerImpl implements MessageListener {
    
    
    @Override
    public Action consume(Message message, ConsumeContext context) {
    
    
        //获取消息的重试次数
        System.out.println(message.getReconsumeTimes());
        return Action.CommitMessage;
    }
}

Guess you like

Origin blog.csdn.net/CSDN877425287/article/details/112786467