redis实现延时队列

思路:
1.使用redis中zset数据结构   
2.使用score排序,score为过期时间点   
3.启动线程不断取出排序第一个,比较score和当前时间点,如果score小于或等于当前时间,说明此数据过期,需要处理  
4.处理完毕在zset中移除
//初始化jedis
private static JedisPool jedisPool = new JedisPool(new GenericObjectPoolConfig(), "127.0.0.1",
        6380, 10000, "mima", 1);

public static Jedis getJedis() {
    return jedisPool.getResource();
}

@Async
@Scheduled(cron = "0 * * * * ?")
public void WODeposit() throws InterruptedException {

        List<String> woList = new ArrayList<String>();

        woList.add("00 01");
        woList.add("00 02");
        woList.add("00 03");
        woList.add("00 04");

        for (int i = 0; i < woList.size(); i++) {
            //延迟5秒
            Calendar cal1 = Calendar.getInstance();
            cal1.add(Calendar.SECOND, 5);
            int second3later = (int) (cal1.getTimeInMillis() / 1000);
            Jedis jedis = RedisDelayTask.getJedis();
           
            jedis.zadd("worderList", second3later, woList.get(i));
            System.out.println(new Date() + "存入命令:" + woList.get(i));
            //关闭jedis,存在低版本无法关闭的情况
            jedis.close();
        }

}


//消费命令
@Async
@Scheduled(cron = "0/10 * * * * ?")
public void WOTakeOut() throws InterruptedException {
    Jedis jedis = RedisDelayTask.getJedis();
    Set<Tuple> items = jedis.zrangeWithScores("worderList", 0, 1);
    if (items == null || items.isEmpty()) {
        try {
            Thread.sleep(500);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } else {
        int score = (int) ((Tuple) items.toArray()[0]).getScore();
        Calendar cal = Calendar.getInstance();
        int nowSecond = (int) (cal.getTimeInMillis() / 1000);
        if (nowSecond >= score) {
            String orderId = ((Tuple) items.toArray()[0]).getElement();
            Long num = jedis.zrem("worderList", orderId);
            if (num != null && num > 0) {
                System.out.println(new Date() + "取出命令:" + orderId);
            }
        }
    }
    //关闭jedis
    jedis.close();
}

关键点:jedis.close(); jedis的版本依赖,低版本存在无法close的情况

<!-- 添加redis依赖 -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.3</version>
</dependency>

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.3.2</version>
</dependency>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值