Ideas
Using redis's zset data structure The
essence of Zset is to add a sorting function to the Set structure. In addition to adding data value, it also provides another attribute score, so we put time on the score and open a new thread to continuously poll the ratio Correct. ,
Code
/**
* @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();
}
}
}
Disadvantages
Does not support distributed and clustered.
It is better to use mq to implement the delay queue.
reference
https://my.oschina.net/u/3266761/blog/1930360