一、介绍
延迟队列,队列内部是有序的,最重要的特性就体现在他的延迟属性上,延时队列中的元素是希望在指定时间到了或之前取出和处理,简单来说,延时队列就是用来存放需要在指定时间被处理的元素的队列。
二、springboot整合
1、引入相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--RabbitMQ依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!--RabbitMQ测试依赖-->
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、添加相关配置
spring.rabbitmq.host=192.168.xxx.165
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=123
3、添加swagger配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
public Docket webApiConfig(){
return new Docket (DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
.build();
}
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
.title("rabbitmq接口文档")
.description("本文档描述了rabbitmq微服务接口定义")
.version("1.0")
.contact(new Contact("zhangsan","xxx","954544@qq.com"))
.build();
}
}
4、添加rabbitMQ配置类
@Configuration
public class TtlQueueConfig {
//普通交换机名称
public static final String X_EXCHANGE ="X";
//死信交换机名称
public static final String Y_DEAD_LETTER_EXCHANGE ="Y";
//普通队列名称
public static final String QUEUE_A ="QA";
public static final String QUEUE_B ="QB";
//死信队列名称
public static final String DEAD_LETTER_QUEUE ="QD";
//声明xExchange
@Bean("xExchange")
public DirectExchange xExchange(){
return new DirectExchange(X_EXCHANGE);
}
//声明yExchange
@Bean("yExchange")
public DirectExchange yExchange(){
return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
}
//声明队列 ttl为10s
@Bean("queueA")
public Queue queueA(){
HashMap<String, Object> arguments = new HashMap<>(3);
//设置死信交换机
arguments.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
//设置死信routingKey
arguments.put("x-dead-letter-routing-key","YD");
//设置TTL 单位ms
arguments.put("x-message-ttl",10000);
return QueueBuilder.durable(QUEUE_A).withArguments(arguments).build();
}
//声明队列 ttl为10s
@Bean("queueB")
public Queue queueB(){
HashMap<String, Object> arguments = new HashMap<>(3);
//设置死信交换机
arguments.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
//设置死信routingKey
arguments.put("x-dead-letter-routing-key","YD");
//设置TTL 单位ms
arguments.put("x-message-ttl",40000);
return QueueBuilder.durable(QUEUE_B).withArguments(arguments).build();
}
//死信队列
@Bean("queueD")
public Queue queueD(){
return QueueBuilder.durable(DEAD_LETTER_QUEUE).build();
}
//绑定
@Bean
public Binding queueABindingX(@Qualifier("queueA") Queue queueA,
@Qualifier("xExchange") DirectExchange xExchange){
return BindingBuilder.bind(queueA).to(xExchange).with("XA");
}
@Bean
public Binding queueBBindingX(@Qualifier("queueB") Queue queueB,
@Qualifier("xExchange") DirectExchange xExchange){
return BindingBuilder.bind(queueB).to(xExchange).with("XB");
}
@Bean
public Binding queueDBindingY(@Qualifier("queueD") Queue queueD,
@Qualifier("yExchange") DirectExchange xExchange){
return BindingBuilder.bind(queueD).to(xExchange).with("YD");
}
}
5、生产者代码
@Slf4j
@RestController
@RequestMapping("/ttl")
public class SendMessageController {
@Autowired
private RabbitTemplate rabbitTemplate;
//开始发消息
@GetMapping("/sendMsg/{message}")
public void sendMsg(@PathVariable String message){
log.info("当前时间:{},发送一条消息给两个队列:{}",new Date().toString(),message);
rabbitTemplate.convertAndSend("X","XA","消息来自ttl为10s的队列"+message);
rabbitTemplate.convertAndSend("X","XB","消息来自ttl为40s的队列"+message);
}
}
6、消费者代码
Slf4j
@Component
public class DeadLetterQueueConsumer {
//接收消息
@RabbitListener(queues = "QD")
public void receiveD(Message message, Channel channel){
String msg = new String(message.getBody());
log.info("当前时间:{},收到死信队列的消息:{}",new Date(),msg);
}
}
三、延时队列优化
基于上面代码增加第三个普通队列配置,以及修改生产者代码
增加配置
//普通队列名称
public static final String QUEUE_C ="QC";
//声明队列QC
@Bean("queueC")
public Queue queueC(){
Map<String, Object> arguments = new HashMap<>(2);
arguments.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
arguments.put("x-dead-letter-routing-key","YD");
return QueueBuilder.durable(QUEUE_C).withArguments(arguments).build();
}
//绑定
@Bean
public Binding queueCBindingX(@Qualifier("queueC") Queue queueC,
@Qualifier("xExchange") DirectExchange xExchange){
return BindingBuilder.bind(queueC).to(xExchange).with("XC");
}
另写一个生产者
@GetMapping("/sendExpirationMsg/{message}/{ttlTime}")
public void sendMsg(@PathVariable String message,@PathVariable String ttlTime){
log.info("当前时间:{},发送一条时长{}毫秒TTl信息给队列QC:{}",new Date(),ttlTime,message);
rabbitTemplate.convertAndSend("X","XC",message,(msg)->{
//发送消息的时候 延迟时长
msg.getMessageProperties().setExpiration(ttlTime);
return msg;
});
}
以上代码会引发出另外一个问题:就是如果使用在消息属性上设置ttl的方式,消息可能并不会按时“死亡”,因为RabbitMQ只会检查第一个消息是否过期,如果过期则丢到死信队列,如果第一个消息的延时时间很长,而第二个消息延时时间很短,第二个消息并不会得到优先执行。
四、RabbitMQ插件实现延迟队列
1、安装延时队列插件
插件地址:https://www.rabbitmq.com/community-plugins.html
插件名称:rabbitmq_delayed_message_exchange
解压放到RabbitMQ的插件目录。
进入RabbitMQ的安装目录下的plugins目录,执行下面命令让该插件生效,然后重启RabbitMQ(重启之前先执行chmod 644 rabbitmq_delayed_message_exchange-3.8.0.ez 给该文件添加读权限)
/usr/lib/rabbitmq/lib/rabbitmq_server-3.8.8/plugins
rabbitmq-plugins enable rabbitmq_delayed_message_exchange
systemctl restart rabbitmq-server
重启之后即可看到(本质其实作用在了交换机)
![](https://i-blog.csdnimg.cn/blog_migrate/79f00346830a2744323b162cb0292c26.png)
2、添加延时队列配置
public class DelayedQueueConfig {
//交换机
public static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";
//队列
public static final String DELAYED_QUEUE_NAME = "delayed.queue";
//routingKey
public static final String DELAYED_ROUTING_KEY = "delayed.routingKey";
//声明交换机
@Bean
public CustomExchange delayedExchange(){
HashMap<String, Object> arguments = new HashMap<>();
arguments.put("x-delayed-type","direct");
/**
* 参数
* 1、交换机名称
* 2、交换机类型
* 3、是否需要持久化
* 4、是否需要自动删除
* 5、其他参数
*/
return new CustomExchange(DELAYED_EXCHANGE_NAME,"x-delayed-message",
true,false,arguments);
}
//声明队列
@Bean
public Queue delayedQueue(){
return new Queue(DELAYED_QUEUE_NAME);
}
//绑定
@Bean
public Binding delayedQueueBindingDelayedExchange(@Qualifier("delayedQueue") Queue delayedQueue,
@Qualifier("delayedExchange") CustomExchange delayedExchange){
return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(DELAYED_ROUTING_KEY).noargs();
}
}
生产者
//开始发消息 基于插件延时消息
@GetMapping("/sendDelayedMsg/{message}/{delayedTime}")
public void sendMsg(@PathVariable String message,@PathVariable Integer delayedTime){
log.info("当前时间:{},发送一条时长{}毫秒延迟信息给队列delayed.queue:{}",new Date(),delayedTime,message);
rabbitTemplate.convertAndSend(DelayedQueueConfig.DELAYED_EXCHANGE_NAME,
DelayedQueueConfig.DELAYED_EXCHANGE_NAME,message,(msg)->{
//发送消息的时候 延迟时长
msg.getMessageProperties().setDelay(delayedTime);
return msg;
});
}
消费者
@Slf4j
@Component
public class DelayedQueueConsumer {
@RabbitListener(queues = DelayedQueueConfig.DELAYED_QUEUE_NAME)
public void receiveDelayedQueue(Message message, Channel channel){
String msg = new String(message.getBody());
log.info("当前时间:{},收到延迟队列消息:{},",new Date().toString(),msg);
}
}