本文只针对 RabbitMQ + spring.xml 发布确认部分配置
1 代码
spring.xml
...
<!-- 工厂连接配置 -->
<!-- 配置发布确认、失败回调 publisher-confirms="true" publisher-returns="true" -->
<rabbit:connection-factory id="connectionFactory" virtual-host="/"
host="${rabbitMq.url}" username="${rabbitMq.username}" password="${rabbitMq.password}"
port="${rabbitMq.port}" publisher-confirms="true" publisher-returns="true"/>
<!-- rabbitTemplate配置 -->
<!-- 配置回调Bean 和 mandatory
mandatory="true"
confirm-callback="rabbitCallbackConfiguration"
return-callback="rabbitCallbackConfiguration" -->
<rabbit:template exchange="messageExchange" id="rabbitTemplate"
connection-factory="connectionFactory" mandatory="true"
confirm-callback="rabbitCallbackConfiguration" return-callback="rabbitCallbackConfiguration"/>
<!-- 配置回调Bean -->
<bean id="rabbitCallbackConfiguration"
class="com.cn.sn.rabbit.configuration.RabbitCallbackConfiguration"/>
...
RabbitCallbackConfiguration.java
package com.cn.sn.rabbit.configuration;
import cn.hutool.json.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.context.annotation.Configuration;
import java.nio.charset.StandardCharsets;
/**
* @auth zk.ren
* @date 2022/7/18.
* <p>
* RabbitMQ 回调监听
*/
@Configuration
public class RabbitCallbackConfiguration implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {
private static final Logger log = LoggerFactory.getLogger(RabbitCallbackConfiguration.class);
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
log.info("confirm correlationData:{} ask:{} acuse:" + cause, correlationData == null ? "" : correlationData.toString(), ack);
}
@Override
public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
log.info("returnedMessage body:{} replyCode:{} replyText:" + replyText + " exchange:" + exchange
+ " routingKey:" + routingKey, (message == null || message.getBody() == null) ? "" : new String(message.getBody(), StandardCharsets.UTF_8), replyCode);
log.info("returnedMessage message:{} ", JSONUtil.toJsonStr(message));
}
}
TestController.java
@Controller
@RequestMapping("/test")
public class TestController {
@Autowired
private RabbitTemplate rabbitTemplate;
@PostConstruct
public void init() {
log.info("rabbitTemplate:{} isNull:{}", rabbitTemplate, null == rabbitTemplate);
}
/**
* CorrelationData 版本 1.6.5.RELEASE 属性只有id
*/
@RequestMapping(value = "rabbitTest", method = RequestMethod.GET)
@ResponseBody
public String rabbitTest(@RequestParam String msg, @RequestParam String route) {
log.info("rabbitTest msg:{}", msg);
Map<String, String> map = new HashMap<>();
map.put("exchange", "test");
route = StringUtils.isNotBlank(route) ? route : "test-loacl";
map.put("routingKey", route);
map.put("body", msg);
rabbitTemplate.convertSendAndReceive("test", route, msg, new CorrelationData(JSONUtil.toJsonStr(map)));
return msg;
}
}
pom.xml
<dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>1.6.5.RELEASE</version> </dependency>
amqp 版本较低 CorrelationData 参数较少 ,后面使用springboot 改为 2.3.11
CorrelationData
参数 变多CorrelationData.class 2.3.11
public class CorrelationData implements Correlation { private final SettableListenableFuture<CorrelationData.Confirm> future = new SettableListenableFuture(); private volatile String id; private volatile ReturnedMessage returnedMessage; }
2 测试
-
正常发送
GET http://127.0.0.1:8080/test/rabbitTest?msg=测试&route=
2022-07-20 16:54:12.810 [http-nio-8080-exec-8] INFO c.j.y.m.controller.TestController-rabbitTest msg:测试
2022-07-20 16:54:13.307 [AMQP Connection 106.52.157.34:5672] INFO c.j.y.m.c.RabbitCallbackConfiguration-confirm correlationData:CorrelationData [id={“body”:“测试”,“exchange”:“test”,“routingKey”:“test-loacl”}] ask:true acuse:null -
发送异常(未配置 routingKey 111)
GET http://127.0.0.1:8080/test/rabbitTest?msg=测试&route=111
2022-07-20 16:57:34.638 [http-nio-8080-exec-10] INFO c.j.y.m.controller.TestController-rabbitTest msg:测试
2022-07-20 16:57:34.713 [AMQP Connection 106.52.157.34:5672] INFO c.j.y.m.c.RabbitCallbackConfiguration-returnedMessage body:测试 replyCode:312 replyText:NO_ROUTE exchange:test routingKey:111
2022-07-20 16:57:34.729 [AMQP Connection 106.52.157.34:5672] INFO c.j.y.m.c.RabbitCallbackConfiguration-returnedMessage message:{“body”:[-26,-75,-117,-24,-81,-107],“messageProperties”:{“headers”:{},“contentLengthSet”:false,“deliveryTag”:0,“priority”:0,“receivedDeliveryMode”:“PERSISTENT”,“replyTo”:“amq.rabbitmq.reply-to.g1hkABZyYWJiaXRAVk0tMTYtMTYtY2VudG9zAAAxAwAAAMdfvxJb.Mp4kpGBIB9PFkEiF5nd99w==”,“contentEncoding”:“UTF-8”,“contentLength”:0,“contentType”:“text/plain”,“deliveryTagSet”:false}}
2022-07-20 16:57:34.730 [AMQP Connection 106.52.157.34:5672] INFO c.j.y.m.c.RabbitCallbackConfiguration-confirm correlationData:CorrelationData [id={“body”:“测试”,“exchange”:“test”,“routingKey”:“111”}] ask:true acuse:null