RabbitMQ(八)——消息确认
RabbitMQ系列
RabbitMQ(八)——消息确认
前言
为什么要使用消息确认?
某些场景中需要确保每条消息都要被成功处理,消息确认分为两种:
- 一种是生产者发送消息到Broker时,Broker给生产者发送消息确认回执,告诉生产者消息已被成功发送到Broker。
- 另外一种是消费者接收到Broker发送的消息时,消费者给Broker发送确认回执,用于通知消息已被成功消费。
第一种:生产者端消息确认
生产者向Broker发送消息,Broker接收到消息后给生产者发送确认回执。主要有两种方式:Tx机制模式和Confirm模式
Tx机制模式:
Tx机制可以叫事务机制,RabbitMQ中有三个Tx机制的方法:TxSelect(),TxCommit(),TxRollback()。
channel.TxSelect()将当前channel设置成transaction模式开启事务,channel.TxCommit()提交事务,channel.TxRollback()回滚事务使用Tx机制,首先使用channel.TxSelect()开启事务,然后发布消息给Broker服务器,如果执行channel.TxCommit()成功了表示消息被Broker接收了。当channel.TxCommit()提交时失败,可以捕获异常然后channel.TxRollback()回滚事务。
channel.TxSelect();
try {
for (int i = 0; i < 10; i++)
{
string msg = $"第{i + 1}条消息";
//5.发布消息
channel.BasicPublish("", "simple", null, Encoding.UTF8.GetBytes(msg));
Console.WriteLine($"已发送消息:{msg}");
}
channel.TxCommit();
}
catch
{
channel.TxRollback();
}

Confirm模式:
Confirm主要方法:ConfirmSelect() , WaitForCnofirms()和WaitForCnofirmOrDie()。
channel.ConfirmSelect()表示开启Confirm模式。channel.WaitForConfirms()等待所有消息确认,如果所有的消息都被服务端成功接收返回true,只要有一条没有被成功接收就返回false。channel.WaitForConfirmsOrDie()与channel.WaitForConfirms()相似,也是等待消息确认,区别在于该方法没有返回值,只要有任意一条消息没被成功接收,会抛出一个OperationInterrupedException类型异常;
//
// 摘要:
// Wait until all published messages have been confirmed.
//
// 参数:
// timeout:
// How long to wait (at most) before returning whether or not any nacks were returned.
//
// timedOut:
// True if the method returned because the timeout elapsed, not because all messages
// were ack'd or at least one nack'd.
//
// 返回结果:
// True if no nacks were received within the timeout, otherwise false.
//
// 言论:
// Waits until all messages published since the last call have been either ack'd
// or nack'd by the broker. Returns whether all the messages were ack'd (and none
// were nack'd). Note, throws an exception when called on a non-Confirm channel.
[AmqpMethodDoNotImplementAttribute(null)]
bool WaitForConfirms(TimeSpan timeout, out bool timedOut);
//
// 摘要:
// Wait until all published messages have been confirmed.
//
// 参数:
// timeout:
// How long to wait (at most) before returning whether or not any nacks were returned.
//
// 返回结果:
// True if no nacks were received within the timeout, otherwise false.
//
// 言论:
// Waits until all messages published since the last call have been either ack'd
// or nack'd by the broker. Returns whether all the messages were ack'd (and none
// were nack'd). Note, throws an exception when called on a non-Confirm channel.
[AmqpMethodDoNotImplementAttribute(null)]
bool WaitForConfirms(TimeSpan timeout);
//
// 摘要:
// Wait until all published messages have been confirmed.
//
// 言论:
// Waits until all messages published since the last call have been either ack'd
// or nack'd by the broker. Returns whether all the messages were ack'd (and none
// were nack'd). Note, throws an exception when called on a non-Confirm channel.
[AmqpMethodDoNotImplementAttribute(null)]
bool WaitForConfirms();
//
// 摘要:
// Wait until all published messages have been confirmed.
//
// 言论:
// Waits until all messages published since the last call have been ack'd by the
// broker. If a nack is received, throws an OperationInterrupedException exception
// immediately.
[AmqpMethodDoNotImplementAttribute(null)]
void WaitForConfirmsOrDie();
//
// 摘要:
// Wait until all published messages have been confirmed.
//
// 言论:
// Waits until all messages published since the last call have been ack'd by the
// broker. If a nack is received or the timeout elapses, throws an OperationInterrupedException
// exception immediately.
[AmqpMethodDoNotImplementAttribute(null)]
void WaitForConfirmsOrDie(TimeSpan timeout);
Confirm API
//开启事务
//channel.TxSelect();
channel.ConfirmSelect();
try {
for (int i = 0; i < 10; i++)
{
string msg = $"第{i + 1}条消息";
//5.发布消息
channel.BasicPublish("", "simple", null, Encoding.UTF8.GetBytes(msg));
Console.WriteLine($"已发送消息:{msg}");
}
//channel.TxCommit();
channel.WaitForConfirmsOrDie();
Console.WriteLine("发送完成");
}
catch(Exception ex)
{
//channel.TxRollback();
Console.WriteLine(ex.Message);
}

