版本说明
- JDK 1.8
- RabbitMQ 3.7.15 Erlang 22.0
- SpringBoot 2.3.3.RELEASE
- // TODO 2021年1月8日 整理CentOS安装RabbitMQ流程
1. 在RabbitMQ的Web管理界面,创建test队列
参数的含义
durability:是否持久化(重启或宕机后消息依然保存)
- durable 持久
- transient 暂时
新建maven项目。
2. pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ponent@Slf4jpublic class Consumer { /** * 消费者1 模拟正常处理消息的情况,消息处理完毕发送确认应答 * @param message * @param channel * @throws IOException */ @RabbitListener(queues = "test") public void process1(Message message, Channel channel) throws IOException { log.info("消费者1 接收消息: " + new String(message.getBody())); log.info("消费者1 确认应答消息:" + new String(message.getBody())); channel.basicAck(message.getMessageProperties().getDeliveryTag(), true); } /** * 消费者2 模拟处理消息出错的情况,消费者2向rabbitmq发送拒绝应答。 * 处理失败的消息会被重新放入ready中,再次发送给消费者,直至收到确认应答 * @param message * @param channel * @throws IOException */ @RabbitListener(queues = "test") public void process2(Message message, Channel channel) throws IOException { log.info("消费者2 接收消息:" + new String(message.getBody())); log.info("消费者2 拒绝应答消息:" + new String(message.getBody())); channel.basicReject(message.getMessageProperties().getDeliveryTag(), true); }}8. 测试RabbitMqController.java
@RestController@RequestMapping("")public class RabbitMqController { @Autowired private Producer producer; @GetMapping("/send") public String send() { producer.send(); return "发送完成"; }}9. 测试
使用postman或浏览器使用Get方法请求http://localhost:20001/send,生产者会向RabbitMQ的test队列发送5条消息:
生产者发送消息,序号为: 0
生产者发送消息,序号为: 1
生产者发送消息,序号为: 2
生产者发送消息,序号为: 3
生产者发送消息,序号为: 4
可以看出序号为2的消息3次被消费者2接收,消费者2也3次发送拒绝应答,直到第4次才被消费者1接收,并返回确认应答。
到此这篇关于SpringBoot整合RabbitMQ 手动应答 简单demo的文章就介绍到这了,更多相关SpringBoot整合RabbitMQ 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!