rocketmq学习笔记 六 流程之拉消息

来源:互联网 发布:手办数据库 编辑:程序博客网 时间:2024/05/19 16:34

其实rocketmq的推消息,也是长轮询的方式去拉去消息


拉消息包括 按id拉消息 按key拉消息 按下标拉消息


其实大同小异


流程图


核心代码

DefaultMQPullConsumerImpl

    private PullResult pullSyncImpl(MessageQueue mq, String subExpression, long offset, int maxNums, boolean block, long timeout)            throws MQClientException, RemotingException, MQBrokerException, InterruptedException {        this.makeSureStateOK();        if (null == mq) {            throw new MQClientException("mq is null", null);        }        if (offset < 0) {            throw new MQClientException("offset < 0", null);        }        if (maxNums <= 0) {            throw new MQClientException("maxNums <= 0", null);        }        this.subscriptionAutomatically(mq.getTopic());        int sysFlag = PullSysFlag.buildSysFlag(false, block, true, false);        SubscriptionData subscriptionData;        try {            subscriptionData = FilterAPI.buildSubscriptionData(this.defaultMQPullConsumer.getConsumerGroup(), //                    mq.getTopic(), subExpression);        } catch (Exception e) {            throw new MQClientException("parse subscription error", e);        }        long timeoutMillis = block ? this.defaultMQPullConsumer.getConsumerTimeoutMillisWhenSuspend() : timeout;        PullResult pullResult = this.pullAPIWrapper.pullKernelImpl(//                mq, // 1                subscriptionData.getSubString(), // 2                0L, // 3                offset, // 4                maxNums, // 5                sysFlag, // 6                0, // 7                this.defaultMQPullConsumer.getBrokerSuspendMaxTimeMillis(), // 8                timeoutMillis, // 9                CommunicationMode.SYNC, // 10                null// 11        );        this.pullAPIWrapper.processPullResult(mq, pullResult, subscriptionData);        if (!this.consumeMessageHookList.isEmpty()) {            ConsumeMessageContext consumeMessageContext = null;            consumeMessageContext = new ConsumeMessageContext();            consumeMessageContext.setConsumerGroup(this.groupName());            consumeMessageContext.setMq(mq);            consumeMessageContext.setMsgList(pullResult.getMsgFoundList());            consumeMessageContext.setSuccess(false);            this.executeHookBefore(consumeMessageContext);            consumeMessageContext.setStatus(ConsumeConcurrentlyStatus.CONSUME_SUCCESS.toString());            consumeMessageContext.setSuccess(true);            this.executeHookAfter(consumeMessageContext);        }        return pullResult;    }



0 0
原创粉丝点击