第二种:消费者端消息确认
Broker发送到消费者时,提供了两种消息确认方式:自动确认和显示确认。
自动确认:
当Broker向消费者发送消息后,不等消息处理结束,立即自动返回一个确认回执。使用:设置消费方法的参数autoAck为true。当Broker接收到确认回执时会删除消息,如果消费者在接收到消息并返回了确认回执后,然后消息还没处理消费者挂了,这条消息就找不回了。
channel.BasicConsume("simple", true, consumer);
显示确认:
自动确认会出现消息丢失,如果让消息处理完再返回确认回执那么久不存在消息丢失问题了,这就是显示确认的思路。使用:先设置消费方法的参数autoAck为false,再使用channel.BasicAck()或channel.BasicReject()方法确认、拒绝消息。
//确认消息:deliveryTag参数是分发的标记,multiple表示是否确认多条
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
//拒绝消息:deliveryTag参数也是分发的标记,requeue表示消息被拒绝后是否重新入队
channel.BasicReject(deliveryTag: ea.DeliveryTag, requeue: false);
当使用显示确认时,如果消费者处理完消息不发送回执,那么消息不会被删除,消息的状态一直是Unacked,这条消息也不会再发送给其他消费者。如果一个消费者在处理消息时尚未发送确认回执的情况下挂了,那么消息会被重新放入队列(状态从Unacked变成Ready),有其他消费者存在时,消息会发送给其他消费者。
consumer.Received += (model, e) =>
{
byte[] message = e.Body.ToArray(); //返回消息确认(true/false,自动/手动确认),没确认就不会消费掉消息
if (Encoding.UTF8.GetString(message).Contains("1"))
{
Console.WriteLine("接收消息:" + Encoding.UTF8.GetString(message));
channel.BasicAck(e.DeliveryTag, false);
}
else
{
Console.WriteLine("拒绝消息:" + Encoding.UTF8.GetString(message));
//拒绝消息 false:拒绝后丢弃 true:拒绝后重新入队
channel.BasicReject(e.DeliveryTag, false);
} };
//消费者开启监听
channel.BasicConsume("simple", false, consumer);

