看一下客户端收到消息后的处理:

MQClientAPIImpl#processPullResponse

private PullResult processPullResponse(
final RemotingCommand response) throws MQBrokerException, RemotingCommandException {
PullStatus pullStatus = PullStatus.NO_NEW_MSG;
switch (response.getCode()) {
case ResponseCode.SUCCESS:
pullStatus = PullStatus.FOUND;
break;
case ResponseCode.PULL_NOT_FOUND:
pullStatus = PullStatus.NO_NEW_MSG;
break;
case ResponseCode.PULL_RETRY_IMMEDIATELY:
pullStatus = PullStatus.NO_MATCHED_MSG;
break;
case ResponseCode.PULL_OFFSET_MOVED:
pullStatus = PullStatus.OFFSET_ILLEGAL;
break; default:
throw new MQBrokerException(response.getCode(), response.getRemark());
} PullMessageResponseHeader responseHeader =
(PullMessageResponseHeader) response.decodeCommandCustomHeader(PullMessageResponseHeader.class); return new PullResultExt(pullStatus, responseHeader.getNextBeginOffset(), responseHeader.getMinOffset(),
responseHeader.getMaxOffset(), null, responseHeader.getSuggestWhichBrokerId(), response.getBody());
}

得到PullResult对象后,MQClientAPIImpl#pullMessageAsyncm,回调pullCallback.onSuccess():

 private void pullMessageAsync(
final String addr,
final RemotingCommand request,
final long timeoutMillis,
final PullCallback pullCallback
) throws RemotingException, InterruptedException {
this.remotingClient.invokeAsync(addr, request, timeoutMillis, new InvokeCallback() {
@Override
public void operationComplete(ResponseFuture responseFuture) {
RemotingCommand response = responseFuture.getResponseCommand();
if (response != null) {
try {
PullResult pullResult = MQClientAPIImpl.this.processPullResponse(response);
assert pullResult != null;
pullCallback.onSuccess(pullResult);
} catch (Exception e) {
pullCallback.onException(e);
}
} else {
if (!responseFuture.isSendRequestOK()) {
pullCallback.onException(new MQClientException("send request failed to " + addr + ". Request: " + request, responseFuture.getCause()));
} else if (responseFuture.isTimeout()) {
pullCallback.onException(new MQClientException("wait response from " + addr + " timeout :" + responseFuture.getTimeoutMillis() + "ms" + ". Request: " + request,
responseFuture.getCause()));
} else {
pullCallback.onException(new MQClientException("unknown reason. addr: " + addr + ", timeoutMillis: " + timeoutMillis + ". Request: " + request, responseFuture.getCause()));
}
}
}
});
}

PullCallback#onSuccess:

调用pullAPIWrapper的processPullResult将消息字节属组解码成消息列表填充msgFoundList,并对消息进行消息过滤(TAG模式)。

public class PullResult {
   private final PullStatus pullStatus;//拉取结果
private final long nextBeginOffset;  //下次拉取偏移量

private final long minOffset;  //消息队列最小偏移量
private final long maxOffset;  //消息队列最大偏移量
private List<MessageExt> msgFoundList;//具体拉取的消息列表

回到onSuccess

可以看到这里如果消息列表为空 马上进行了重试,为什么PullStatus.FOUND,msgFoundList还会为空呢?因为在RocketMQ根据TAG消息过滤,在服务端只是验证了TAG的hashcode,在客户端再次对消息进行过滤,故可能出现msgFoundList为空的情况。

else:

将拉取的消息提交到ConsumeMessageService中公消费者消费(异步)。

根据pullInterval参数,等待pullInterval毫秒后将PullRequest对象放入到PullMessageService的pullRequestQueue中,该消息队列的下次拉取即将被激活,达到持续消息拉取,实现准实时消息拉取的效果。

异常码处理:

NO_NEW_MSG和NO_MATCHED_MSG会直接使用服务端矫正后的偏移量进行一次重试。

OFFSET_ILLEGAL:

case OFFSET_ILLEGAL:
log.warn("the pull request offset illegal, {} {}",
pullRequest.toString(), pullResult.toString());
pullRequest.setNextOffset(pullResult.getNextBeginOffset()); pullRequest.getProcessQueue().setDropped(true);//丢弃该消费队列,ProcessQueue中拉取的消息将停止消费
DefaultMQPushConsumerImpl.this.executeTaskLater(new Runnable() {//更新消息的消费进度,并持久化,并将该消息队列从rebalanceImpl
                    
try {
DefaultMQPushConsumerImpl.this.offsetStore.updateOffset(pullRequest.getMessageQueue(),
pullRequest.getNextOffset(), false);

DefaultMQPushConsumerImpl.this.offsetStore.persist(pullRequest.getMessageQueue());

DefaultMQPushConsumerImpl.this.rebalanceImpl.removeProcessQueue(pullRequest.getMessageQueue());

log.warn("fix the pull request offset, {}", pullRequest);
} catch (Throwable e) {
log.error("executeTaskLater Exception", e);
}
}
}, 10000);
break;
 
 

