首页 > 技术文章 > springboot 动态的修改定时任务

blackCatFish 2019-06-12 13:41 原文

有些需求 需要动态修改定时任务执行的时间

import com.cmbchina.ccd.itpm.dao.WeeklyReportSettingMapper;
import com.cmbchina.ccd.itpm.entity.WeeklyReportSetting;
import com.cmbchina.ccd.itpm.entity.WeeklyReportSettingExample;
import com.cmbchina.ccd.itpm.utils.R;
import com.cmbchina.ccd.itpm.utils.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ScheduledFuture;

import static com.cmbchina.ccd.itpm.utils.ObjectUtils.isNotEmpty;

/**
 * @ClassName QuartzController
 * @Description TODO
 * @Author blackCatFish
 * @Date 2019/5/5
 * @Version 1.0
 **/
@RestController
@Component
@Api(tags = "周报发起设置")
public class QuartzController {
    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;
    @Autowired
    private WeeklyReportSettingMapper weeklyReportSettingMapper;

    private ScheduledFuture<?> future;

    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        return new ThreadPoolTaskScheduler();
    }

    @ApiOperation(value = "定时任务设置")
    @PostMapping("/startCron")
    public R addTimer(@RequestBody WeeklyReportSetting weeklyReportSetting, HttpServletRequest request) {
//要清空之前的定时记录

        if (future != null) {
            future.cancel(true);
        }
        //8 8 5 ? ? 2
        if (weeklyReportSetting.getReportType() == 1) {
            String corn="";
            switch (weeklyReportSetting.getSendWeek()){
                case 1:
                    corn="06 00 " + weeklyReportSetting.getSendTie() + " * * SUN";
                    break;
                case 2:
                    corn="06 00 " + weeklyReportSetting.getSendTie() + " * * MON";
                    break;
                case 3:
                    corn="06 00 " + weeklyReportSetting.getSendTie() + " * * TUES";
                    break;
                case 4:
                    corn="06 00 " + weeklyReportSetting.getSendTie() + " * * WED";
                    break;
                case 5:
                    corn="06 00 " + weeklyReportSetting.getSendTie() + " * * THUR";
                    break;
                case 6:
                    corn="06 00 " + weeklyReportSetting.getSendTie() + " * * FRI";
                    break;
                case 7:
                    corn="06 00 " + weeklyReportSetting.getSendTie() + " * * SAT";
                    break;
            }
            future = threadPoolTaskScheduler.schedule(new MyRunnable(weeklyReportSetting), new CronTrigger(corn));
        } else if (weeklyReportSetting.getReportType() == 2) {
            String time = weeklyReportSetting.getTimes();
            String[] times = time.split("-");
            String cron = "06 " + times[4] + " " + times[3] + " " + times[2] + " " + times[1] + " *";
            future = threadPoolTaskScheduler.schedule(new MyRunnable(weeklyReportSetting), new CronTrigger(cron));
        }
        return R.ok();
    }

    private class MyRunnable implements Runnable {
        private WeeklyReportSetting weeklyReportSetting;

        public MyRunnable(WeeklyReportSetting weeklyReportSetting) {
            this.weeklyReportSetting = weeklyReportSetting;
        }

        @Override
        public void run() {
            WeeklyReportSettingExample example = new WeeklyReportSettingExample();
            List<WeeklyReportSetting> weeklyReportSettingList = weeklyReportSettingMapper.selectByExample(example);
            if (isNotEmpty(weeklyReportSettingList)) {
                weeklyReportSetting.setSettingId(System.currentTimeMillis() + "");
                Date date = new Date();
                weeklyReportSetting.setSettingTime(date);
                weeklyReportSettingMapper.deleteByPrimaryKey(weeklyReportSettingList.get(0).getSettingId());
                
                weeklyReportSettingMapper.insert(weeklyReportSetting);
            } else {
                weeklyReportSetting.setSettingId(System.currentTimeMillis() + "");
                Date date = new Date();
                weeklyReportSetting.setSettingTime(date);
            weeklyReportSettingMapper.insert(weeklyReportSetting);
            }

        }
    }
}

这样参数由前端传入进来后端进行处理成corn表达式就可以进行动态的修改定时任务了 

推荐阅读