RabbitMQ(八)——消息确认的更多相关文章
- Java使用RabbitMQ之消息确认(confirm模板)
RabbitMQ生产者消息确认Confirm模式,分为普通模式.批量模式和异步模式,本次举例为普通模式. 源码: package org.study.confirm4; import com.rabb ...
- RabbitMQ的消息确认机制
一:确认种类 RabbitMQ的消息确认有两种. 一种是消息发送确认.这种是用来确认生产者将消息发送给交换器,交换器传递给队列的过程中,消息是否成功投递.发送确认分为两步,一是确认是否到达交换器,二是 ...
- RabbitMq初探——消息确认
消息确认机制 前言 消息队列的下游,业务逻辑可能复杂,处理任务可能花费很长时间.若在一条消息到达它的下游,任务刚处理了一半,由于不确定因素,下游的任务处理进程 被kill掉啦,导致任务无法执行完成.而 ...
- RabbitMq之消息确认
最近阅读了rabbitmq的官方文档,然后结合之前面试时被问到关于消息队列的问题来探索一下关于消息队列的消息确认机制. 其实消息确认就是消费者确认消息被消费了, 生产者确认消息已经发送到了消息队列中了 ...
- RabbitMQ学习笔记六:RabbitMQ之消息确认
使用消息队列,必须要考虑的问题就是生产者消息发送失败和消费者消息处理失败,这两种情况怎么处理. 生产者发送消息,成功,则确认消息发送成功;失败,则返回消息发送失败信息,再做处理. 消费者处理消息,成功 ...
- RabbitMQ 之消息确认机制(事务+Confirm)
概述 在 Rabbitmq 中我们可以通过持久化来解决因为服务器异常而导致丢失的问题,除此之外我们还会遇到一个问题:生产者将消息发送出去之后,消息到底有没有正确到达 Rabbit 服务器呢?如果不错得 ...
- RabbitMQ (十一) 消息确认机制 - 消费者确认
由于生产者和消费者不直接通信,生产者只负责把消息发送到队列,消费者只负责从队列获取消息(不管是push还是pull). 消息被"消费"后,是需要从队列中删除的.那怎么确认消息被&q ...
- springboot整合rabbitmq,支持消息确认机制
安装 推荐一篇博客https://blog.csdn.net/zhuzhezhuzhe1/article/details/80464291 项目结构 POM.XML <?xml version= ...
- 快速掌握RabbitMQ(三)——消息确认、持久化、优先级的C#实现
1 消息确认 在一些场合,如转账.付费时每一条消息都必须保证成功的被处理.AMQP是金融级的消息队列协议,有很高的可靠性,这里介绍在使用RabbitMQ时怎么保证消息被成功处理的.消息确认可以分为两种 ...
- RabbitMQ的消息确认ACK机制
1.什么是消息确认ACK. 答:如果在处理消息的过程中,消费者的服务器在处理消息的时候出现异常,那么可能这条正在处理的消息就没有完成消息消费,数据就会丢失.为了确保数据不会丢失,RabbitMQ支持消 ...
随机推荐
- pycharm之激活
激活相关文件: https://github.com/lanlangdeai/develop-kit/tree/master/software/editor/pycharm 一. 激活码激活 步骤: ...
- brew之加速
有没有出现这种场景:使用brew install 安装程序,一直卡在brew updating,这可能是使用着默认的github镜像源导致,那么我们就需要将其切换到国内 1.镜像切换(推荐中科大) 1 ...
- Sealos Devbox 使用教程:使用 Cursor 一键搞定数据库开发环境
"诶,你这前后端开发环境怎么搭建这么快?" "用了 Devbox 啊." "不是吧,你怎么在 Cursor 里连接开发环境的数据库,这些都配好了?&q ...
- new Date()在ios下的坑
坑位 最新在开发一个时间轴功能,在使用new Date()的时候发现在IOS下全是NaN. Why new Date("2018-04-27 11:11")在chrome,fire ...
- StarBlog博客Vue前端开发笔记:(2)SASS与SCSS
前言 本项目需要使用 SCSS 来编写页面样式. Sass (Syntactically Awesome Stylesheets)是一个 css 预处理器,而 SCSS 是 Sass 的一种语法格式, ...
- 我们需要什么样的 ORM 框架
了解我的人都知道, 本人一直非常排斥 ORM 框架, 由于对象关系阻抗不匹配, 一直觉得它没有什么用, 操作数据库最好的手段是 sql+动态语言. 但这两年想法有了重大改变. 2013 年用 js 实 ...
- ChatGPT生成接口测试用例(二)
5.1.4 自动生成测试数据 测试数据的生成通常是接口测试的一个烦琐任务.ChatGPT可以帮助测试团队生成测试数据,包括各种输入和它们的组合.测试人员可以描述他们需要的数据类型和范围,ChatGPT ...
- 【转载】获取Java接口的所有实现类
https://www.cnblogs.com/wangzhen-fly/p/11002814.html 前言:想看基于spring 的最简单实现方法,请直接看 第七步. 本文价值在于 包扫描的原理探 ...
- The "https://packagist.phpcomposer.com/packages.json" file could not be down
composer自身版本太低了,更新下 composer self-update 使用阿里云镜像 composer config -g repo.packagist composer https:// ...
- Element库的Vue版本ElementUI的本地引入方法
最近刚接触ElementUI,发现官方介绍的使用方法中只有npm安装和CDN引入这两种方式,没有本地引入的方法. 因为我的学习环境有时候是断网状态的,所以自己研究了一下本地引入的方法,记录在此. 1. ...