问题遇到的现象和发生背景
RabbitMQ延迟队列的相关问题
问题相关代码,请勿粘贴截图
配置类
package com.atguigu.rabbitmq.configer;
import lombok.extern.slf4j.Slf4j;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@Slf4j
@RequestMapping("ttl")
@RestController
public class SendMsgController {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("sendMsg/{message}")
public void sendMsg(@PathVariable String message){
log.info("当前时间:{},发送一条信息给两个 TTL 队列:{}", new Date(), message);
rabbitTemplate.convertAndSend("X", "XA", "消息来自 ttl 为 10S 的队列: "+message);
rabbitTemplate.convertAndSend("X", "XB", "消息来自 ttl 为 40S 的队列: "+message);
}
}
package com.atguigu.rabbitmq.configer;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Date;
死信接收队列类
@Slf4j
@Component
public class DeadLetterQueueConsumer {
@RabbitListener(queues = "QD")
public void receiveD(Message message, Channel channel) throws IOException {
String msg = new String(message.getBody());
log.info("当前时间:{},收到死信队列信息{}", new Date().toString(), msg);
}
}
生产发送消息类
package com.atguigu.rabbitmq.configer;
import lombok.extern.slf4j.Slf4j;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@Slf4j
@RequestMapping("ttl")
@RestController
public class SendMsgController {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("sendMsg/{message}")
public void sendMsg(@PathVariable String message){
log.info("当前时间:{},发送一条信息给两个 TTL 队列:{}", new Date(), message);
rabbitTemplate.convertAndSend("X", "XA", "消息来自 ttl 为 10S 的队列: "+message);
rabbitTemplate.convertAndSend("X", "XB", "消息来自 ttl 为 40S 的队列: "+message);
}
}
运行结果及报错内容
2022-05-31 22:38:45.790 INFO 78776 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-05-31 22:38:45.790 INFO 78776 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-05-31 22:38:45.790 INFO 78776 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 0 ms
2022-05-31 22:38:45.819 INFO 78776 --- [nio-8080-exec-1] c.a.rabbitmq.configer.SendMsgController : 当前时间:Tue May 31 22:38:45 CST 2022,发送一条信息给两个 TTL 队列:111
2022-05-31 22:38:55.837 INFO 78776 --- [ntContainer#0-1] c.a.r.configer.DeadLetterQueueConsumer : 当前时间:Tue May 31 22:38:55 CST 2022,收到死信队列信息消息来自 ttl 为 10S 的队列: 111
2022-05-31 22:39:25.830 INFO 78776 --- [ntContainer#0-1] c.a.r.configer.DeadLetterQueueConsumer : 当前时间:Tue May 31 22:39:25 CST 2022,收到死信队列信息消息来自 ttl 为 40S 的队列: 111
2022-05-31 22:39:35.832 INFO 78776 --- [ntContainer#0-1] c.a.r.configer.DeadLetterQueueConsumer : 当前时间:Tue May 31 22:39:35 CST 2022,收到死信队列信息消息来自 ttl 为 10S 的队列: 111
2022-05-31 22:39:35.833 INFO 78776 --- [ntContainer#0-1] c.a.r.configer.DeadLetterQueueConsumer : 当前时间:Tue May 31 22:39:35 CST 2022,收到死信队列信息消息来自 ttl 为 40S 的队列: 111
就是在第一次发完10秒和40秒后又会发送一次
还有就是我只对routingkey为XA的发送,运行结果有50秒的信息,