springboot 动态的修改定时任务
Posted blackcatfish
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot 动态的修改定时任务相关的知识,希望对你有一定的参考价值。
有些需求 需要动态修改定时任务执行的时间
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表达式就可以进行动态的修改定时任务了
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 **/@[email protected]@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<WeeklyReportSetting> 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<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()); weeklyReportSetting.setEmailTemplate( "<div>各专项小组对口联系人,见信好!</div><div style=\"text-indent: 2em;\">感谢各位对分行信用卡部持牌申请工作的大力支持,TIME 周报请点击链接 <a href=\"http://cpbc.cs/#/weekly/history/TIME\">http://cpbc.cs/#/email/TIME</a> 查看。</div>"); weeklyReportSettingMapper.insert(weeklyReportSetting); else weeklyReportSetting.setSettingId(System.currentTimeMillis() + ""); Date date = new Date(); weeklyReportSetting.setSettingTime(date); weeklyReportSetting.setEmailTemplate( "<div>各专项小组对口联系人,见信好!</div><div style=\"text-indent: 2em;\">感谢各位对分行信用卡部持牌申请工作的大力支持,TIME 周报请点击链接 <a href=\"http://cpbc.cs/#/weekly/history/TIME\">http://cpbc.cs/#/email/TIME</a> 查看。</div>"); weeklyReportSettingMapper.insert(weeklyReportSetting);
以上是关于springboot 动态的修改定时任务的主要内容,如果未能解决你的问题,请参考以下文章
基于springboot ThreadPoolTaskScheduler类实现定时任务动态添加修改