rocketmq-product-send方法源码分析

先看有哪些send方法

在这里插入图片描述

首先说红圈的

有3个红圈。归类成3种发送方式。假设前提条件,发送的topic,有3个broker,每个broker总共4个write队列,总共有12个队列。

  • 普通发送。负载均衡12个队列。指定超时时间
  • 指定MessageQueue,发送,指定超时时间
  • 指定selector器,指定特定参数,指定超时时间。一般用于局部有序,比如相同userId的,到同一个队列

默认超时时间时3秒

再说蓝圈

  • sendDefaultImpl 负载均衡的方式,选择队列。然后调sendKernelImpl
  • sendSelectImpl 指定队列selector和arg的方式,选择队列。然后调sendKernelImpl
  • sendKernelImpl 最核心的方式。这里已经明确队列,做真实的消息发送

很明显,只需要简单解读sendDefaultImpl和sendSelectImpl如何选择队列。然后重点在于查看sendKernelImpl方法实现

sendDefaultImpl选择队列分析

先看源码

private SendResult sendDefaultImpl(
      Message msg,
      final CommunicationMode communicationMode,
      final SendCallback sendCallback,
      final long timeout
  ) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
   
   
      this.makeSureStateOK();
      Validators.checkMessage(msg, this.defaultMQProducer);
      final long invokeID = random.nextLong();
      long beginTimestampFirst = System.currentTimeMillis();
      long beginTimestampPrev = beginTimestampFirst;
      long endTimestamp = beginTimestampFirst;
      TopicPublishInfo topicPublishInfo = this.tryToFindTopicPublishInfo(msg.getTopic());
      if (topicPublishInfo != null && topicPublishInfo.ok()) {
   
   
          boolean callTimeout = false;
          MessageQueue mq = null;
          Exception exception = null;
          SendResult sendResult = null;
          int timesTotal = communicationMode == CommunicationMode.SYNC ? 1 + this.defaultMQProducer.getRetryTimesWhenSendFailed() : 1;
          int times = 0;
          String[] brokersSent = new String[timesTotal];
          for (; times < timesTotal; times++) {
   
   
              String lastBrokerName = null == mq ? null : mq.getBrokerName();
              MessageQueue mqSelected = this.selectOneMessageQueue(topicPublishInfo, lastBrokerName);
              if (mqSelected != null) {
   
   
                  mq = mqSelected;
                  brokersSent[times] = mq.getBrokerName();
                  try {
   
   
                      beginTimestampPrev = System.currentTimeMillis();
                      if (times > 0) {
   
   
                          //Reset topic with namespace during resend.
                          msg.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值