Commit b630b9af authored by zhangwanglin's avatar zhangwanglin

定时任务

parent a27ca066
package com.hungraim.ltc.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
/**
* @author zwl
* @ClassName RedisLock
* @description
* @date 2023/3/19 22:04
**/
@Component
public class RedisLock {
@Autowired
private RedisTemplate redisTemplate;
/**
* 查询
*
* @param key 键
* @return 值
*/
public Object get(Object key) {
return redisTemplate.opsForValue().get(key);
}
/**
* 保存
*
* @param key 键
* @param value 值
* @param expire 失效时长
* @param unit 失效时长的单位
*/
public void set(Object key, Object value, long expire, TimeUnit unit) {
if (expire > 0) {
redisTemplate.opsForValue().set(key, value, expire, unit);
}
}
/**
* 线程等待
*
* @param millis
*/
private void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
// do nothing
}
}
/**
* 尝试加锁
*
* @param key
* @param expireMilliseconds 锁超时时长,单位:毫秒
* @param retryTimes 重试次数,每秒重试 1 次
* @return
*/
public boolean tryLock(Object key, long expireMilliseconds, long retryTimes) {
for (long i = 1; i <= retryTimes; i++) {
if (tryLock(key, expireMilliseconds)) {
return true;
} else if (i != retryTimes) {
sleep(1000);
}
}
return false;
}
/**
* 尝试加锁
*
* @param key
* @param expireMilliseconds 锁超时时长,单位:毫秒
* @return
*/
public boolean tryLock(Object key, long expireMilliseconds) {
String expireAt = String.valueOf(System.currentTimeMillis() + expireMilliseconds + 1);
Boolean acquire = redisTemplate.opsForValue().setIfAbsent(key, expireAt, expireMilliseconds, TimeUnit.MILLISECONDS);
if (acquire != null) {
return acquire;
} else {
return false;
}
}
/**
* 删除锁
*
* @param key
* @return
*/
public boolean delLock(Object key) {
return redisTemplate.delete(key);
}
/**
* 删除缓存
*
* @param key
* @return
*/
public boolean delete(Object key) {
return redisTemplate.delete(key);
}
}
package com.hungraim.ltc.governance.controller; package com.hungraim.ltc.governance.controller;
import com.hungraim.ltc.governance.service.SrvTaskService; import com.hungraim.ltc.governance.service.SrvTaskService;
import com.hungraim.ltc.redis.RedisLock;
import com.hungraim.ltc.util.DateUtils; import com.hungraim.ltc.util.DateUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -22,17 +23,25 @@ public class SrvTaskScheduledController { ...@@ -22,17 +23,25 @@ public class SrvTaskScheduledController {
@Autowired @Autowired
private SrvTaskService srvTaskService; private SrvTaskService srvTaskService;
@Scheduled(cron = "0 50 20 * * ?") @Autowired
public void timeAllocationSrvTask() { private RedisLock redisLock;
log.info("timeAllocationSrvTask========start");
String nextDay = DateUtils.getNextDay();
srvTaskService.timeAllocationSrvTask(nextDay);
log.info("timeAllocationSrvTask========end:{}");
}
public static final String JOB_KEY = "redis.job.task:srvTask";
@Scheduled(cron = "0 35 23 * * ?")
public void timeAllocationSrvTask() {
boolean flag = redisLock.tryLock(JOB_KEY, 1000*30);
if(flag){
String nextDay = DateUtils.getNextDay();
log.info("timeAllocationSrvTask========start:{}",nextDay);
srvTaskService.timeAllocationSrvTask(nextDay);
log.info("timeAllocationSrvTask========end:{}");
}
@Scheduled(cron = "*/5 * * * * ?")
public void test111() {
log.info("test111===============================================");
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment