思路
利用redis的zset数据结构
Zset本质就是Set结构上加了个排序的功能,除了添加数据value之外,还提供另一属性score,所以我们把时间放在score上 新开一个线程不断去轮询比对。、
代码
/**
* @Auther: wwh
* @Date: 2019-03-29 11:06
* @Description:
*/
public class RedisDelayTest {
private static final String ADDR = "127.0.0.1";
private static final int PORT = 6379;
private static JedisPool jedisPool = new JedisPool(ADDR, PORT);
private static CountDownLatch cdl = new CountDownLatch(10);
public static Jedis getJedis() {
return jedisPool.getResource();
}
/**
*
* @date: 2019-03-29 13:01
* @param: * @param delayTime: 延迟多少秒执行
* @return: * @return: void
* @author: wwh
* @Description: 添加延迟任务
*/
public void addDelayMsg(int delayTime) {
Calendar instance = Calendar.getInstance();
instance.add(Calendar.SECOND, delayTime);
RedisDelayTest.getJedis().zadd("taskId", (instance.getTimeInMillis()) / 1000, "" + delayTime);
System.out.println("添加延迟任务: 当前时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
System.out.println((delayTime) + "秒后执行");
}
public static void consumerDelayMessage() {
Jedis jedis = RedisDelayTest.getJedis();
while (true) {
Set<Tuple> taskSet = jedis.zrangeWithScores("taskId", 0, 0);
if (taskSet == null || taskSet.isEmpty()) {
try {
TimeUnit.MICROSECONDS.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
}
Tuple tuple = (Tuple) taskSet.toArray()[0];
double score = tuple.getScore();
Calendar instance = Calendar.getInstance();
long nowTime = instance.getTimeInMillis() / 1000;
if (nowTime >= score) {
String element = tuple.getElement();
Long taskId = jedis.zrem("taskId", element);
if (taskId > 0) {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ":redis消费了一个任务:消费的订单taskId为" + element);
}
}
}
}
static class DelayMessage implements Runnable{
@Override
public void run() {
try {
cdl.await();
consumerDelayMessage();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
RedisDelayTest delayTest = new RedisDelayTest();
delayTest.addDelayMsg(5);
for (int i = 0; i < 10; i++) {
new Thread(new DelayMessage()).start();
cdl.countDown();
}
}
}
缺点
不支持分布式和集群。
最好还是用mq实现延迟队列。
参考
https://my.oschina.net/u/3266761/blog/1930360