6-rocketmq send message

Time:2021-7-22
public static void main(String[] args) throws Exception {
    // Instantiate with a producer group name.
    DefaultMQProducer producer = new
        DefaultMQProducer("GroupNameDemo");
    // Specify name server addresses.
    producer.setNamesrvAddr("localhost:9876");
    //Launch the instance.
    producer.start();
    for (int i = 0; i < 100; i++) {
        //Create a message instance, specifying topic, tag and message body.
        Message msg = new Message("TopicDemo" /* Topic */,
                                  "TagA" /* Tag */,
                                  ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
                                 );
        // Call send message to deliver message to one of brokers.
        SendResult sendResult = producer.send(msg);
        System.out.printf("%s%n", sendResult);
    }
    //Shut down once the producer instance is not longer in use.
    producer.shutdown();
}

The basic process of message sending

Message sending entryDefaultMQProducer#send()

// DefaultMQProducer.java

Message verification

  1. Check message content cannot be empty
  2. Check message length cannot be 0
  3. By default, the message size cannot exceed 4m
// Validators.java

message sending

// DefaultMQProducerImpl.java

Query topic routing information

If the producer has cached the routing information of topic, it will return it directly. If there is no cache, query nameserver for the routing information of the topic.

If the routing information is not found, an exception will be thrown directly.

// DefaultMQProducerImpl.java

TopicPublishInfoThe information is as follows

public class TopicPublishInfo {

The details are as follows

{
    "haveTopicRouterInfo": true,
    "messageQueueList": [
        {
            "brokerName": "PQSZ-L0039",
            "queueId": 0,
            "topic": "TopicDemo"
        },
        {
            "brokerName": "PQSZ-L0039",
            "queueId": 1,
            "topic": "TopicDemo"
        },
        {
            "brokerName": "PQSZ-L0039",
            "queueId": 2,
            "topic": "TopicDemo"
        },
        {
            "brokerName": "PQSZ-L0039",
            "queueId": 3,
            "topic": "TopicDemo"
        }
    ],
    "orderTopic": false,
    "sendWhichQueue": {
        "andIncrement": 1497938501
    },
    "topicRouteData": {
        "brokerDatas": [
            {
                "brokerAddrs": {
                    "0": "10.178.42.122:10911"
                },
                "brokerName": "PQSZ-L0039",
                "cluster": "DefaultCluster"
            }
        ],
        "filterServerTable": {},
        "queueDatas": [
            {
                "brokerName": "PQSZ-L0039",
                "perm": 6,
                "readQueueNums": 4,
                "topicSynFlag": 0,
                "writeQueueNums": 4
            }
        ]
    }
}

Select message queue to send message

Select message queue based on topic routing information

// DefaultMQProducerImpl.java

It can be found from the above code that the message will be called if it is sent successfully or failedMQFaultStrategy#updateFaultItemIf the broker failure delay mechanism is enabled, the code in it will be executed.

/**

There are two ways to select message queues.

1) Sendlatencyfaultenable = false. Broker failure delay mechanism is not enabled by default.

2) Sendlatencyfaultenable = true to enable the broker failure delay mechanism.

When the broker failure delay mechanism is not enabled, the method of selecting queue is inTopicPublishInfo#selectOneMessageQueue

public MessageQueue selectOneMessageQueue(final String lastBrokerName) {

Enable the broker failure delay mechanism, select the message queue entry in theMQFaultStrategy#selectOneMessageQueue

public MessageQueue selectOneMessageQueue(final TopicPublishInfo tpInfo, final String lastBrokerName) {

summary

Message queue load mechanism: when message producers send messages, if the routing information of topic is not cached in the local routing table, they send a request to get routing information to nameserver to update the local routing information table, and the message producers update the routing table from nameserver every 30s.

Message sending exception mechanism: message sending is highly available mainly through two means: retrial and broker evasion. Broker circumvention is to find errors in a message sending process. In a certain period of time, the message producer will not select the message queue on the broker (message server) to improve the success rate of sending messages.

This work adoptsCC agreementReprint must indicate the author and the link of this article

Recommended Today

Pit and summary in the development of Android widget

@ catalogue summary Official reference design sketch AndroidManifest.xml Receiver Service Options res/xml/ widget_desktop_options.xml Common parameters Size updatePeriodMillis initialLayout Using App Widgets with Collections Official reference AppWidgetProvider RemoteViewsService RemoteViewsFactory DesktopActivity summary Official reference Build an App Widget design sketch Put a rendering. This is the desktop app folder I play AndroidManifest.xml Receiver Remember not to make […]