手机英语网站,WordPress文章小工具,网站点击量软件,推广型网站建设重试机制
在消息从Broker到消费者的传递过程中#xff0c;可能会遇到各种问题#xff0c;如网络故障、服务不可用、资源不足等#xff0c;这些问题都可能导致消息处理失败。为了解决这些问题#xff0c;RabbitMQ提供了重试机制#xff0c;允许消息在处理失败之后重新发送…重试机制
在消息从Broker到消费者的传递过程中可能会遇到各种问题如网络故障、服务不可用、资源不足等这些问题都可能导致消息处理失败。为了解决这些问题RabbitMQ提供了重试机制允许消息在处理失败之后重新发送。
但如果是程序逻辑引起的错误那么多次重试也是不起作用的因此设置了重试次数。
消费者确认机制为AUTO时
当消费者确认机制是AUTO时如果程序逻辑错误那么就会不断重试造成消息积压。因此我们就需要设置重试次数当多次重试还是失败消息就会被自动确认自然消息就会丢失。
spring:rabbitmq:host: 43.138.108.125port: 5672username: adminpassword: adminvirtual-host: mq-springboot-testlistener:simple:retry:enabled: true # 开启消费者失败重试initial-interval: 5000ms # 初始失败等待时长max-attempts: 5 # 最大重试次数
Configuration
public class RetryConfig {Bean(retryQueue)public Queue retryQueue() {return QueueBuilder.durable(Constants.RETRY_QUEUE).build();}Bean(retryExchange)public Exchange retryExchange() {return ExchangeBuilder.directExchange(Constants.RETRY_EXCHANGE).durable(true).build();}Bean(retryQueueBind)public Binding retryQueueBind(Qualifier(retryExchange) Exchange exchange,Qualifier(retryQueue) Queue queue) {return BindingBuilder.bind(queue).to(exchange).with(retry).noargs();}}
RestController
RequestMapping(/retry)
public class RetryController {Resourcepublic RabbitTemplate rabbitTemplate;RequestMappingpublic void retryQueue() {this.rabbitTemplate.convertAndSend(Constants.RETRY_EXCHANGE, retry, hello 重试机制);System.out.println(重试机制生产者发送消息成功);}}
Configuration
public class RetryListener {RabbitListener(queues Constants.RETRY_QUEUE)public void retryListener(String msg) {System.out.println(获取到消息 msg);int n 3 / 0;}}
上述代码和可靠性传输一文的消费者确认机制中策略为AUTO的代码类似只不过在此配置文件中加了一个重试机制。当启动程序之后可以看到如下结果 重试时 重试结束之后 从测试结果可以看出当消费者确认机制的策略为AUTO时遇到异常就会进行重试当重试结束之后依然没有接收时就会自动确认消息。
消费者确认机制为MANAUL时
当消费者确认机制是MANUL时修改消费者代码并启动程序查看结果
spring:rabbitmq:host: 43.138.108.125port: 5672username: adminpassword: adminvirtual-host: mq-springboot-testlistener:simple:acknowledge-mode: manual # 消息确认机制手动确认retry:enabled: true # 开启消费者失败重试initial-interval: 5000ms # 初始失败等待时长max-attempts: 5 # 最大重试次数
Configuration
public class RetryListener {RabbitListener(queues Constants.RETRY_QUEUE)public void retryListener(Message msg, Channel channel) throws IOException {try {System.out.println(接收到消息 msg);int num 3 / 0; // 模拟处理失败channel.basicAck(msg.getMessageProperties().getDeliveryTag(), true);} catch (Exception e) {channel.basicReject(msg.getMessageProperties().getDeliveryTag(), true);}}} 从测试结果可以得出消费者确认机制为手动确认时并不会依据配置文件中的重试次数等内容来做而是依据消息者自身的代码实现来做实现机制。原因是因为手动确认模式下消费者需要显示地对消息进行确认如果消费者在消息处理过程中遇到异常可以选择确认不确定消息也可以选择重新入队。所以重试的控制权并不在应用程序本身而在于代码逻辑本身。 1. 消费者确认机制为AUTO时如果程序逻辑异常多次重试还是失败。那么消息就会自动确认进而消息就会丢失。 2. 消费者确认机制为MANAUL时如果程序逻辑异常多次重试依然处理失败无法被确认消息就会积压。 3. 消费者确认机制为NONE时不管发生什么情况当消息从Broker内部发出时就会自动确认因此它不存在任何内容。 TTL
TTL过期时间。当消息到达过期时间之后还没有被消息消息就会被自动清除。
RabbitMQ可以对队列和消息设置过期时间。如果两种方法同时使用那么就以两者较小的值为准。
设置消息的TTL
Configuration
public class TllConfig {Bean(ttlQueue)public Queue ttlQueue() {return QueueBuilder.durable(Constants.TTL_QUEUE).build();}Bean(ttlExchange)public Exchange ttlExchange() {return ExchangeBuilder.directExchange(Constants.TTL_EXCHANGE).build();}Bean(ttlQueueBind)public Binding ttlQueueBind(Qualifier(ttlExchange) Exchange exchange,Qualifier(ttlQueue) Queue queue) {return BindingBuilder.bind(queue).to(exchange).with(ttl).noargs();}}
RestController
RequestMapping(/ttl)
public class TtlController {Resourceprivate RabbitTemplate rabbitTemplate;RequestMappingpublic void ttlQueue() {MessagePostProcessor messagePostProcessor new MessagePostProcessor() {Overridepublic Message postProcessMessage(Message message) throws AmqpException {message.getMessageProperties().setExpiration(50000);return message;}};this.rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE, ttl, hello ttl, messagePostProcessor);}} 在TTL的测试中不需要消费者的存在否则看不到消息在队列中的自动丢失。
设置队列的TTL
注意设置队列的TTL并不是过期之后删除整个队列也是关于消息设置的。只不过投递到该消息队列的所有消息都有一个共同的过期时间而已。
Configuration
public class TllConfig {Bean(ttlQueue)public Queue ttlQueue() {return QueueBuilder.durable(Constants.TTL_QUEUE).ttl(5000).build();}Bean(ttlExchange)public Exchange ttlExchange() {return ExchangeBuilder.directExchange(Constants.TTL_EXCHANGE).build();}Bean(ttlQueueBind)public Binding ttlQueueBind(Qualifier(ttlExchange) Exchange exchange,Qualifier(ttlQueue) Queue queue) {return BindingBuilder.bind(queue).to(exchange).with(ttl).noargs();}}
设置队列的TTL只需要在声明队列时给出过期时间即可。在测试的过程中如果是先测试了消息的过期时间那么在测试队列时需要先将持久化的队列给删除再启动程序。
当启动程序之后可以看到队列上加了一个TTL的标识表明队列的过期时间设置成功 区别
设置队列的过期时间一旦消息过期就会从队列中删除。
设置消息的过期时间即使消息过期如果消息不在队首还得等到消息到达队首之后才会进行判定是否过期。如果过期那就删除反之就投递到相应的消费者中。 为什么这两种方法处理的方式不一样 因为设置队列的过期时间那么队列中过期的消息一定在队首RabbitMQ只需要定期从队首扫描消息是否有过期的消息即可。 而设置消息的过期时间每条消息的过期时间都不一致如果要删除队列的所有过期消息那么就要扫描整个队列所以不如等到消息要进行投递时再判断消息是否过期这样可以减少一定的资源消耗。 文章转载自: http://www.morning.xfyjn.cn.gov.cn.xfyjn.cn http://www.morning.xrqkm.cn.gov.cn.xrqkm.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.pqqxc.cn.gov.cn.pqqxc.cn http://www.morning.prznc.cn.gov.cn.prznc.cn http://www.morning.jrgxx.cn.gov.cn.jrgxx.cn http://www.morning.gqcsd.cn.gov.cn.gqcsd.cn http://www.morning.mfbcs.cn.gov.cn.mfbcs.cn http://www.morning.lgwpm.cn.gov.cn.lgwpm.cn http://www.morning.bkpbm.cn.gov.cn.bkpbm.cn http://www.morning.rxhs.cn.gov.cn.rxhs.cn http://www.morning.gglhj.cn.gov.cn.gglhj.cn http://www.morning.dmthy.cn.gov.cn.dmthy.cn http://www.morning.qhrlb.cn.gov.cn.qhrlb.cn http://www.morning.stfdh.cn.gov.cn.stfdh.cn http://www.morning.kndyz.cn.gov.cn.kndyz.cn http://www.morning.fhrt.cn.gov.cn.fhrt.cn http://www.morning.kxnnh.cn.gov.cn.kxnnh.cn http://www.morning.gtbjc.cn.gov.cn.gtbjc.cn http://www.morning.nmfml.cn.gov.cn.nmfml.cn http://www.morning.tpps.cn.gov.cn.tpps.cn http://www.morning.lkmks.cn.gov.cn.lkmks.cn http://www.morning.bpmnh.cn.gov.cn.bpmnh.cn http://www.morning.hprmg.cn.gov.cn.hprmg.cn http://www.morning.sfwcx.cn.gov.cn.sfwcx.cn http://www.morning.rpstb.cn.gov.cn.rpstb.cn http://www.morning.hlshn.cn.gov.cn.hlshn.cn http://www.morning.tkryt.cn.gov.cn.tkryt.cn http://www.morning.weitao0415.cn.gov.cn.weitao0415.cn http://www.morning.wgrl.cn.gov.cn.wgrl.cn http://www.morning.rxnr.cn.gov.cn.rxnr.cn http://www.morning.gstg.cn.gov.cn.gstg.cn http://www.morning.jwpcj.cn.gov.cn.jwpcj.cn http://www.morning.zlgbx.cn.gov.cn.zlgbx.cn http://www.morning.wwthz.cn.gov.cn.wwthz.cn http://www.morning.tpwrm.cn.gov.cn.tpwrm.cn http://www.morning.pjtw.cn.gov.cn.pjtw.cn http://www.morning.qwqzk.cn.gov.cn.qwqzk.cn http://www.morning.qmqgx.cn.gov.cn.qmqgx.cn http://www.morning.dnconr.cn.gov.cn.dnconr.cn http://www.morning.poapal.com.gov.cn.poapal.com http://www.morning.ymsdr.cn.gov.cn.ymsdr.cn http://www.morning.bswhr.cn.gov.cn.bswhr.cn http://www.morning.qfzjn.cn.gov.cn.qfzjn.cn http://www.morning.lynb.cn.gov.cn.lynb.cn http://www.morning.npgwb.cn.gov.cn.npgwb.cn http://www.morning.bfjyp.cn.gov.cn.bfjyp.cn http://www.morning.qnzld.cn.gov.cn.qnzld.cn http://www.morning.kgnrh.cn.gov.cn.kgnrh.cn http://www.morning.jxhlx.cn.gov.cn.jxhlx.cn http://www.morning.dpmkn.cn.gov.cn.dpmkn.cn http://www.morning.whothehellami.com.gov.cn.whothehellami.com http://www.morning.pcshb.cn.gov.cn.pcshb.cn http://www.morning.wxrbl.cn.gov.cn.wxrbl.cn http://www.morning.fjglf.cn.gov.cn.fjglf.cn http://www.morning.hwpcm.cn.gov.cn.hwpcm.cn http://www.morning.rpfpx.cn.gov.cn.rpfpx.cn http://www.morning.wfbnp.cn.gov.cn.wfbnp.cn http://www.morning.twdkt.cn.gov.cn.twdkt.cn http://www.morning.ryxgk.cn.gov.cn.ryxgk.cn http://www.morning.hnhkz.cn.gov.cn.hnhkz.cn http://www.morning.wctqc.cn.gov.cn.wctqc.cn http://www.morning.zrhhb.cn.gov.cn.zrhhb.cn http://www.morning.wcjk.cn.gov.cn.wcjk.cn http://www.morning.deanzhu.com.gov.cn.deanzhu.com http://www.morning.yxplz.cn.gov.cn.yxplz.cn http://www.morning.rcmcw.cn.gov.cn.rcmcw.cn http://www.morning.dgfpp.cn.gov.cn.dgfpp.cn http://www.morning.qqnp.cn.gov.cn.qqnp.cn http://www.morning.xkjrq.cn.gov.cn.xkjrq.cn http://www.morning.ghpld.cn.gov.cn.ghpld.cn http://www.morning.hlnys.cn.gov.cn.hlnys.cn http://www.morning.rcntx.cn.gov.cn.rcntx.cn http://www.morning.lwmxk.cn.gov.cn.lwmxk.cn http://www.morning.gthc.cn.gov.cn.gthc.cn http://www.morning.nmkbl.cn.gov.cn.nmkbl.cn http://www.morning.mqwdh.cn.gov.cn.mqwdh.cn http://www.morning.wjlkz.cn.gov.cn.wjlkz.cn http://www.morning.drywd.cn.gov.cn.drywd.cn http://www.morning.xjkr.cn.gov.cn.xjkr.cn