基于Spring Schedule和redis锁互斥定时任务

定时任务DemoSchedule


public class DemoSchedule implements SchedulingConfigurer{
    private final Logger logger = LoggerFactory.getLogger(DemoSchedule.class);
    private static final String cron= "0 0 02 * * ?";
    @Autowired
    private DistributedLock redisLock;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(new Task(redisLock),new DemoTrigger());
    }

    private static class Task implements Runnable{
        
        private DistributedLock redisLock;
        
        public Task(DistributedLock redisLock;){
            this.redisLock= redisLock;
        }
       
        @Override
        public void run() {
            boolean lockSuccess = redisLock.tryGetLock("demoId", 5000L * 60);
            if(! lockSuccess) {
                logger.info("demoId has started at other point...");
                return;
            }
            delayHandler();
        }

        private void delayHandler(){
           //do something
        }
    }

    private class DemoTrigger implements Trigger{
        @Override
        public Date nextExecutionTime(TriggerContext triggerContext) {
            CronTrigger trigger = new CronTrigger(cron);
            Date nextExec = trigger.nextExecutionTime(triggerContext);
            logger.debug("cron now:[{}]", cron);
            return nextExec;
        }
    }
}

redis分布式锁


public class DistributedLock {

    public static final Logger logger = LoggerFactory.getLogger(DistributedLock.class);

    public static final String BILL_PREFIX = "locked_bill_";

    private final String LOCK_VALUE = "lock_value";

    @Autowired
    private RedisTemplate redisTemplate;

    private DistributedLock() {}

    /**
     * 尝试获取分布式锁
     * @param lockKey 锁
     * @param expireTime 超期时间
     * @return 是否获取成功
     */
    public synchronized boolean tryGetLock(String lockKey, Long expireTime) {
        try {
            /*该方法会在没有key时,设置key;存在key时返回false;因此可以通过该方法及设置key的有效期,判断是否有其它线程持有锁*/
            Boolean success = redisTemplate.opsForValue().setIfAbsent(lockKey, LOCK_VALUE);
            if(success != null && success) {
                redisTemplate.expire(lockKey, expireTime, TimeUnit.MILLISECONDS);
                return true;
            }
            return false;
        } catch (Exception e) {
            logger.error("[tryGetDistributedLock] error", e);
            return false;
        }
    }

    /**
     * 释放分布式锁
     * @param lockKey 锁
     * @return 是否释放成功
     */
    public boolean releaseLock(String lockKey) {
        try {
            redisTemplate.delete(lockKey);
        } catch (Exception e) {
            logger.error("[releaseDistributedLock] error", e);
            return false;
        }
        return true;
    }
}

你可能感兴趣的:(基于Spring Schedule和redis锁互斥定时任务)