RocketMQ分析 - 生产端消息重试机制

1.可以通过 producer.setRetryTimesWhenSendFailed(count) 来设置生产者发送消息时候失败重试的次数,默认值是2,即失败一次后,会重试两次,总共发送三次消息

# com.alibaba.rocketmq.client.producer.DefaultMQProducer
private int retryTimesWhenSendFailed = 2;

2.生产者发送消息重试机制

贴上源码,客户端版本号是:3.2.6

# com.alibaba.rocketmq.client.impl.producer.DefaultMQProducerImpl#sendDefaultImpl
int timesTotal = 1 + this.defaultMQProducer.getRetryTimesWhenSendFailed();
int times = 0;
String[] brokersSent = new String[timesTotal];
for (; times < timesTotal && (endTimestamp - beginTimestamp) < maxTimeout; times++) {
    String lastBrokerName = null == mq ? null : mq.getBrokerName();
    MessageQueue tmpmq = topicPublishInfo.selectOneMessageQueue(lastBrokerName);
    if (tmpmq != null) {
        mq = tmpmq;
        brokersSent[times] = mq.getBrokerName();
        try {
            sendResult = this.sendKernelImpl(msg, mq, communicationMode, sendCallback, timeout);
            endTimestamp = System.currentTimeMillis();
            switch (communicationMode) {
            case ASYNC:
                return null;
            case ONEWAY:
                return null;
            case SYNC:
                if (sendResult.getSendStatus() != SendStatus.SEND_OK) {
                    if (this.defaultMQProducer.isRetryAnotherBrokerWhenNotStoreOK()) {
                        continue;
                    }
                }

                return sendResult;
            default:
                break;
            }
        }
        catch (Exception e) {
            endTimestamp = System.currentTimeMillis();
            continue;
        }
    }
    else {
        break;
    }
} // end of
posted @ 2019-10-29 19:59  snower1995  阅读(998)  评论(0编辑  收藏  举报