rabbitmq五种模式详解(含实现代码) (6)

topicQueueTest2,只有消费者2 可以消费

至此,五种队列的实现已结束!

7. 实现生产者消息确认 7.1 配置文件 ######################### RabbitMQ配置 ######################## # RabbitMQ主机 spring.rabbitmq.host=127.0.0.1 # RabbitMQ虚拟主机 spring.rabbitmq.virtual-host=demo # RabbitMQ服务端口 spring.rabbitmq.port=5672 # RabbitMQ服务用户名 spring.rabbitmq.username=admin # RabbitMQ服务密码 spring.rabbitmq.password=admin # RabbitMQ服务发布确认属性配置 ## NONE值是禁用发布确认模式,是默认值 ## CORRELATED值是发布消息成功到交换器后会触发回调方法 ## SIMPLE值经测试有两种效果,其一效果和CORRELATED值一样会触发回调方法,其二在发布消息成功后使用rabbitTemplate调用waitForConfirms或waitForConfirmsOrDie方法等待broker节点返回发送结果,根据返回结果来判定下一步的逻辑,要注意的点是waitForConfirmsOrDie方法如果返回false则会关闭channel,则接下来无法发送消息到broker; spring.rabbitmq.publisher-confirm-type=simple # 连接超时时间 spring.rabbitmq.connection-timeout=20000 # RabbitMQ服务开启消息发送确认 spring.rabbitmq.publisher-returns=true ######################### simple模式配置 ######################## # RabbitMQ服务 消息接收确认模式 ## NONE:不确认 ## AUTO:自动确认 ## MANUAL:手动确认 spring.rabbitmq.listener.simple.acknowledge-mode=manual # 指定最小的消费者数量 spring.rabbitmq.listener.simple.concurrency=1 # 指定最大的消费者数量 spring.rabbitmq.listener.simple.max-concurrency=1 # 每次只消费一个消息 spring.rabbitmq.listener.simple.prefetch=1 # 开启支持重试 spring.rabbitmq.listener.simple.retry.enabled=true # 启用强制信息,默认为false spring.rabbitmq.template.mandatory=true 7.2 编写消息发送确认类 RabbitConfirmCallback package com.gmtgo.demo.config; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.connection.CorrelationData; import org.springframework.amqp.rabbit.core.RabbitTemplate; /** * @author 大帅 */ @Slf4j public class RabbitConfirmCallback implements RabbitTemplate.ConfirmCallback { @Override public void confirm(CorrelationData correlationData, boolean ack, String cause) { log.info("=======ConfirmCallback========="); log.info("correlationData {} " , correlationData); log.info("ack = {}" , ack); log.info("cause = {}" , cause); log.info("=======ConfirmCallback========="); } } 7.3 编写消息发送交换机返回机制RabbitConfirmReturnCallBack package com.gmtgo.demo.config; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.core.RabbitTemplate; /** * @author 大帅 */ @Slf4j public class RabbitConfirmReturnCallBack implements RabbitTemplate.ReturnCallback { @Override public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) { log.info("--------------ReturnCallback----------------"); log.info("message = " + message); log.info("replyCode = {}", replyCode); log.info("replyText = {}", replyText); log.info("exchange = {}", exchange); log.info("routingKey = {}", routingKey); log.info("--------------ReturnCallback----------------"); } } 7.4 RabbitMQ配置

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpgyzj.html