走路自带风. 2022-05-31 22:51 采纳率: 80.4%
浏览 31
已结题

RabbitMQ延迟队列的相关问题

问题遇到的现象和发生背景

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秒的信息,

  • 写回答

1条回答 默认 最新

  • 皮蛋不吃粥 2022-06-01 08:33
    关注

    把贴出来的代码格式化一下

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月2日
  • 创建了问题 5月31日

悬赏问题

  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题