springboot 动态的修改定时任务

2020-12-13 01:46

阅读:409

标签:contex   spring   servlet   new t   8 8   opera   body   ati   ota   

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

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 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表达式就可以进行动态的修改定时任务了

package com.cmbchina.ccd.itpm.controller;
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 yinj * @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) {        WeeklyReportSettingExample example = new WeeklyReportSettingExample();        List weeklyReportSettingList = weeklyReportSettingMapper.selectByExample(example);        if (isNotEmpty(weeklyReportSettingList)) {            weeklyReportSetting.setSettingId(System.currentTimeMillis() + "");            Date date = new Date();            weeklyReportSetting.setSettingTime(date);            weeklyReportSetting.setEmployeeName(SessionUtil.getUser(request).getName());            weeklyReportSetting.setEmailTemplate(null);            weeklyReportSettingMapper.deleteByPrimaryKey(weeklyReportSettingList.get(0).getSettingId());            weeklyReportSettingMapper.insert(weeklyReportSetting);        } else {            weeklyReportSetting.setSettingId(System.currentTimeMillis() + "");            Date date = new Date();            weeklyReportSetting.setSettingTime(date);            weeklyReportSetting.setEmailTemplate(null);            weeklyReportSetting.setEmployeeName(SessionUtil.getUser(request).getName());            weeklyReportSettingMapper.insert(weeklyReportSetting);        }
        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 weeklyReportSettingList = weeklyReportSettingMapper.selectByExample(example);            if (isNotEmpty(weeklyReportSettingList)) {                weeklyReportSetting.setSettingId(System.currentTimeMillis() + "");                Date date = new Date();                weeklyReportSetting.setSettingTime(date);                weeklyReportSettingMapper.deleteByPrimaryKey(weeklyReportSettingList.get(0).getSettingId());                weeklyReportSetting.setEmailTemplate(                        "

各专项小组对口联系人,见信好!
感谢各位对分行信用卡部持牌申请工作的大力支持,TIME 周报请点击链接 http://cpbc.cs/#/email/TIME 查看。
");                weeklyReportSettingMapper.insert(weeklyReportSetting);            } else {                weeklyReportSetting.setSettingId(System.currentTimeMillis() + "");                Date date = new Date();                weeklyReportSetting.setSettingTime(date);                weeklyReportSetting.setEmailTemplate(                        "
各专项小组对口联系人,见信好!
感谢各位对分行信用卡部持牌申请工作的大力支持,TIME 周报请点击链接 http://cpbc.cs/#/email/TIME 查看。
");                weeklyReportSettingMapper.insert(weeklyReportSetting);            }
        }    }}

 

springboot 动态的修改定时任务

标签:contex   spring   servlet   new t   8 8   opera   body   ati   ota   

原文地址:https://www.cnblogs.com/blackCatFish/p/11009122.html


评论


亲,登录后才可以留言!