• java怎么根据用户设定的时间执行定时任务


    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;
    }
    }
  • 相关阅读:
    蓝桥杯_基础训练_龟兔赛跑预测
    大数加法
    Splay!
    topsort
    各种方法
    有时候dfs可以简化各种组合的操作
    组合数学
    重新认识三观
    手速狗还是不行啊。。。
    set和map和pair 转自ACdreamers
  • 原文地址:https://www.cnblogs.com/zhuyeshen/p/15994491.html
Copyright © 2020-2023  润新知