使用Redisson的RScoredSortedSet实现延时队列

大兔子大兔子 提交于 2019-12-09 23:44:17

1、实现思路

在存储对象时,使用时间戳作为对象的score,score最小的在set的最前面,最先取出

伪代码如下

 RScoredSortedSet<String> set = redissonClient.getScoredSortedSet("simple");
        set.add(System.currentTimeMillis() + 10000,"1");
        set.add(System.currentTimeMillis() + 30000,"2");
        set.add(System.currentTimeMillis() + 20000,"3");

 

2、项目启动时,启动个线程循环获取set中的对象,伪代码如下

package com.ahies.stm.app.init;

import com.ahies.stm.app.centerControl.control.queueHandler.CentralControlQueueHandler;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description
 * @Date 2019/12/9 20:30
 * @Author zsj
 */

@Component
@Order(3)
public class CentralControlQueueRunner implements CommandLineRunner {
    @Autowired
    RedissonClient redissonClient;
    public static ExecutorService queueThreadPool = Executors.newFixedThreadPool(1);
    @Autowired
    CentralControlQueueHandler controlQueueHandler;


    @Override
    public void run(String... args) throws Exception {
        queueThreadPool.execute(controlQueueHandler);

    }
}

 

2、 CentralControlQueueHandler  线程代码如下

package com.ahies.stm.app.centerControl.control.queueHandler;

import com.ahies.stm.app.init.CentralControlQueueRunner;
import lombok.Getter;
import lombok.Setter;
import org.redisson.api.RScoredSortedSet;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description TODO
 * @Date 2019/12/9 20:32
 * @Author zsj
 */
@Getter
@Setter
@Component
public class CentralControlQueueHandler implements Runnable {
    @Autowired
    RedissonClient redissonClient;

    @Override
    public void run() {

        RScoredSortedSet<String> set = redissonClient.getScoredSortedSet("simple");
        while (true) {
            if (set.size() > 0) {
                Double score = set.firstScore().doubleValue();
                long time = System.currentTimeMillis();

                if (time == score) {
                    //当score 等于当前时间戳时   该对象才可被取出
                    String first = set.first();
                    System.out.println(score);
                    //取出对象
                    set.remove(first);
                }
            } else {
//当set中没有数据时 终止循环
                break;
            }

        }

    }
}

 

3、当set中添加新的对象时,启动线程任务,代码如下

package com.ahies.stm.app.centerControl.control.controller;


import com.ahies.stm.app.centerControl.control.queueHandler.CentralControlQueueHandler;
import com.ahies.stm.app.constant.SysConstant;
import com.ahies.stm.app.init.CentralControlQueueRunner;
import com.ahies.stm.app.util.ResponseResult;
import io.netty.channel.Channel;
import org.redisson.api.RScoredSortedSet;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetSocketAddress;
import java.time.LocalDateTime;

/**
 * @Description: 
 */
@RestController
@CrossOrigin
@RequestMapping("/centerControl/control/centralControlr")
public class CentralControlrController {
  

    @Autowired
    CentralControlQueueHandler controlQueueHandler;



    @RequestMapping("/open")
    public  ResponseResult<String> open( ){
        RScoredSortedSet<String> set = redissonClient.getScoredSortedSet("simple");
        set.add(System.currentTimeMillis() + 10000,"1");
        set.add(System.currentTimeMillis() + 30000,"2");
        set.add(System.currentTimeMillis() + 20000,"3");
        //当set中添加对象时 启动线程任务
        CentralControlQueueRunner.queueThreadPool.execute(controlQueueHandler);
        return ResponseResult.dataSuccess(null,SysConstant.QUERY_SUCCESS);
    }






}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!