(十五)处理有异常消息
发布日期:2021-11-18 17:47:36
浏览次数:8
分类:技术文章
本文共 3616 字,大约阅读时间需要 12 分钟。
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
在封装创建request的时候会调用handleProduceResponse(),这个里面会调用
private void completeBatch(RecordBatch batch, Errors error, long baseOffset, long timestamp, long correlationId, long now) { //如果响应里面带有异常 并且 这个请求是可以重试的 if (error != Errors.NONE && canRetry(batch, error)) { // retry log.warn("Got error produce response with correlation id {} on topic-partition {}, retrying ({} attempts left). Error: {}", correlationId, batch.topicPartition, this.retries - batch.attempts - 1, error); //重新把发送失败等着批次 加入到队列里面。 this.accumulator.reenqueue(batch, now); this.sensors.recordRetries(batch.topicPartition.topic(), batch.recordCount); } else { //这儿过来的数据:带有异常,但是不可以重试(1:压根就不让重试2:重试次数超了) //其余的都走这个分支。 RuntimeException exception; //如果响应里面带有 没有权限的异常 if (error == Errors.TOPIC_AUTHORIZATION_FAILED) //自己封装一个异常信息(自定义了异常) exception = new TopicAuthorizationException(batch.topicPartition.topic()); else // exception = error.exception(); // tell the user the result of their request //TODO 核心代码 把异常的信息也给带过去了 //我们刚刚看的就是这儿的代码 //里面调用了用户传进来的回调函数 //回调函数调用了以后 //说明我们的一个完整的消息的发送流程就结束了。 batch.done(baseOffset, timestamp, exception); //看起来这个代码就是要回收资源的。 this.accumulator.deallocate(batch); if (error != Errors.NONE) this.sensors.recordErrors(batch.topicPartition.topic(), batch.recordCount); } if (error.exception() instanceof InvalidMetadataException) { if (error.exception() instanceof UnknownTopicOrPartitionException) log.warn("Received unknown topic or partition error in produce request on partition {}. The " + "topic/partition may not exist or the user may not have Describe access to it", batch.topicPartition); metadata.requestUpdate(); } // Unmute the completed partition. if (guaranteeMessageOrder) this.accumulator.unmutePartition(batch.topicPartition); }
有异常并且可以重试,执行第一个分支,会调用
public void reenqueue(RecordBatch batch, long now) { //重试次数 累加 batch.attempts++; //上一次重试的时间 batch.lastAttemptMs = now; batch.lastAppendTime = now; batch.setRetry(); Dequedeque = getOrCreateDeque(batch.topicPartition); synchronized (deque) { //重新放入到队列里面 //放入到队头 deque.addFirst(batch); } }
有异常但是不可以重试,执行第二个分支,会调用
//这儿过来的数据:带有异常,但是不可以重试(1:压根就不让重试2:重试次数超了) //其余的都走这个分支。 RuntimeException exception; //如果响应里面带有 没有权限的异常 if (error == Errors.TOPIC_AUTHORIZATION_FAILED) //自己封装一个异常信息(自定义了异常) exception = new TopicAuthorizationException(batch.topicPartition.topic()); else // exception = error.exception(); // tell the user the result of their request //TODO 核心代码 把异常的信息也给带过去了 //我们刚刚看的就是这儿的代码 //里面调用了用户传进来的回调函数 //回调函数调用了以后 //说明我们的一个完整的消息的发送流程就结束了。 batch.done(baseOffset, timestamp, exception); //看起来这个代码就是要回收资源的。 this.accumulator.deallocate(batch); if (error != Errors.NONE) this.sensors.recordErrors(batch.topicPartition.topic(), batch.recordCount);
转载地址:https://blog.csdn.net/weixin_37850264/article/details/112385491 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月07日 03时51分33秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
说说在 Python 中,如何找出所有字符串匹配
2019-04-26
说说 Python 正则表达式中的那些字符类别码
2019-04-26
说说 Spring Boot 的条件化注解
2019-04-26
说说如何使用 Python 在 word 中创建表格
2019-04-26
Python 基础知识考题与解答(2020 版)
2019-04-26
说说 Oracle 的 SYSDATE 函数
2019-04-26
说说 Oracle 的 NVL 与 NVL2 函数
2019-04-26
说说 TCP 协议以及三次握手流程
2019-04-26
说说 Oracle 的 TRUNC 函数
2019-04-26
系统架构设计笔记(41)—— 系统过渡计划
2019-04-26
系统架构设计笔记(42)—— 软件架构概述
2019-04-26
系统架构设计笔记(57)—— 测试自动化与面向对象的测试
2019-04-26
系统架构设计笔记(58)—— 嵌入式系统概论
2019-04-26
说说 Python 的生成器表达式
2019-04-26
说说 Activiti 中的用户与组的概念
2019-04-26
系统架构设计笔记(62)—— 嵌入式数据库管理系统
2019-04-26
系统架构设计笔记(63)—— 实时嵌入式操作系统
2019-04-26
说说如何使用 Canvas 绘制弧线与曲线
2019-04-26
系统架构设计笔记(64)—— 嵌入式系统设计
2019-04-26