1. 概述
ReturnListener用于处理一些不可路由的消息(比如因失误把routeKey或交换机名称写错)。
2. 原生API开启Return消息机制
basicPublish(...)方法的参数mandatory设置为true,当发送消息不可达时,会执行ReturnListener,如果为false则会删除该消息- 添加监听事件
channel.addReturnListener
2.1 代码实现
- 生产者
public static void main(String[] args) throws Exception{
ConnectionFactory connectionFactory=new ConnectionFactory();
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
//设置虚拟主机
connectionFactory.setVirtualHost("/");
//创建一个链接
Connection connection = connectionFactory.newConnection();
//创建channel
Channel channel = connection.createChannel();
String exchangeName="test_return_exchange";
String routeKey="error.test";
String msg="RabbitMQ send message return test!";
//添加Return监听事件
channel.addReturnListener(new ReturnListener() {
@Override
public void handleReturn(int replyCode, String replyText, String exchange, String routingKey,
AMQP.BasicProperties properties, byte[] body) throws IOException {
System.err.println("---------return msg----------");
System.err.println("replyCode: " + replyCode);
System.err.println("replyText: " + replyText);
System.err.println("exchange: " + exchange);
System.err.println("routingKey: " + routingKey);
System.err.println("properties: " + properties);
System.err.println("body: " + new String(body));
}
});
//参数 mandatory:true,当发送消息不可达时,会执行ReturnListener ,如果为false则会删除该消息
channel.basicPublish(exchangeName,routeKey,true,null,msg.getBytes());
}
- 消费者
public static void main(String[] args) throws Exception{
ConnectionFactory connectionFactory=new ConnectionFactory();
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
//设置虚拟主机
connectionFactory.setVirtualHost("/");
//创建链接
Connection connection = connectionFactory.newConnection();
//创建channel
Channel channel = connection.createChannel();
String exchangeName="test_return_exchange";
String exchangeType="topic";
//声明Exchange
channel.exchangeDeclare(exchangeName,exchangeType,true,false,false,null);
String queueName="test_return_queue";
//声明队列
channel.queueDeclare(queueName,true,false,false,null);
String routeKey="return.#";
//绑定队列和交换机
channel.queueBind(queueName,exchangeName,routeKey);
channel.basicConsume(queueName, true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("接收到消息::"+new String(body));
}
});
}
依次运行消费端、生产端,
3.Spring boot项目中ReturnListener
参考如下文章:
Spring AMQP API详解
Spring Boot整合RabbitMQ
RabbitMQ系列文章目录
1、RabbitMQ Windows/CentOS7平台安装手册
2、RabbitMQ中一些重要概念
3、RabbitMQ Exchange类型之Direct Exchange
4、RabbitMQ Exchange类型之Topic Exchange
5、RabbitMQ Exchange类型之fanout Exchange
6、RabbitMQ Exchange类型之headers Exchang
7、Confirm消息确认机制
8、RabbitMQ中ReturnListener的使用
9、RabbitMQ消费端限流
10、ACK确认机制与消息补偿
11、RabbitMQ队列/消息的生存时间(Time-To-Live)
12、RabbitMQ死信队列(Dead Letter Exchanges)
13、Spring AMQP API详解
14、Spring Boot整合RabbitMQ
本文详细介绍RabbitMQ中ReturnListener的使用,包括如何通过原生API和SpringBoot项目配置ReturnListener来处理不可路由消息,提供了生产者和消费者的代码实现。
RabbitMQ中ReturnListener的使用&spm=1001.2101.3001.5002&articleId=103159500&d=1&t=3&u=3a90252ec2294f8cacda1a9ff0ca4b12)
2万+

被折叠的 条评论
为什么被折叠?



