RocketMQ 消费者(三) - Consumer端拉消息 (图解)

RocketMQ 消费者(三) - Consumer端拉消息 (图解)

1. 拉消息服务

与负载均衡服务一样, 消费者端拉消息的入口在 客户端实例中,为 PullMessageService

内部有几个关键点:

  1. 其内部维护了一个 阻塞任务队列

     private final LinkedBlockingQueue<PullRequest> pullRequestQueue = new LinkedBlockingQueue<PullRequest>();
    

    负载均衡服务 最终 构建的 PullReqeuset,都会放入该队列中。

  2. 启动入口

    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");
    }

while 循环判读 拉消息服务的状态, 内部从阻塞队列中获取 拉消息请求任务PullReqeust,获取到后并执行。

2. 拉消息流程

上图为 消费者端 拉消息的 整个流程,其中关键步骤可以简单概括为以下几点:

  1. 校验 消费者端 和 该ProcessQueue队列信息的 状态。
  2. 对ProcessQueue 进行流控限制校验
  3. 创建 PullCallBack 拉消息回调, 接收处理Broker端返回的队列消息信息。
  4. 构建RemotingCommand 远程调用对象,并根据 推荐的 BrokerAddr 地址,向目标Broker发送远程过程调用请求。
posted @ 2022-03-12 13:45  s686编程传  阅读(156)  评论(0编辑  收藏  举报