First, let’s talk about the reasons why messages are sent repeatedly:
- Messages are sent abnormally and repeatedly
- Consuming messages throws an exception
- heavy balance
- Clean up messages that have been consumed for a long time
- Consumer submission
offset
Failed- Master-slave synchronization
offset
Failed- Server-side persistence
offset
Failed
Before talking about the positive reasons for failure, let’s first look atrocketmq
the basic knowledge of the premise
RocketMQ
The basic principle of sending consumer messages (according toFIFO
algorithm)
- The producer sends the message,
- Throughload balancing algorithm, choose to send to one
Broker
belowtopic
QueueQUEUE
,- First establish a connection with this queue, and then send the message to this queue
QUEUE
- If the consumer consumes up to this point
queue
, the message will be consumed!~
A brief explanation:
Broker
: ServerTopic
:The name of a type of message collectionQueue
: It isTopic
the member (queue) in the collection, which stores the message ===> How do you understand it? It is the big head soldier, the one who really does the work and stores the message
The above basic principles are based on normal circumstances, but (haha, I am most afraid of failure~), it is the core of our article!What about abnormal situations?
1. Messages are sent abnormally and repeatedly
Information about newsTransfer super time,响应 super time,< /span>etc.interrupted
At this time, in order to ensure that the message is sent successfully, ourRocketMQ
will retry the message sending operation, usually twice.
//重试次数默认两次,三次只对同步调用生效
private int retryTimesWhenSendFailed=2;
you canmeeting?proceedingheavy test呢?
Select another machine
QUEUE
to send it!ThisRetry operation: The advantage is that it ensures that the message is sent successfully,but a>repeated sending of messagesThe problem will be that
eg:
mq
Sending a message for the first time,But the network speed was very slow at that time, and it happened to pass the timeout period and reached the server. The server accepted and processed it.
However, due to the timeout mechanism, the sender sends the message again, which leads to the problem of duplicate messages.
(Hey guys, does it look like a computerhandshake screen~haha)
2. Exception thrown when consuming messages
When we use
RocketMQ
, when we are in the mode ofconcurrent consumption of messages, we need :
RealEntrance:
MessageListenerConcurrencyly
(Purpose :Print news)Secondly:
The consumer
get
arrivedmsg
after,will call the implementation of
MessageListenerConcurrencyly
,Input the message collection that needs to be consumed(This thing iscore)
msgs
In the above code, when an exception occurs in the message:
status=null
, we will see that status
is set to RECONSUME_LATER
( and will be re-consumed later< a i=4>)
So once an exception is thrown, the message can be consumed repeatedly! Repeatedly consumed! Repeatedly consumed!!! (Say the important thing three times!!!)
You may say? Isn’t this a normal operation? An exception is thrown and the message is not consumed successfully. What should I do if there is no repeated consumption?
Hmm, your question is correct, but (haha, there is atwist!)
Let’s move forwardStop it, when we consume, the server queue we pass in isThe entire collection(The key point is here!!!)
What does this mean?
Of course, the entire message collection is re consumed ( Did you react? No? Let’s continue reading!~)
After the message is processed, whether it is successful or abnormal, the result needs to be processed. The code is as follows: