rocketmq transaction-事务回查

文章基于rocket-mq4.3 代码分析

在broker的 initialize() 方法最后的地方会设置事务消息回查相关的服务类

 

private void initialTransaction() {
        this.transactionalMessageService = ServiceProvider.loadClass(ServiceProvider.TRANSACTION_SERVICE_ID, TransactionalMessageService.class);
        if (null == this.transactionalMessageService) {
            this.transactionalMessageService = new TransactionalMessageServiceImpl(new TransactionalMessageBridge(this, this.getMessageStore()));
            log.warn("Load default transaction message hook service: {}", TransactionalMessageServiceImpl.class.getSimpleName());
        }
        this.transactionalMessageCheckListener = ServiceProvider.loadClass(ServiceProvider.TRANSACTION_LISTENER_ID, AbstractTransactionalMessageCheckListener.class);
        if (null == this.transactionalMessageCheckListener) {
            this.transactionalMessageCheckListener = new DefaultTransactionalMessageCheckListener();
            log.warn("Load default discard message hook service: {}", DefaultTransactionalMessageCheckListener.class.getSimpleName());
        }
        this.transactionalMessageCheckListener.setBrokerController(this);
        this.transactionalMessageCheckService = new TransactionalMessageCheckService(this);
    }

TransactionalMessageCheckService是一个线程类,最终会不断执行 onWaitEnd() 方法

@Override
    protected void onWaitEnd() {
        long timeout = brokerController.getBrokerConfig().getTransactionTimeOut();
        int checkMax = brokerController.getBrokerConfig().getTransactionCheckMax();
        long begin = System.currentTimeMillis();
        log.info("Begin to check prepare message, begin time:{}", begin);
        this.brokerController.getTransactionalMessageService().check(timeout, checkMax, this.brokerController.getTransactionalMessageCheckListener());
        log.info("End to check prepare message, consumed time:{}", System.currentTimeMillis() - begin);
    }

即最终执行的是:org.apache.rocketmq.broker.transaction.queue.TransactionalMessageServiceImpl#check 方法

该方法会不断的通过固定的topic  MixAll.RMQ_SYS_TRANS_HALF_TOPIC 组装对应的 MessageQueue集合(根据该topic对应的配置 topicConfig.getReadQueueNums | 其实我对rocketmq topic配置信息里为什么把readQueueNum和writeQueueNum分开,而不使用同一个数字感到不解),然后做事务回逻辑;

 

 

你可能感兴趣的:(RocketMq)