1.需求
利用rabbitmq的死信交换机构建延时队列,在容器启动的时候往死信交换机发送一条消息,一分钟后监听者接收到消息并消费。
2.依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
3.配置文件
spring.rabbitmq.host=121.199.31.160
spring.rabbitmq.port=5672
spring.rabbitmq.username=root
spring.rabbitmq.password=root
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.publisher-returns=true
#默认情况下消息消费者是自动确认消息的,如果要手动确认消息则需要修改确认模式为manual
spring.rabbitmq.listener.simple.cknowledge-mode=manual
# 消费者每次从队列获取的消息数量。此属性当不设置时为:轮询分发,设置为1为:公平分发
spring.rabbitmq.listener.simple.prefetch=1
4.编写配置类
/**
* @author yhd
* @createtime 2021/1/22 14:37
*/
@SpringBootApplication
public class DelayConfig {
//延时交换机
public static final String EXCHANGE_DIRECT_ORDER_CANCEL = "spring.boot.test.delay.exchange";
//路由键
public static final String ROUTING_ORDER_CANCEL = "spring.boot.test.delay.routing";
//延迟队列
public static final String QUEUE_ORDER_CANCEL = "spring.boot.test.delay.queue";
// 延迟时间 单位:秒
public static final int DELAY_TIME = 60;
@Bean //声明死信队列
public Queue delayQueue() {
// 第一个参数是创建的queue的名字,第二个参数是是否支持持久化
return new Queue(QUEUE_ORDER_CANCEL, true);
}
@Bean //声明私信交换机
public CustomExchange delayExchange() {
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-delayed-type", "direct");
return new CustomExchange(EXCHANGE_DIRECT_ORDER_CANCEL, "x-delayed-message", true, false, args);
}
@Bean //死信交换机绑定死信队列并设置路由键
public Binding bindingDelay() {
return BindingBuilder.bind(delayQueue()).to(delayExchange()).with(ROUTING_ORDER_CANCEL).noargs();
}
}
5.编写消息发送方和接收方
/**
* @author yhd
* @createtime 2021/1/22 14:34
* 测试springboot整合mq利用死信队列发送消息并接收
*/
@Component
@Slf4j
public class SpringBootDelayQueueTest {
@Resource
private RabbitTemplate rabbitTemplate;
@Resource
private AmqpTemplate amqpTemplate;
/**
* 发送消息
*/
public void sendMessage() {
amqpTemplate.convertAndSend(
DelayConfig.EXCHANGE_DIRECT_ORDER_CANCEL,DelayConfig.ROUTING_ORDER_CANCEL,
"try send message to delay queue !",
msg -> {
msg.getMessageProperties().setDelay(DelayConfig.DELAY_TIME * 1000);
return msg;
});
}
/**
* 接收消息
*/
@RabbitListener(queues = DelayConfig.QUEUE_ORDER_CANCEL)
public void receiveMessage(String msg, Message message, Channel channel) throws Exception {
log.info("the delaty queue received message : {}", msg);
//log.info("the delaty queue received message : {}", new String(message.getBody()));
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
}
}
6.容器启动发送消息
/**
* @author yhd
* @createtime 2021/1/22 14:56
*/
@Component
@Slf4j
public class BootMq implements ApplicationRunner {
@Resource
private SpringBootDelayQueueTest springBootDelayQueueTest;
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("发送私信");
springBootDelayQueueTest.sendMessage();
}
}