package com.yytx.cloud.qa.manager.timer;
import com.alibaba.fastjson.JSON;
import com.yytx.cloud.common.qa.entity.QaTaskEntity;
import com.yytx.cloud.common.redis.service.RedisCache;
import com.yytx.cloud.qa.manager.task.controller.QaTaskController;
import com.yytx.cloud.qa.manager.task.dao.MaTaskDao;
import com.yytx.cloud.qa.manager.task.service.MaTaskService;
import com.yytx.cloud.qa.manager.task.vo.QaTaskQueryParam;
import com.yytx.cloud.qa.manager.task.vo.QaTaskVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
大家好,現在有個需求。用戶在頁面設定時間,在次日的1點執行定時任務。看了Quartz和TimerTask,他們是根據當期時間然后設置一定的時間間隔來定時的執行任務。而現在只想在次日1點執行任務,而且只是執行1次,請問大家有什么好的辦法?
/**
* @author :zhuyeshen
* @date :Created in 2022/2/14 16:17
*/
@Component
public class QaTaskTimer implements ApplicationRunner {
private final static Logger logger = LoggerFactory.getLogger(QaTaskTimer.class);
// 規定的每天時間15:33:30運行
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Autowired
private MaTaskDao dao;
@Autowired
private MaTaskService maTaskService;
@Autowired
private RedisCache redisCache;
@Override
public void run(ApplicationArguments args) throws Exception {
//初始化方法
QaTaskEntity qaTaskEntity = null;
startUpdate(qaTaskEntity);
}
public void startUpdate(QaTaskEntity qaTaskEntity) {
//當qaTaskEntity為null時候是代表初始化加載定時任務
if (qaTaskEntity == null) {
QaTaskQueryParam qaTaskQueryParam = new QaTaskQueryParam();
qaTaskQueryParam.setType("2");
qaTaskQueryParam.setExecStatus("1");
List<QaTaskVo> list = dao.qaTaskList(qaTaskQueryParam);
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
QaTaskEntity taskEntity = new QaTaskEntity();
QaTaskVo qaTaskVo = list.get(i);
if (qaTaskVo == null) {
break;
}
BeanUtils.copyProperties(qaTaskVo, taskEntity);
// 首次運行時間
Date startTime = taskEntity.getExecTime();
if (!isPastDate(startTime)) {
//設置計時間隔
Timer timer = new Timer(true);
timer.schedule(new TimerTask() {
@Override
public void run() {
//需要執行的代碼
if (null != redisCache.getLockService().lock("qa_task_timer", 10000, 1)) {
try {
maTaskService.execuQaTask(taskEntity);
logger.info("------定時任務執行" + JSON.toJSONString(taskEntity));
} catch (Exception e) {
logger.error("定時任務異常" + JSON.toJSONString(qaTaskEntity), e.getMessage());
}
}
}
}, startTime);
}
}
}
} else {
// 首次運行時間
Date startTime = qaTaskEntity.getExecTime();
if (!isPastDate(startTime)) {
//設置計時間隔
Timer timer = new Timer(true);
timer.schedule(new TimerTask() {
@Override
public void run() {
//需要執行的代碼
if (null != redisCache.getLockService().lock("qa_task_timer", 10000, 1)) {
try {
maTaskService.execuQaTask(qaTaskEntity);
logger.info("------定時任務執行" + JSON.toJSONString(qaTaskEntity));
} catch (Exception e) {
logger.error("定時任務異常" + JSON.toJSONString(qaTaskEntity), e.getMessage());
}
}
}
}, startTime);
}
}
}
public static boolean isPastDate(Date pastDat) {
boolean flag = false;
Date nowDate = new Date();
//格式化日期
//在日期字符串非空時執行
if (pastDat != null && !"".equals(pastDat)) {
//調用Date里面的before方法來做判斷
flag = pastDat.before(nowDate);
if (flag) {
logger.info("該日期早於今日");
} else {
logger.info("該日期晚於今日");
}
} else {
logger.info("日期參數不可為空");
}
return flag;
}
}