Retry mechanism
Because MQ often in complex distributed systems, regardless of network fluctuations, service downtime, program abnormal factors, the failure to send a message or consumer issues likely to arise. Therefore, the retry is all MQ middleware must take into account a key point of the message. If there is no message retry, message loss may arise problems that may have a significant impact on the system. So, Bing Chengning be more than a message, the message can not be lost in principle, most of all MQ message retry provide good support.
Encapsulating the message to the user RocketMQ retry processing flow, the developer without manual handling. RocketMQ support the retry mechanism for the production side and the consumer side categories.
Simulating production retry end
Consumer news consumption end of two states:
package com.alibaba.rocketmq.client.consumer.listener;
public enum ConsumeConcurrentlyStatus {
CONSUME_SUCCESS,
RECONSUME_LATER;
private ConsumeConcurrentlyStatus() {
}
}
It is a success (CONSUME_SUCCESS), & retry a failed (RECONSUME_LATER);
Consumer consumption in order to ensure the success of the message, only to use side made it clear that the consumer succeeds, the return CONSUME_SUCCESS, RocketMQ would think the message consumer success.
If the message consumer fails, simply return ConsumeConcurrentlyStatus.RECONSUME_LATER, RocketMQ consumer will think that the message has failed, need to re-delivery.
1. abnormal
package com.wn.consumer;
import com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import com.alibaba.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.common.message.MessageExt;
import java.util.List;
public class MQConsumer {
public static void main(String[] args) throws MQClientException {
//创建消费者
DefaultMQPushConsumer consumer=new new DefaultMQPushConsumer ( " RMQ-Group " );
// set NameServer address
consumer.setNamesrvAddr ( " 192.168.138.187:9876;192.168.138.188:9876 " );
// Set the name of the consumer instance
consumer.setInstanceName ( " Consumer " );
// Subscribe Topic
Consumer.subscribe ( " WN02 " , " TagA " );
// listen for messages
consumer.registerMessageListener ( new new MessageListenerConcurrently () {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
//获取消息
for (MessageExt msg:list){
System.out.println(msg.getMsgId()+"---"+new String(msg.getBody()));
}
try {
int i=1/0;
}catch (Exception e){
e.printStackTrace();
//需要重试
return ConsumeConcurrentlyStatus.RECONSUME_LATER;
}
//消息成功
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer.start();
System.out.println("Consumer Started...");
}
}
2. network latency
package com.wn.consumer;
import com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import com.alibaba.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.common.message.MessageExt;
import java.util.List;
public class MQConsumer {
public static void main(String[] args) throws MQClientException {
//创建消费者
DefaultMQPushConsumer consumer=new new DefaultMQPushConsumer ( " RMQ-Group " );
// set NameServer address
consumer.setNamesrvAddr ( " 192.168.138.187:9876;192.168.138.188:9876 " );
// Set the name of the consumer instance
consumer.setInstanceName ( " Consumer " );
// Subscribe Topic
Consumer.subscribe ( " wn03 " , " TagA " );
// listen for messages
consumer.registerMessageListener ( new new MessageListenerConcurrently () {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
//获取消息
for (MessageExt msg:list){
System.out.println(msg.getMsgId()+"---"+new String(msg.getBody()));
}
//网络延迟
try {
Thread.sleep(600000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//消息成功
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer.start();
System.out.println("Consumer Started...");
}
}