SpringBoot 消息队列RabbitMQ使用延迟消息插件 接收延迟消息
介绍
需要确保MQ已经安装好了延迟插件。
创建延迟交换机与队列
@RabbitListener(bindings = @QueueBinding(value = @Queue(value = "test.queue",durable = "true"),exchange = @Exchange(value = "test.fanout",delayed = "true"),key = {"a","b"}))public void a(String str){System.out.println(str);}
delayed =“true” 表示为延迟消息
发送消息
private final RabbitTemplate rabbitTemplate;@GetMapping("/putRecord")
public Result userMessage(){rabbitTemplate.convertAndSend("test.fanout", "a", "发送的消息", new MessagePostProcessor() {@Overridepublic Message postProcessMessage(Message message) throws AmqpException {message.getMessageProperties().setDelay(1000*5); //设置延迟时间return message;}});return Result.success("操作成功",null);
}