• 05 RocketMQ - Consumer 源码分析


    Consumer 启动流程

    在这里插入图片描述

    DefaultMQPushConsumer::start()

    1. 组装 consumerGroup
    2. 启动 consumer
    3. 启动并发处理队列任务
    public void start() throws MQClientException {
            //1.组装 consumerGroup
            setConsumerGroup(NamespaceUtil.wrapNamespace(this.getNamespace(), this.consumerGroup));
            //2.启动 consumer
            this.defaultMQPushConsumerImpl.start();
            //3.启动并发处理队列任务
            if (null != traceDispatcher) {
                try {
                    traceDispatcher.start(this.getNamesrvAddr(), this.getAccessChannel());
                } catch (MQClientException e) {
                    log.warn("acl dispatcher start failed ", e);
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    DefaultMQPushConsumerImpl::start()

    1. 检查配置信息
    2. 加工订阅信息(同时,如果消息消费模式为集群模式,还需要为该消费组创建一个重试主题。)
    3. 创建或获得MQ客户端实例
    4. 负载均衡(队列默认分配算法)
    5. 封装拉取消息的API包装类(主要有消息的拉取方法和接受拉取到的消息)
    6. 设置消费进度存储实现类(如果是集群模式,使用远程存储RemoteBrokerOffsetStore,如果是广播模式,则使用本地存储LocalFileOffsetStore)
    7. 加载消息进度
    8. 设置顺序消息服务或者并发消息服务
    9. 消息消费服务启动
    10. 注册消费者
    11. MQClientInstance启动
    12. 更新TopicRouteData
    13. 检测broker状态
    14. 发送心跳
    15. 重新负载
    public synchronized void start() throws MQClientException {
            switch (this.serviceState) {
                case CREATE_JUST:
                    log.info("the consumer [{}] start beginning. messageModel={}, isUnitMode={}", this.defaultMQPushConsumer.getConsumerGroup(),
                        this.defaultMQPushConsumer.getMessageModel(), this.defaultMQPushConsumer.isUnitMode());
                    this.serviceState = ServiceState.START_FAILED;
                    //1.检查配置信息
                    this.checkConfig();
                    //2.加工订阅信息(同时,如果消息消费模式为集群模式,还需要为该消费组创建一个重试主题。)
                    this.copySubscription();
    
                    if (this.defaultMQPushConsumer.getMessageModel() == MessageModel.CLUSTERING) {
                        this.defaultMQPushConsumer.changeInstanceNameToPID();
                    }
                    //3.创建或获得MQ客户端实例
                    // IP@instanceName@unitName 相同则为同一实例
                    this.mQClientFactory = MQClientManager.getInstance().getOrCreateMQClientInstance(this.defaultMQPushConsumer, this.rpcHook);
                    //4.负载均衡(队列默认分配算法)
                    this.rebalanceImpl.setConsumerGroup(this.defaultMQPushConsumer.getConsumerGroup());
                    this.rebalanceImpl.setMessageModel(this.defaultMQPushConsumer.getMessageModel());
                    //集群模式下不设置默认为 AllocateMessageQueueAveragely(平均分配消息队列)
                    this.rebalanceImpl.setAllocateMessageQueueStrategy(this.defaultMQPushConsumer.getAllocateMessageQueueStrategy());
                    this.rebalanceImpl.setmQClientFactory(this.mQClientFactory);
                    //5.封装拉取消息的API包装类(主要有消息的拉取方法和接受拉取到的消息)
                    this.pullAPIWrapper = new PullAPIWrapper(
                        mQClientFactory,
                        this.defaultMQPushConsumer.getConsumerGroup(), isUnitMode());
                    this.pullAPIWrapper.registerFilterMessageHook(filterMessageHookList);
                    //6.设置消费进度存储实现类(如果是集群模式,使用远程存储RemoteBrokerOffsetStore,如果是广播模式,则使用本地存储LocalFileOffsetStore)
                    if (this.defaultMQPushConsumer.getOffsetStore() != null) {
                        this.offsetStore = this.defaultMQPushConsumer.getOffsetStore();
                    } else {
                        switch (this.defaultMQPushConsumer.getMessageModel()) {
                            case BROADCASTING:
                                this.offsetStore = new LocalFileOffsetStore(this.mQClientFactory, this.defaultMQPushConsumer.getConsumerGroup());
                                break;
                            case CLUSTERING:
                                this.offsetStore = new RemoteBrokerOffsetStore(this.mQClientFactory, this.defaultMQPushConsumer.getConsumerGroup());
                                break;
                            default:
                                break;
                        }
                        this.defaultMQPushConsumer.setOffsetStore(this.offsetStore);
                    }
                    //7.加载消息进度(offsetStore是用来操作消费进度的对象)
                    //push模式消费进度最后持久化在broker端,但是consumer端在内存中也持有消费进度
                    this.offsetStore.load();
                    //8.设置顺序消息服务或者并发消息服务
                    if (this.getMessageListenerInner() instanceof MessageListenerOrderly) {
                        this.consumeOrderly = true;
                        this.consumeMessageService =
                            new ConsumeMessageOrderlyService(this, (MessageListenerOrderly) this.getMessageListenerInner());
                    } else if (this.getMessageListenerInner() instanceof MessageListenerConcurrently) {
                        this.consumeOrderly = false;
                        this.consumeMessageService =
                            new ConsumeMessageConcurrentlyService(this, (MessageListenerConcurrently) this.getMessageListenerInner());
                    }
                    //9.消息消费服务启动
                    this.consumeMessageService.start();
                    //10.注册消费者
                    boolean registerOK = mQClientFactory.registerConsumer(this.defaultMQPushConsumer.getConsumerGroup(), this);
                    if (!registerOK) {
                        this.serviceState = ServiceState.CREATE_JUST;
                        this.consumeMessageService.shutdown(defaultMQPushConsumer.getAwaitTerminationMillisWhenShutdown());
                        throw new MQClientException("The consumer group[" + this.defaultMQPushConsumer.getConsumerGroup()
                            + "] has been created before, specify another name please." + FAQUrl.suggestTodo(FAQUrl.GROUP_NAME_DUPLICATE_URL),
                            null);
                    }
                    //11.MQClientInstance启动
                    mQClientFactory.start();
                    log.info("the consumer [{}] start OK.", this.defaultMQPushConsumer.getConsumerGroup());
                    this.serviceState = ServiceState.RUNNING;
                    break;
                case RUNNING:
                case START_FAILED:
                case SHUTDOWN_ALREADY:
                    throw new MQClientException("The PushConsumer service state not OK, maybe started once, "
                        + this.serviceState
                        + FAQUrl.suggestTodo(FAQUrl.CLIENT_SERVICE_NOT_OK),
                        null);
                default:
                    break;
            }
            //12.更新TopicRouteData
            this.updateTopicSubscribeInfoWhenSubscriptionChanged();
            //13.检测broker状态
            this.mQClientFactory.checkClientInBroker();
            //14.发送心跳
            this.mQClientFactory.sendHeartbeatToAllBrokerWithLock();
            //15.重新负载
            this.mQClientFactory.rebalanceImmediately();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92

    ConsumeMessageService::start

    在这里插入图片描述
    存在顺序消费并发消费两个实现类

    ConsumeMessageConcurrentlyService::start()

    并发消费

    public void start() {
            //定时任务清除过期的消息
            this.cleanExpireMsgExecutors.scheduleAtFixedRate(new Runnable() {
    
                @Override
                public void run() {
                    cleanExpireMsg();
                }
    
            }, this.defaultMQPushConsumer.getConsumeTimeout(), this.defaultMQPushConsumer.getConsumeTimeout(), TimeUnit.MINUTES);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    ConsumeMessageOrderlyService::start()

    顺序消费

    public void start() {
            //加锁确保一个队列同时只能被一个消费者消费,保证消息的顺序性
            //锁有效期60s,每20s去续锁
            if (MessageModel.CLUSTERING.equals(ConsumeMessageOrderlyService.this.defaultMQPushConsumerImpl.messageModel())) {
                this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
                    @Override
                    public void run() {
                        ConsumeMessageOrderlyService.this.lockMQPeriodically();
                    }
                }, 1000 * 1, ProcessQueue.REBALANCE_LOCK_INTERVAL, TimeUnit.MILLISECONDS);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    Consumer 消息消费

    消费负载均衡

    • 集群模式
      在集群模式下,消息只需要投递到订阅这个topic的Consumer Group下的一个实例即可。RocketMQ采用主动拉取的方式拉取并消费消息,在拉取的时候需要明确指定拉取哪一条message queue。

      而每当实例的数量有变更,都会触发一次所有实例的负载均衡,这时候会按照queue的数量和实例的数量平均分配queue给每个实例。

      默认的分配算法是AllocateMessageQueueAveragely(平均分配)
      还有另外一种平均的算法是AllocateMessageQueueAveragelyByCircle(环形分配策略),也是平均分摊每一条queue,只是以环状轮流分queue的形式
      image.png
      需要注意的是,集群模式下,queue都是只允许分配只一个实例,这是由于如果多个实例同时消费一个queue的消息,由于拉取哪些消息是consumer主动控制的,那样会导致同一个消息在不同的实例下被消费多次,所以算法上都是一个queue只分给一个consumer实例,一个consumer实例可以允许同时分到不同的queue

    平均分配策略(默认)(AllocateMessageQueueAveragely)
    环形分配策略(AllocateMessageQueueAveragelyByCircle)
    手动配置分配策略(AllocateMessageQueueByConfig)
    机房分配策略(AllocateMessageQueueByMachineRoom)
    一致性哈希分配策略(AllocateMessageQueueConsistentHash)
    靠近机房策略(AllocateMachineRoomNearby)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 广播模式
      由于广播模式下要求一条消息需要投递到一个消费组下面所有的消费者实例,所以也就没有消息被分摊消费的说法。

      在实现上,其中一个不同就是在consumer分配queue的时候,所有consumer都分到所有的queue

    消费流程

    image.png
    顺序消费 的流程和并发消费流程整体差不多,唯一的多的就是使用锁机制(对队列上锁)来确保一个队列同时只能被一个消费者消费,从而确保消费的顺序性

    1. 获取topic配置信息

    定时任务每30s获取更新Topic相关信息

    //30s一次修改路由信息
            this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        MQClientInstance.this.updateTopicRouteInfoFromNameServer();
                    } catch (Exception e) {
                        log.error("ScheduledTask updateTopicRouteInfoFromNameServer exception", e);
                    }
                }
            }, 10, this.clientConfig.getPollNameServerInterval(), TimeUnit.MILLISECONDS);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    2. 获取Group的ConsumerList

    集群模式下获取指定消费组下的所有消费者

    3. 分配Queue

    集群模式下分配消费者对应的Queue

    4. 获取Queue的消费offset
    • 集群模式
      消费进度存储会持久化到Broker
    • 广播模式
      消费进度会存储持久化到实例本地
      在这里插入图片描述
      上述 2,3,4均在 rebalanceService.start() 中实现(在 MQClientInstance 启动时就会执行),下面看下 rebalanceService::run()
    private static long waitInterval =
            Long.parseLong(System.getProperty(
                "rocketmq.client.rebalance.waitInterval", "20000"));
    public void run() {
            log.info(this.getServiceName() + " service started");
    
            while (!this.isStopped()) {
                this.waitForRunning(waitInterval);
                this.mqClientFactory.doRebalance();
            }
    
            log.info(this.getServiceName() + " service end");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    跳转到重点方法 RebalanceImpl::rebalanceByTopic()

    private void  rebalanceByTopic(final String topic, final boolean isOrder) {
            switch (messageModel) {
                // 广播模式(消费进度会存储持久化到实例本地)
                case BROADCASTING: {
                    // 1.获取 topic 下的 brokerName 与 queueId
                    Set<MessageQueue> mqSet = this.topicSubscribeInfoTable.get(topic);
                    if (mqSet != null) {
                        //2.获取offset,更新 ProcessQueueTable (每个consumer通过balanced维护processQueueTable)
                        boolean changed = this.updateProcessQueueTableInRebalance(topic, mqSet, isOrder);
                        if (changed) {
                            //3.通知队列改变
                            this.messageQueueChanged(topic, mqSet, mqSet);
                            log.info("messageQueueChanged {} {} {} {}",
                                consumerGroup,
                                topic,
                                mqSet,
                                mqSet);
                        }
                    } else {
                        log.warn("doRebalance, {}, but the topic[{}] not exist.", consumerGroup, topic);
                    }
                    break;
                }
                // 集群模式(消费进度存储会持久化到Broker)
                case CLUSTERING: {
                    // 1.获取 topic 下的 brokerName 与 queueId
                    Set<MessageQueue> mqSet = this.topicSubscribeInfoTable.get(topic);
                    // 2.获取Group的ConsumerList
                    List<String> cidAll = this.mQClientFactory.findConsumerIdList(topic, consumerGroup);
                    if (null == mqSet) {
                        if (!topic.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
                            log.warn("doRebalance, {}, but the topic[{}] not exist.", consumerGroup, topic);
                        }
                    }
    
                    if (null == cidAll) {
                        log.warn("doRebalance, {} {}, get consumer id list failed", consumerGroup, topic);
                    }
    
                    if (mqSet != null && cidAll != null) {
                        List<MessageQueue> mqAll = new ArrayList<MessageQueue>();
                        mqAll.addAll(mqSet);
                        //排序
                        Collections.sort(mqAll);
                        Collections.sort(cidAll);
    
                        //3.按负载策略分配消息队列
                        AllocateMessageQueueStrategy strategy = this.allocateMessageQueueStrategy;
    
                        List<MessageQueue> allocateResult = null;
                        try {
                            allocateResult = strategy.allocate(
                                this.consumerGroup,
                                this.mQClientFactory.getClientId(),
                                mqAll,
                                cidAll);
                        } catch (Throwable e) {
                            log.error("AllocateMessageQueueStrategy.allocate Exception. allocateMessageQueueStrategyName={}", strategy.getName(),
                                e);
                            return;
                        }
                        //4.对消息队列去重
                        Set<MessageQueue> allocateResultSet = new HashSet<MessageQueue>();
                        if (allocateResult != null) {
                            allocateResultSet.addAll(allocateResult);
                        }
                        //5.获取分配的消息队列的 offset,更新 ProcessQueueTable (每个consumer通过balanced维护processQueueTable)
                        boolean changed = this.updateProcessQueueTableInRebalance(topic, allocateResultSet, isOrder);
                        if (changed) {
                            //6.通知队列改变
                            log.info(
                                "rebalanced result changed. allocateMessageQueueStrategyName={}, group={}, topic={}, clientId={}, mqAllSize={}, cidAllSize={}, rebalanceResultSize={}, rebalanceResultSet={}",
                                strategy.getName(), consumerGroup, topic, this.mQClientFactory.getClientId(), mqSet.size(), cidAll.size(),
                                allocateResultSet.size(), allocateResultSet);
                            this.messageQueueChanged(topic, mqSet, allocateResultSet);
                        }
                    }
                    break;
                }
                default:
                    break;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

    RebalanceImpl::updateProcessQueueTableInRebalance()获取offset,更新 ProcessQueueTable

    private boolean updateProcessQueueTableInRebalance(final String topic, final Set<MessageQueue> mqSet,
            final boolean isOrder) {
            boolean changed = false;
    
            Iterator<Entry<MessageQueue, ProcessQueue>> it = this.processQueueTable.entrySet().iterator();
    
            // 1.删除processQueue不必要的messageQueue
            /**代码省略*/
    
            List<PullRequest> pullRequestList = new ArrayList<PullRequest>();
            for (MessageQueue mq : mqSet) {
                if (!this.processQueueTable.containsKey(mq)) {
                    if (isOrder && !this.lock(mq)) {
                        log.warn("doRebalance, {}, add a new mq failed, {}, because lock failed", consumerGroup, mq);
                        continue;
                    }
                    //2.从内存中删除这个消费队列的消费进度
                    this.removeDirtyOffset(mq);
                    ProcessQueue pq = new ProcessQueue();
                    //3.获取nextOffset,及更新当前messageQueue对应请求的offset
                    long nextOffset = this.computePullFromWhere(mq);
                    if (nextOffset >= 0) {
                        ProcessQueue pre = this.processQueueTable.putIfAbsent(mq, pq);
                        if (pre != null) {
                            log.info("doRebalance, {}, mq already exists, {}", consumerGroup, mq);
                        } else {
                            log.info("doRebalance, {}, add a new mq, {}", consumerGroup, mq);
                            PullRequest pullRequest = new PullRequest();
                            pullRequest.setConsumerGroup(consumerGroup);
                            pullRequest.setNextOffset(nextOffset);
                            pullRequest.setMessageQueue(mq);
                            pullRequest.setProcessQueue(pq);
                            pullRequestList.add(pullRequest);
                            changed = true;
                        }
                    } else {
                        log.warn("doRebalance, {}, add new mq failed, {}", consumerGroup, mq);
                    }
                }
            }
            //4.转发拉取请求队列
            this.dispatchPullRequest(pullRequestList);
    
            return changed;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    RebalancePushImpl::computePullFromWhere()获取offset

    public long computePullFromWhere(MessageQueue mq) {
            long result = -1;
            // 从哪里开始消费
            final ConsumeFromWhere consumeFromWhere = this.defaultMQPushConsumerImpl.getDefaultMQPushConsumer().getConsumeFromWhere();
            //集群模式 消费进度存储会持久化到Broker
            //广播模式 消费进度会存储持久化到实例本地
            final OffsetStore offsetStore = this.defaultMQPushConsumerImpl.getOffsetStore();
            switch (consumeFromWhere) {
                case CONSUME_FROM_LAST_OFFSET_AND_FROM_MIN_WHEN_BOOT_FIRST:
                case CONSUME_FROM_MIN_OFFSET:
                case CONSUME_FROM_MAX_OFFSET:
                case CONSUME_FROM_LAST_OFFSET: {
                    long lastOffset = offsetStore.readOffset(mq, ReadOffsetType.READ_FROM_STORE);
                    if (lastOffset >= 0) {
                        result = lastOffset;
                    }
                    // First start,no offset
                    else if (-1 == lastOffset) {
                        if (mq.getTopic().startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
                            result = 0L;
                        } else {
                            try {
                                result = this.mQClientFactory.getMQAdminImpl().maxOffset(mq);
                            } catch (MQClientException e) {
                                result = -1;
                            }
                        }
                    } else {
                        result = -1;
                    }
                    break;
                }
                case CONSUME_FROM_FIRST_OFFSET: {
                    long lastOffset = offsetStore.readOffset(mq, ReadOffsetType.READ_FROM_STORE);
                    if (lastOffset >= 0) {
                        result = lastOffset;
                    } else if (-1 == lastOffset) {
                        result = 0L;
                    } else {
                        result = -1;
                    }
                    break;
                }
                case CONSUME_FROM_TIMESTAMP: {
                    long lastOffset = offsetStore.readOffset(mq, ReadOffsetType.READ_FROM_STORE);
                    if (lastOffset >= 0) {
                        result = lastOffset;
                    } else if (-1 == lastOffset) {
                        if (mq.getTopic().startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
                            try {
                                result = this.mQClientFactory.getMQAdminImpl().maxOffset(mq);
                            } catch (MQClientException e) {
                                result = -1;
                            }
                        } else {
                            try {
                                long timestamp = UtilAll.parseDate(this.defaultMQPushConsumerImpl.getDefaultMQPushConsumer().getConsumeTimestamp(),
                                    UtilAll.YYYYMMDDHHMMSS).getTime();
                                result = this.mQClientFactory.getMQAdminImpl().searchOffset(mq, timestamp);
                            } catch (MQClientException e) {
                                result = -1;
                            }
                        }
                    } else {
                        result = -1;
                    }
                    break;
                }
    
                default:
                    break;
            }
    
            return result;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    5. 加锁Queue(顺序消费)

    加锁确保一个队列同时只能被一个消费者消费,保证消息的顺序性,锁有效期60s,每20s去续锁
    RebalanceImpl::lockAll()

    6. 拉取Queue的消息

    开启一个线程去拉取消息

    public void run() {
            log.info(this.getServiceName() + " service started");
    
            while (!this.isStopped()) {
                try {
                    PullRequest pullRequest = this.pullRequestQueue.take();
                    this.pullMessage(pullRequest);
                } catch (InterruptedException ignored) {
                } catch (Exception e) {
                    log.error("Pull Message Service Run Method exception", e);
                }
            }
    
            log.info(this.getServiceName() + " service end");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    7. 消费消息

    拿到消息开始消费

    8. 更新Queue的消费offset

    定时任务每5s持久化消息消费进度

    //5s一次持久化消费进度
            this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        MQClientInstance.this.persistAllConsumerOffset();
                    } catch (Exception e) {
                        log.error("ScheduledTask persistAllConsumerOffset exception", e);
                    }
                }
            }, 1000 * 10, this.clientConfig.getPersistConsumerOffsetInterval(), TimeUnit.MILLISECONDS);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    9. 注销Consumer
    public synchronized void shutdown(long awaitTerminateMillis) {
            switch (this.serviceState) {
                case CREATE_JUST:
                    break;
                case RUNNING:
                    this.consumeMessageService.shutdown(awaitTerminateMillis);
                    this.persistConsumerOffset();
                    this.mQClientFactory.unregisterConsumer(this.defaultMQPushConsumer.getConsumerGroup());
                    this.mQClientFactory.shutdown();
                    log.info("the consumer [{}] shutdown OK", this.defaultMQPushConsumer.getConsumerGroup());
                    this.rebalanceImpl.destroy();
                    this.serviceState = ServiceState.SHUTDOWN_ALREADY;
                    break;
                case SHUTDOWN_ALREADY:
                    break;
                default:
                    break;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    10. 解锁Queue(顺序消费)

    注销 Consumer 后会解锁 Queue
    RebalanceImpl::unlockAll()

  • 相关阅读:
    一张表实现类某音的视频"评论回复"功能
    无需编写任何代码即可使用 AI 创建新漫画,关于如何使用 GPT-3 和 DALL-E 为有趣页面生成原创内容的教程
    2023/9/8 -- C++/QT
    Python 操作MySql数据库(封装、优雅)
    [附源码]java毕业设计本科毕业设计过程管理系统
    虚拟机中磁盘空间查询
    centOs 6.10 编译 qt 5.15.11
    Java(四)(多态,final,常量,抽象类,接口)
    ARM作业,M4
    Node之Express学习笔记
  • 原文地址:https://blog.csdn.net/qq_33512765/article/details/126766777