rabbitmq生产者端确认消息
测试场景:设置publisher-confirm-type消息确认模式,测试回调函数。
yml配置:
spring:
rabbitmq:
addresses: 127.0.0.1
port: 5672
username: guest
password: guest
virtual-host: /
#客户端接收消息配置
listener:
simple:
#最小并发
concurrency: 5
#最大并发
max-concurrency: 10
#接收消息方式 manual:手动 auto:自动 none: 不配置
acknowledge-mode: manual
#并发下欲取数据条数,1:每次取一条manual
prefetch: 1
retry:
enabled: true
max-attempts: 5
max-interval: 100000
#消息确认模式
publisher-confirm-type: simple
#可以确保消息在未被队列接收时返回,而不是丢弃
publisher-returns: true
connection-timeout: 5000
当设置publisher-confirm-type:simple 时,就是开启了生产者端发送消息后确认消息模式。
生产者:
package com.test.topic.producer;
import com.test.topic.entity.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
/**
* @author: xiaobai
* @date: 2021/3/16 11:15
* @version:V1.0
* @description:消息生产者
*/
@RestController
@Slf4j
public class SendOrder {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("/topicSend")
public void testSend(){
Order order = new Order();
order.setId(2020031600000001L);
order.setName("测试订单1");
order.setMessageId(System.currentTimeMillis() + "$" + UUID.randomUUID().toString());
send(order);
}
/**
* 回调函数:confirm 确认消息发送成功
*/
final RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback(){
/**
*
* @param correlationData 消息唯一id
* @param b true: 消息发送成功,可以处理接下来的业务逻辑...
* false: 消息发送失败,则将这条消息在数据库中标记为失败,以便后面重试
* @param s
*/
@Override
public void confirm(CorrelationData correlationData, boolean b, String s) {
log.info("message id:" + correlationData);
String messageId = correlationData.getId();
if(b){
log.info("标记messageID为"+messageId+"的消息为成功状态");
}else{
log.error("消息发送失败...进行后续补偿处理");
}
}
};
public void send(Order order){
rabbitTemplate.setConfirmCallback(confirmCallback);
CorrelationData correlationData = new CorrelationData();
correlationData.setId(order.getMessageId());
rabbitTemplate.convertAndSend("order-exchange","order.1",order,correlationData);
log.info("消息发送成功:" +order.toString());
}
}
消费者:
package com.test.topic.consumer;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.test.topic.entity.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
/**
* @author: xiaobai
* @date: 2021/3/16 14:10
* @version:V1.0
* @description:
*/
@Component
@Slf4j
public class OrderReceiver {
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 笨方法
*/
public void consumer1(){
Object o = rabbitTemplate.receiveAndConvert("order-queue");
System.out.println("接收到的消息:" + o.toString());
}
/**
* 由于设置了手动获取消息,必须要手动basicAck
* RabbitListener 设置后可以自动创建exchange 和queue 并且创建绑定关系
* @param order
* @param headers
* @param channel
*/
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "order-queue",durable = "true"),
exchange = @Exchange(value = "order-exchange",durable = "true",type = "topic"),
key = "order.#"
)
)
@RabbitHandler
public void consumer(@Payload Order order, @Headers Map<String,Object> headers, Channel channel) throws IOException {
log.info("消费者开始消费消息......" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
log.info("订单id:" + order.getId());
Long delivery_tag = (Long)headers.get(AmqpHeaders.DELIVERY_TAG);
channel.basicAck(delivery_tag,false);
throw new RuntimeException();
}
}
执行情况:
2021-04-27 14:37:05.653 INFO 9812 --- [nio-8080-exec-1] com.test.topic.producer.SendOrder : 消息发送成功:Order(id=2020031600000001, name=测试订单1, messageId=1619505425637$ec446154-2077-46ea-a3d3-ffc9f6effb58)
2021-04-27 14:37:05.655 INFO 9812 --- [ntContainer#2-5] com.test.topic.consumer.OrderReceiver : 消费者开始消费消息......2021-04-27 14:37:05
2021-04-27 14:37:05.656 INFO 9812 --- [ntContainer#2-5] com.test.topic.consumer.OrderReceiver : 订单id:2020031600000001
2021-04-27 14:37:05.662 INFO 9812 --- [nectionFactory5] com.test.topic.producer.SendOrder : message id:CorrelationData [id=1619505425637$ec446154-2077-46ea-a3d3-ffc9f6effb58]
2021-04-27 14:37:05.662 INFO 9812 --- [nectionFactory5] com.test.topic.producer.SendOrder : 标记messageID为1619505425637$ec446154-2077-46ea-a3d3-ffc9f6effb58的消息为成功状态
此处表示启用消息确认模式,可以确定生产者消息一定发送成功, 如果不成功,可以做其他处理。
当设置publisher-confirm-type:none时,则不会调用回调函数confirmCallback。大家可以自行测试。