【mq读书笔记】客户端处理消息(回调提交到异步业务线程池,pullRequest重新入队)的更多相关文章

  1. 【mq读书笔记】顺序消息

    注意异常情况导致整个消费无限重试 阻塞消费 mq支持局部消息顺序消费,可以确保同一个消息消费队列中的消息被顺序消费.看下针对顺序消息在整个消费过程中做的调整: 队列负载: DefaultMQPushC ...

  2. 【mq读书笔记】定时消息

    mq不支持任意的时间京都,如果要支持,不可避免的需要在Broker层做消息排序,加上持久化方面的考量,将不可避免地带来巨大的性能消耗,所以rocketMQ只支持特定级别的延迟消息. 在Broker短通 ...

  3. 【mq读书笔记】消息拉取

    疑问:PullRequest何时添加? PullMessageService提供延迟添加与立即添加2种方式 疑问:PullRequest是在什么时候创建的呢? 1.上上图中 PullRequest p ...

  4. 【mq读书笔记】mq消息消费

    消息消费以组的的模式开展: 一个消费组内可以包含多个消费者,每一个消费组可订阅多个主题: 消费组之间有集群模式与广播模式两种消费模式:集群模式-主题下的同一条消息只允许被其中一个消费者消费.广播模式- ...

  5. TJI读书笔记14-闭包与回调

      TJI读书笔记14-闭包与回调 闭包与回调 为什么要使用内部类?内部类继承自某个类或者实现某个接口,内部类的代码可以操作外嵌类的对象. 这不是使用内部类的理由. 那么为什么使用内部类呢? 我觉得如 ...

  6. 【mq读书笔记】消息确认(失败消息,定时队列重新消费)

    接上文的集群模式,监听器返回RECONSUME_LATER,需要将将这些消息发送给Broker延迟消息.如果发送ack消息失败,将延迟5s后提交线程池进行消费. 入口:ConsumeMessageCo ...

  7. 【mq读书笔记】消息拉取长轮训机制(Broker端)

    RocketMQ并没有真正实现推模式,而是消费者主动想消息服务器拉取消息,推模式是循环向消息服务端发送消息拉取请求. 如果消息消费者向RocketMQ发送消息拉取时,消息未到达消费队列: 如果不启用长 ...

  8. 【mq读书笔记】消息消费过程(钩子 失败重试 消费偏移记录)

    在https://www.cnblogs.com/lccsblog/p/12249265.html中,PullMessageService负责对消息队列进行消息拉取,从远端服务器拉取消息后将消息存入P ...

  9. 【mq读书笔记】消息到达唤醒挂起线程检查新消息

    DefaultMessageStore#start 当新消息到达CommitLog是,ReputMessageService线程负责将消息转发给ConsumeQueue,IndexFile,如果Bro ...

随机推荐

  1. Java学习的第三十九天

    1.例3.7 100~200之间全部素数 package bgio; public class cjava { public static void main(String[]args) { int ...

  2. error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build Tools“

    python3 是用 VC++ 14 编译的, python27 是 VC++ 9 编译的, 安装 python3 的包需要编译的也是要 VC++ 14 以上支持的. 可以下载安装这个: 链接:htt ...

  3. 《Clojure编程》笔记 第13章 测试

    目录 背景简述 第13章 测试 13.1 术语 13.2 clojure.test 13.2.1 定义测试的两种方式 13.2.1.1 用deftest宏把测试定义成单独的函数 13.2.1.2 用w ...

  4. 2018noip游记

    2018noip游记 相隔一年多才想起可以弄一篇博客纪念一下我的首次比赛, 以现在的水平回望过去,发现很好玩很有纪念意义, 于是这篇博客诞生了 \(T1\) 当时的我刚学会什么是字符串,但仍然很不熟练 ...

  5. 服务器性能监控神器nmon使用介绍

    介绍 Nmon (Nigel's Monitor)是由IBM 提供.免费监控 AIX 系统与 Linux 系统资源的工具.该工具可将服务器系统资源耗用情况收集起来并输出一个特定的文件,并可利用 exc ...

  6. c# 生成xml的结构 专业代码

    详细看代码把 public ArrayList kepingyi(string names="") { ArrayList list = new ArrayList(); stri ...

  7. 2.while循环

    while循环 #-*- coding: utf-8-*- #指定识别utf-8的字符串 1.while循环以及跳出循环 while True: #无限循环 print('i love pyhon') ...

  8. java.sql.SQLException: Error: Error: could not match input

    impala执行sql,输出后我在控制台粘贴执行OK,奇怪了. java.sql.SQLException: Error: Error: could not match input 原因竟然是myba ...

  9. 经典c程序100例==11--20

    [程序11] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月 后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律为数列1 ...

  10. 问题记录-CoordinatorLayout+WebView使用遇到的问题

    需求背景: 使用CoordinatorLayout+viewpager+tablayout+webview实现首页折叠效果. 使用问题: 在使用过程中首页的页面为原生/h5混合页,在原生页面正常,嵌套 ...