赞
踩
死信交换机有什么用呢? 在创建队列的时候 可以给这个队列附带一个交换机, 那么这个队列作废的消息就会被重新发到附带的交换机,然后让这个交换机重新路由这条消息。
通俗的说,就是消息产生之后,因为设置了超时时间,在这段时间内消息没有被消费就会被扔到死信队列里面。
// 交换机名称 private static final String DESTINATION_NAME = "rabbitMq_topic"; //消息队列 private static final String queueName = "topic_queue"; //routingKey private static final String routingKey = "topic.#"; //配置死信队列 private static final String dlxExchangeName = "dlx.exchange"; private static final String dlxQueueName = "dlx.queue"; private static final String dlxRoutingKey = "#"; @Test public void producer() throws IOException, TimeoutException { //获取连接 Connection connection = MQConnectionUtils.newConnection(); //创建通道 Channel channel = connection.createChannel(); Map<String, Object> arguments = new HashMap<String, Object>(16); // 为队列设置队列交换器 arguments.put("x-dead-letter-exchange", dlxExchangeName); // 设置队列中的消息 60s 钟后过期 arguments.put("x-message-ttl", 60000); //正常生产者绑定交换机 参数1 交换机名称 参数2 交换机类型 channel.exchangeDeclare(DESTINATION_NAME, "topic", true, false, null); //消费声明队列 channel.queueDeclare(queueName, true, false, false, arguments); //消费者队列绑定交换机 绑定路由件 路由键 channel.queueBind(queueName, DESTINATION_NAME, routingKey); String message = new
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。