[RocketMQ] Reasons why messages are sent repeatedly

First, let’s talk about the reasons why messages are sent repeatedly:

  1. Messages are sent abnormally and repeatedly
  2. Consuming messages throws an exception
  3. heavy balance
  4. Clean up messages that have been consumed for a long time
  5. Consumer submissionoffsetFailed
  6. Master-slave synchronizationoffsetFailed
  7. Server-side persistenceoffsetFailed

Before talking about the positive reasons for failure, let’s first look atrocketmqthe basic knowledge of the premise

RocketMQThe basic principle of sending consumer messages (according toFIFO algorithm)

  1. The producer sends the message,
  2. Throughload balancing algorithm, choose to send to one Broker belowtopicQueueQUEUE,
  3. First establish a connection with this queue, and then send the message to this queueQUEUE
  4. If the consumer consumes up to this pointqueue, the message will be consumed!~

A brief explanation:

  • Broker: Server
  • Topic:The name of a type of message collection
  • Queue: It is Topic 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 machineQUEUE 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:

mqSending 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 useRocketMQ, when we are in the mode ofconcurrent consumption of messages, we need :

  1. RealEntrance:MessageListenerConcurrencyly(Purpose :Print news)

  2. Secondly:

    • The consumergetarrivedmsgafter,

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

Insert image description here

Lianjia

Guess you like

Origin blog.csdn.net/weixin_43475992/article/details/133185803