赞
踩
1.基本处理流程
@RequestMapping("/direct")
public Object sendEmail(String msg) {
try {
rabbitTemplate.convertAndSend("exchange.direct.springboot.email", "queue.email.routing.key", msg);
return msg;
} catch (AmqpException e) {
System.out.println("发送出现异常:" + e.getMessage());
return "网络中断,请稍后再试";
}
}
3.模拟无交换器异常
ERROR 4880 — [.200.57.39:5672] o.s.a.r.c.CachingConnectionFactory : Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange ‘noExchange’ in vhost ‘/’, class-id=60, method-id=40)
4.模拟无路由异常
1. 因为消息是异步发送,所以需要确保消息能正确发送
2. 所以可配置RabbitTemplate然后指定回调信息
3. 步骤01:修改配置文件,配置回调参数
publisher-confirm-type
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: tianxin
password: tianxin
# 开启消息发broker回调
publisher-confirm-type: correlated
# 开启路由消息路由回调
publisher-returns: true
# 强制确认,也可以在代码中开启
template:
mandatory: true
/** * The type of publisher confirms to use. */ public enum ConfirmType { /** * Use {@code RabbitTemplate#waitForConfirms()} (or {@code waitForConfirmsOrDie()} * within scoped operations. */ SIMPLE, /** * Use with {@code CorrelationData} to correlate confirmations with sent * messsages. */ CORRELATED, /** * Publisher confirms are disabled (default). */ NONE }
4.步骤02:配置RabbitTemplate,设置交换器确认回调和路由回调
setConfirmCallback:无论成功与否都会调用
setReturnCallback:错误时才调用
import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Objects; @Configuration public class CustomRabbitTemplate { @Bean public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { RabbitTemplate rabbitTemplate = new RabbitTemplate(); // 开启mandatory为true才能触发回调方法,无论消息推送结果如何强制调用回调方法 rabbitTemplate.setMandatory(true); // 设置连接工厂信息 rabbitTemplate.setConnectionFactory(connectionFactory); // 消息发broker回调:发送者到broker的exchange是否正确找到 rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> { System.out.println("setConfirmCallback 消息数据:" + correlationData); if (Objects.nonNull(correlationData)) { System.out.println("setConfirmCallback 消息数据:" + correlationData.getReturnedMessage()); } System.out.println("setConfirmCallback 消息确认:" + ack); System.out.println("setConfirmCallback 原因:" + cause); System.out.println("-----------------------------------"); }); // 消息路由回调:从交换器路由到队列是否正确发送 rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> { System.out.println("setReturnCallback 消息:" + message); System.out.println("setReturnCallback 回应码:" + replyCode); System.out.println("setReturnCallback 回应信息:" + replyText); System.out.println("setReturnCallback 交换器:" + exchange); System.out.println("setReturnCallback 路由键:" + routingKey); System.out.println("-----------------------------------"); }); return rabbitTemplate; } }
/**
* A callback for publisher confirmations.
*
*/
@FunctionalInterface
public interface ConfirmCallback {
/**
* Confirmation callback.
* @param correlationData correlation data f
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。