什么场景下使用重试队列
消费端一直不回传消费结果,MQ认为消息没有收到,Consumer下一次拉取,Broker依然会发送该消息,所以,任何异常都要返回ConsumeConcurrentlyStatus.RECONSUME_LATER,这样MQ会将消息放到重试队列;
重试队列名称为:%RETRY%+consumergroup
配置
Broker配置如下:
messageDelayLevel=5s 5s 5s 5s 5s 5s 5s 5s 5s 5s 5s 5s 5s 5s 5s 5s 5s 5s(测试使用)
默认配置为:messageDelayLevel=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
测试
测试Demo
public static void main(String[] args) throws InterruptedException, MQClientException {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("RetryConsumer");
consumer.setNamesrvAddr(ExampleConstant.NAMESERVER);
consumer.setMessageModel(org.apache.rocketmq.common.protocol.heartbeat.MessageModel.CLUSTERING);
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
consumer.subscribe("TopicTest", "");
consumer.setMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
ConsumeConcurrentlyContext context) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.printf(df.format(new Date()) + ", %s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
return ConsumeConcurrentlyStatus.RECONSUME_LATER;
}
});
consumer.start();
}
测试结果
死信队列
消息在重试队列消费16次(默认次数)后,进入死信队列,名为:%DLQ%+consumergroup