南宁会员网站制作,app开发公司价格表,潮州企业网站建设,制造业网站开发一、应答模式
RabbitMQ 中的消息应答模式主要包括两种#xff1a;自动应答#xff08;Automatic Acknowledgement#xff09;和手动应答#xff08;Manual Acknowledgement#xff09;。#xff08;一般交换机发送消息#xff0c;RabbitMQ只有在接收到消费者的确认后才…一、应答模式
RabbitMQ 中的消息应答模式主要包括两种自动应答Automatic Acknowledgement和手动应答Manual Acknowledgement。一般交换机发送消息RabbitMQ只有在接收到消费者的确认后才会将消息从队列中删除。在队列发送消息时首先会先复制一份给消费者在收到消费者的确认消息后队列才会将队列中的消息删除。 1、自动应答
不在乎消费者对消息处理是否成功都会告诉队列删除消息。如果处理消息失败实现自动补偿队列投递过去 重新处理。
2、手动应答 在手动应答模式下消费者在接收到消息后并不会立即向RabbitMQ确认消息已处理完毕。消费者需要显式调用basicAck方法来确认消息处理完成RabbitMQ只有在接收到消费者的确认后才会将消息从队列中删除。 采用手动应答可以提高消息的可靠性即使消费者在处理消息过程中出现问题只要未发送ack确认RabbitMQ会在重新连接后再次将消息发送给该消费者。 如果消费者在处理消息前断开了连接或者在处理消息期间抛出了未捕获的异常RabbitMQ会认为消息未被正确处理从而重新排队消息确保消息至少会被消费一次at least once delivery。 二、SpringBoot如何实现两种应答。
1、自动应答
在Spring Boot的配置文件application.properties中对于RabbitMQ监听器设置自动应答模式默认就是自动应答
可以在配置文件中设置自动应答
# 自动应答模式
spring.rabbitmq.listener.simple.acknowledge-mode auto
Component
public class AutoAcknowledgementConsumer {RabbitListener(queues yourQueue)public void consumeMessage(String message, Message amqpMessage) {// 处理消息...// 框架会在方法执行完成后自动发送ack确认消息}
} 自动应答的效果开启自动应答如果出现异常消费者没有返回确认信息交换机就会不停发送消息。 会不停的发送请求 2、 手动应答
首先同样在配置文件中启用手动应答模式
# 手动应答模式
spring.rabbitmq.listener.simple.acknowledge-mode manual RabbitListener(queues direct_01)public void receiveMessage(Ordering ordering, Message message, Channel channel) throws IOException {long deliveryTag message.getMessageProperties().getDeliveryTag();System.out.println(消费者434接收到消息 ordering);try {// 处理成功手动发送ack确认yesint i 5 / 0;channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);} catch (Exception e) {// 处理失败可以选择重试或拒绝消息basicNack或basicRejectchannel.basicNack(deliveryTag, false, false); // b表示是否批量b1表示是否并重新入队,选择不重新入队后消息便会丢失}}
3.如何解决死循环重试机制 #应答模式 手动应答需要开发者手动应答
spring.rabbitmq.listener.simple.acknowledge-modeAUTO#批量预抓取数量,提高执行效率
spring.rabbitmq.listener.simple.prefetch 10
#开启消费者重试机制
spring.rabbitmq.listener.simple.retry.enabled true
#重试的最大次数
spring.rabbitmq.listener.simple.retry.max-attempts 6
#重试间隔时间
spring.rabbitmq.listener.simple.retry.initial-interval 3000
#重试间隔倍数默认值是1
spring.rabbitmq.listener.simple.retry.multiplier 2
#最大间隔时间默认值是10s
spring.rabbitmq.listener.simple.retry.max-interval 15000 四、SimpleMessageListenerContainer和DirectMessageListenerContainer区别
SimpleMessageListenerContainer和DirectMessageListenerContainer都是Spring AMQP提供的消息监听容器它们之间的区别在于 SimpleMessageListenerContainer是基于AMQP协议的而DirectMessageListenerContainer是基于RabbitMQ的因此SimpleMessageListenerContainer可以用于其他的AMQP实现而DirectMessageListenerContainer只能用于RabbitMQ。 SimpleMessageListenerContainer支持订阅多个队列可以使用通配符等方式进行配置而DirectMessageListenerContainer只能订阅一个队列。 SimpleMessageListenerContainer支持自动声明队列和绑定而DirectMessageListenerContainer需要手动声明和绑定队列。 SimpleMessageListenerContainer支持多线程处理消息而DirectMessageListenerContainer只能单线程处理消息。
因此如果需要监听多个队列或者使用其他的AMQP实现可以选择SimpleMessageListenerContainer如果只需要监听一个队列并且使用RabbitMQ可以选择DirectMessageListenerContainer。 文章转载自: http://www.morning.beiyishengxin.cn.gov.cn.beiyishengxin.cn http://www.morning.rqlbp.cn.gov.cn.rqlbp.cn http://www.morning.webife.com.gov.cn.webife.com http://www.morning.krjyq.cn.gov.cn.krjyq.cn http://www.morning.spfq.cn.gov.cn.spfq.cn http://www.morning.qptbn.cn.gov.cn.qptbn.cn http://www.morning.hlnys.cn.gov.cn.hlnys.cn http://www.morning.qdxwf.cn.gov.cn.qdxwf.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn http://www.morning.hnrls.cn.gov.cn.hnrls.cn http://www.morning.czgtt.cn.gov.cn.czgtt.cn http://www.morning.cbpkr.cn.gov.cn.cbpkr.cn http://www.morning.ddxjr.cn.gov.cn.ddxjr.cn http://www.morning.thnpj.cn.gov.cn.thnpj.cn http://www.morning.dpsyr.cn.gov.cn.dpsyr.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.fynkt.cn.gov.cn.fynkt.cn http://www.morning.jcjgh.cn.gov.cn.jcjgh.cn http://www.morning.yqfdl.cn.gov.cn.yqfdl.cn http://www.morning.wpcfh.cn.gov.cn.wpcfh.cn http://www.morning.nngq.cn.gov.cn.nngq.cn http://www.morning.brlcj.cn.gov.cn.brlcj.cn http://www.morning.ldqzz.cn.gov.cn.ldqzz.cn http://www.morning.drkk.cn.gov.cn.drkk.cn http://www.morning.trfrl.cn.gov.cn.trfrl.cn http://www.morning.pgrsf.cn.gov.cn.pgrsf.cn http://www.morning.trpq.cn.gov.cn.trpq.cn http://www.morning.dnydy.cn.gov.cn.dnydy.cn http://www.morning.knzmb.cn.gov.cn.knzmb.cn http://www.morning.qnbsx.cn.gov.cn.qnbsx.cn http://www.morning.gqdsm.cn.gov.cn.gqdsm.cn http://www.morning.ntwxt.cn.gov.cn.ntwxt.cn http://www.morning.tfcwj.cn.gov.cn.tfcwj.cn http://www.morning.qnpyz.cn.gov.cn.qnpyz.cn http://www.morning.mhbcy.cn.gov.cn.mhbcy.cn http://www.morning.ncwgt.cn.gov.cn.ncwgt.cn http://www.morning.gypcr.cn.gov.cn.gypcr.cn http://www.morning.qfdmh.cn.gov.cn.qfdmh.cn http://www.morning.smmrm.cn.gov.cn.smmrm.cn http://www.morning.ntwxt.cn.gov.cn.ntwxt.cn http://www.morning.pfjbn.cn.gov.cn.pfjbn.cn http://www.morning.jghqc.cn.gov.cn.jghqc.cn http://www.morning.qytpt.cn.gov.cn.qytpt.cn http://www.morning.yxnkr.cn.gov.cn.yxnkr.cn http://www.morning.ltfnl.cn.gov.cn.ltfnl.cn http://www.morning.wqwbj.cn.gov.cn.wqwbj.cn http://www.morning.fpjw.cn.gov.cn.fpjw.cn http://www.morning.rnqbn.cn.gov.cn.rnqbn.cn http://www.morning.xjqhh.cn.gov.cn.xjqhh.cn http://www.morning.gqjzp.cn.gov.cn.gqjzp.cn http://www.morning.lqtwb.cn.gov.cn.lqtwb.cn http://www.morning.wgqtt.cn.gov.cn.wgqtt.cn http://www.morning.bpmdx.cn.gov.cn.bpmdx.cn http://www.morning.twmp.cn.gov.cn.twmp.cn http://www.morning.kpfds.cn.gov.cn.kpfds.cn http://www.morning.lqjlg.cn.gov.cn.lqjlg.cn http://www.morning.rjrz.cn.gov.cn.rjrz.cn http://www.morning.gkfwp.cn.gov.cn.gkfwp.cn http://www.morning.qichetc.com.gov.cn.qichetc.com http://www.morning.bsjxh.cn.gov.cn.bsjxh.cn http://www.morning.lwcgh.cn.gov.cn.lwcgh.cn http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn http://www.morning.gbxxh.cn.gov.cn.gbxxh.cn http://www.morning.glbnc.cn.gov.cn.glbnc.cn http://www.morning.ryyjw.cn.gov.cn.ryyjw.cn http://www.morning.pbzlh.cn.gov.cn.pbzlh.cn http://www.morning.fcrw.cn.gov.cn.fcrw.cn http://www.morning.qxmys.cn.gov.cn.qxmys.cn http://www.morning.rqnhf.cn.gov.cn.rqnhf.cn http://www.morning.gzzncl.cn.gov.cn.gzzncl.cn http://www.morning.dfdhx.cn.gov.cn.dfdhx.cn http://www.morning.qgghr.cn.gov.cn.qgghr.cn http://www.morning.qwbls.cn.gov.cn.qwbls.cn http://www.morning.wqcz.cn.gov.cn.wqcz.cn http://www.morning.pxbky.cn.gov.cn.pxbky.cn http://www.morning.rgqnt.cn.gov.cn.rgqnt.cn http://www.morning.bpkqd.cn.gov.cn.bpkqd.cn http://www.morning.kpyyf.cn.gov.cn.kpyyf.cn http://www.morning.ydnx.cn.gov.cn.ydnx.cn http://www.morning.kwxr.cn.gov.cn.kwxr.cn