srpingboot整合rabbitmq 实现死信列队并演示1 basic.nack 并且不再重新投递 requeue=false 和 2 ttl消息 3 达到队列最大长度 三种情况

编程入门 行业动态 更新时间:2024-10-28 16:22:22

关于死信队列的描述,可查看博文 
blog.csdn.net/qq_41712271/article/details/115658848?spm=1001.2014.3001.5501

1 application.yml配置文件

server:port: 8021
spring:#给项目来个名字application:name: rabbitmq-test#配置rabbitMq 服务器rabbitmq:host: 127.0.0.1port: 5672username: needpassword: 123456#虚拟host 可以不设置,使用server默认hostvirtual-host: /testhostlistener:simple:acknowledge-mode: manual #手动ACKdirect:acknowledge-mode: manual #手动ACK

2 创建交换机,队列,绑定等,注意看createZhengChangQueue() 方法,创建后的效果图,也可以看上面的博文

import .springframework.amqp.core.*;
import .springframework.amqp.rabbit.connection.ConnectionFactory;
import .springframework.amqp.rabbit.core.RabbitAdmin;
import .springframework.beans.factory.annotation.Autowired;
import .springframework.context.annotation.Bean;
import .springframework.context.annotation.Configuration;
import java.util.HashMap;@Configuration
public class ExchangeQueueBindingConfig {//就是靠他@AutowiredRabbitAdmin rabbitAdmin;//死信队列@Beanpublic Queue createShiXinQueue() {return new Queue("shixin_queue", true);}//死信交换机@Beanpublic DirectExchange createShiXinExchange() {return new DirectExchange("shixin_exchange", true, false);}//死信交换机 死信队列 绑定@Beanpublic Binding createShiXinBinding() {return BindingBuilder.bind(createShiXinQueue()).to(createShiXinExchange()).with("shixin");}//正常队列,这里要设置死信交换机,关键所在@Beanpublic Queue createZhengChangQueue() {HashMap<String, Object> map = new HashMap<String, Object>();map.put("x-dead-letter-exchange", "shixin_exchange");map.put("x-dead-letter-routing-key", "shixin");//故意设置成500,演示 消息满以后,会到死信队列中map.put("x-max-length",500);return new Queue("zhengchang_queue", true, false, false, map);}//正常交换机@Beanpublic TopicExchange createZhengChangExchange() {return new TopicExchange("zhengchang_exchange", true, false);}//正常交换机 正常队列 绑定@Beanpublic Binding createZhengChangBinding() {return BindingBuilder.bind(createZhengChangQueue()).to(createZhengChangExchange()).with("chongqing.#");}//创建初始化RabbitAdmin对象@Beanpublic RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);// 只有设置为 true,spring 才会加载 RabbitAdmin 这个类rabbitAdmin.setAutoStartup(true);return rabbitAdmin;}//创建交换机和对列@Beanpublic void createExchangeQueue() {//死信rabbitAdmin.declareExchange(createShiXinExchange());rabbitAdmin.declareQueue(createShiXinQueue());//正常rabbitAdmin.declareExchange(createZhengChangExchange());rabbitAdmin.declareQueue(createZhengChangQueue());}
}

3 演示发送TTL超时消息,并不要有消费者去消费,过期后就会到死信队列

import .huawei.rabbitmqtest1.pojo.User;
import .alibaba.fastjson.JSON;
import .junit.jupiter.api.Test;
import .springframework.amqp.core.Message;
import .springframework.amqp.core.MessageProperties;
import .springframework.amqp.rabbit.connection.CorrelationData;
import .springframework.amqp.rabbit.core.RabbitTemplate;
import .springframework.beans.factory.annotation.Autowired;
import .springframework.boot.test.context.SpringBootTest;import java.nio.charset.StandardCharsets;
import java.util.UUID;@SpringBootTest
public class Test_1 {@AutowiredRabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法@Testvoid fangfa1() {MessageProperties messageProperties = new MessageProperties();messageProperties.setExpiration("9000"); // 设置过期时间,单位:毫秒for (int i = 1; i < 10; i++) {User user = new User(i + "", "小明 " + i);//  这个参数是用来做消息的唯一标识//发布消息时使用,存储在消息的headers中CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());Message message = new Message(JSON.toJSONString(user).getBytes(StandardCharsets.UTF_8), messageProperties);rabbitTemplate.convertAndSend("zhengchang_exchange", "chongqing.news", message, correlationData);}}
}

4 一定要手动ack,演示 故意乱输信息(文字等),会出错,进而走到 basic.nack 并且不再重新投递 requeue=false,就会到死信队列

import .rabbitmq.client.Channel;
import .springframework.amqp.core.Message;
import .springframework.amqp.rabbit.annotation.RabbitListener;
import .springframework.stereotype.Component;
import java.io.IOException;@Component
public class Test_2 {//监听的队列名称@RabbitListener(queues = "zhengchang_queue")public void process(Message message, Channel channel) throws IOException {long deliveryTag = message.getMessageProperties().getDeliveryTag();try {String msgbody = new String(message.getBody());//1.接收转换消息System.out.println("消费者 1 收到消息  : " + msgbody + " 编号: " + deliveryTag);//2. 处理业务逻辑System.out.println("处理业务逻辑...");//模拟出现错误System.out.println(500 / Double.valueOf(msgbody));//3. 手动签收channel.basicAck(deliveryTag, true);} catch (Exception e) {//4.拒绝签收/*第三个参数:requeue:重回队列。如果设置为true,则消息重新回到queue,broker会重新发送该消息给消费端这里要演示发到死信队列,就设置为false*/channel.basicNack(deliveryTag, true, false);//channel.basicReject(deliveryTag,false);}}
}

5 演示 达到最大队列长度,并不要有消费者去消费,多的消息也会到列信队列
注意看第二步故意设成500,而这里故意调成505,多的几条就会到死信队列中
另外如果发送a,b,c,d,e,f  ,而到死信中的是 a,b,c,也就是先发送的消息会弄到死信中

import .huawei.rabbitmqtest1.pojo.User;
import .alibaba.fastjson.JSON;
import .junit.jupiter.api.Test;
import .springframework.amqp.core.Message;
import .springframework.amqp.core.MessageProperties;
import .springframework.amqp.rabbit.connection.CorrelationData;
import .springframework.amqp.rabbit.core.RabbitTemplate;
import .springframework.beans.factory.annotation.Autowired;
import .springframework.boot.test.context.SpringBootTest;
import java.nio.charset.StandardCharsets;
import java.util.UUID;@SpringBootTest
public class Test_3 {@AutowiredRabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法@Testvoid fangfa3() {MessageProperties messageProperties = new MessageProperties();for (int i = 1; i < 505; i++) {User user = new User(i + "", "小明 " + i);//  这个参数是用来做消息的唯一标识//发布消息时使用,存储在消息的headers中CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());Message message = new Message(JSON.toJSONString(user).getBytes(StandardCharsets.UTF_8),messageProperties);rabbitTemplate.convertAndSend("zhengchang_exchange", "chongqing.news", message, correlationData);}}
}

 

更多推荐

死信,三种,队列,演示,长度

本文发布于:2023-05-31 12:01:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/391357.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:死信   三种   队列   演示   长度

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!