什么是延时队列?顾名思义:首先它要具有队列的特性,再给它附加一个延迟消费队列消息的功能,也就是说可以指定队列中的消息在哪个时间点被消费。
延时队列在项目中的应用还是比较多的,尤其像电商类平台:
1、订单成功后,在30分钟内没有支付,自动取消订单
2、外卖平台发送订餐通知,下单成功后60s给用户推送短信。
3、如果订单一直处于某一个未完结状态时,及时处理关单,并退还库存
4、淘宝新建商户一个月内还没上传商品信息,将冻结商铺等
遗憾的是,RabbitMQ中并未提供延迟队列特性,但我们可以利用RabbitMQ死信队列超时转发的特性间接实现。
Dead Letter Exchanges(DLX)
DLX即死信交换机,绑定在死信交换机上的即死信队列。RabbitMQ的 Queue(队列)可以配置两个参数x-dead-letter-exchange 和 x-dead-letter-routing-key(可选),一旦队列内出现了Dead Letter(死信),则按照这两个参数可以将消息重新路由到另一个Exchange(交换机),让消息重新被消费。
队列出现Dead Letter的情况有:
● 消息或者队列的TTL过期
● 队列达到最大长度
● 消息被消费端拒绝(basic.reject or basic.nack)
RabbitUtils
package com.itlaoqi.rabbitmq.common;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class RabbitUtils {
private static ConnectionFactory connectionFactory = new ConnectionFactory();
static {
connectionFactory.setHost("192.168.31.202"); //RabbitMQ服务器IP
connectionFactory.setPort(5672);//5672是RabbitMQ的默认端口号
connectionFactory.setUsername("admin"); //用户名
connectionFactory.setPassword("admin"); //密码
connectionFactory.setVirtualHost("/"); //vhost
}
public static Connection getConnection(){
Connection conn = null;
try {
conn = connectionFactory.newConnection();
return conn;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Provider
package com.itlaoqi.rabbitmq;
import com.itlaoqi.rabbitmq.common.RabbitUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;
@Slf4j
public class Provider {
public static void main(String[] args) throws IOException, TimeoutException {
//创建RabbitMQ连接
Connection connection = RabbitUtils.getConnection();
//创建通信频道
Channel channel = connection.createChannel();
//创建普通交换机(订阅发布fanout)
channel.exchangeDeclare("example-exchange", "fanout", true);
//普通队列参数
HashMap<String, Object> queueArgs = new HashMap<>();
//队列内TTL最大10秒,等待超时后送入x-dead-letter-exchange死信交换机
queueArgs.put("x-message-ttl", 10000);
//设置死信交换机名称
queueArgs.put("x-dead-letter-exchange", "dlx-exchange");
//创建不同队列example-queue
channel.queueDeclare("example-queue", true, false, false, queueArgs);
//将普通队列与普通交换机绑定
channel.queueBind("example-queue", "example-exchange", "");
//创建死信交换机dlx-exchange
channel.exchangeDeclare("dlx-exchange", "fanout", true);
//创建死信队列dlx-queue
channel.queueDeclare("dlx-queue", true, false, false, new HashMap<>());
//将死信交换机与死信队列进行绑定
channel.queueBind("dlx-queue", "dlx-exchange", "");
//循环发送10条数据
for(int i = 0 ; i < 10 ; i++) {
String time = String.valueOf(new Date().getTime());
channel.basicPublish("example-exchange", "", null, time.getBytes());
log.info(time + "消息已发送");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
channel.close();
connection.close();
}
}
DelayConsumer
package com.itlaoqi.rabbitmq;
import com.itlaoqi.rabbitmq.common.RabbitUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.Date;
//死信(延迟)队列消费者
public class DelayConsumer {
public static void main(String[] args) throws IOException {
Connection connection = RabbitUtils.getConnection();
final Channel channel = connection.createChannel();
//channel.basicQos(1);//设置限流(可选)
//消费延迟消息
channel.basicConsume("dlx-queue" , false , new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("收到消息信息(" + new Date().getTime() + "):" + new String(body));
//手动应答
channel.basicAck(envelope.getDeliveryTag() , false);
}
});
System.out.println("延迟队列消费者启动成功");
}
}