在rabbitmq中如何确认消息发送到交换机上以及是否正常交换机转发到消息队列上,在rabbitmq上均有其确认实现接口。
- confirm-callback :消息确认机制,也就是消息是否正常发送到交换机的回调,内部可以实现正常与失败的区分处理。
- return-callback:消息的失败回调处理,也就是消息通过交换机时没有正常转发到消息队列中时触发。
代码实现参考:
- pom文件
- springboot的application.properties
- spring资源文件spring/spring-rabbitmq.xml 以及编写确认回调和失败huidiao
- springboot 启动类编写
- web模块Controller编写(测试使用)
- 访问测试
一、pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xiaohui.mqproducer</groupId>
<artifactId>MqProducer</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
</project>
二、springboot的application.properties
#端口
server.port=8888
#rabbitMq配置
spring.rabbitmq.host=172.18.255.54
spring.rabbitmq.port=5672
spring.rabbitmq.virtual-host=/myhost
spring.rabbitmq.username=xiaohui
spring.rabbitmq.password=root
三、spring资源文件spring/spring-rabbitmq.xml
再该资源文件中指明了消息的确认回调处理类以及消息失败回调的处理类,两个类分别集成了RabbitTemplate.ReturnCallback、RabbitTemplate.ConfirmCallback接口,并做简单的逻辑实现。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<!-- xml可以使用$占位符 -->
<context:property-placeholder location="classpath*:application.properties" />
<!-- publisher-confirms="true" 表示消息开启确认机制 -->
<rabbit:connection-factory id="connectionFactory"
host="${spring.rabbitmq.host}"
port="${spring.rabbitmq.port}"
username="${spring.rabbitmq.username}"
password="${spring.rabbitmq.password}"
virtual-host="${spring.rabbitmq.virtual-host}"
publisher-confirms="true"
publisher-returns="true"/>
<!-- 消息确认回调处理类 -->
<bean id="confirmCallback" class="com.xiaohui.rabbitmq.service.MsgSendConfirmCallBack" />
<!-- 消息的回调 -->
<bean id="sendReturnCallback" class="com.xiaohui.rabbitmq.service.MsgSendReturnCallBack" />
<!-- 定义消息发送类,并指定回调处理对象 以及连接工厂信息
confirm-callback: 消息是否成功送达交换机
return-callback:失败返回回调,需要同时配置 mandatory="true"
-->
<rabbit:template id="rabbitTemplate"
connection-factory="connectionFactory"
confirm-callback="confirmCallback"
return-callback="sendReturnCallback"
mandatory="true" />
</beans>
消息确认回调类 MsgSendConfirmCallBack.java
package com.xiaohui.rabbitmq.service;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
public class MsgSendConfirmCallBack implements RabbitTemplate.ConfirmCallback {
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
if(ack){
System.out.println("消息确认成功");
}else{
System.out.println("消息确认失败"+cause);
}
}
}
MsgSendReturnCallBack.java
package com.xiaohui.rabbitmq.service;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
public class MsgSendReturnCallBack implements RabbitTemplate.ReturnCallback {
@Override
public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
String msgJson = new String(message.getBody());
System.out.println("return msg:"+msgJson);
}
}
四、springboot 启动类编写
package com.xiaohui.rabbitmq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
/**
* 生产者启动类
*/
@SpringBootApplication
@ImportResource({"classpath:spring/spring-rabbitmq.xml"})
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class,args);
}
}
五、web模块Controller编写(测试使用)
package com.xiaohui.rabbitmq.controller;
import com.xiaohui.rabbitmq.config.RabbitMqConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MsgController {
@Autowired
RabbitTemplate rabbitTemplate;
@GetMapping("/sendConfirmMsg")
public String sendConfirmMsg(@RequestParam String msg,@RequestParam String key){
rabbitTemplate.convertAndSend(key,msg);
return "消息发送成功!";
}
@GetMapping("/sendReturnMsg")
public String sendReturnMsg(@RequestParam String msg,@RequestParam String exchange,@RequestParam String key){
rabbitTemplate.convertAndSend(exchange,key,msg);
return "消息发送成功!";
}
}
六、访问测试
启动项目之后,我们分别进行交换机的成功发送与失败发送,在进行交换机正常消息队列的转发与失败转发查看其现象。
测试1:我们传入了一个不存在的交换机名,可以看到在消息确认回调类 MsgSendConfirmCallBack.java中执行了其逻辑,单并未开始涉及到交换机消息发送失败确认逻辑。
测试2:正确的交换机,错误的路由key。可以看到,消息确认逻辑正常监听执行打印,消息失败监听处理逻辑中也监听到了消息内容。
测试3:正确的交换机、正确的路由key。只有消息确认监听执行了回调信息。
示例代码的工作结构如下(划掉的为不涉及的文件):