Some requirements need to dynamically modify the timing of task execution time
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 = "Weekly initiate setting" ) public class QuartzController { @Autowired private ThreadPoolTaskScheduler threadPoolTaskScheduler; @Autowired private WeeklyReportSettingMapper weeklyReportSettingMapper; private ScheduledFuture<?> future; @Bean public ThreadPoolTaskScheduler threadPoolTaskScheduler() { return new ThreadPoolTaskScheduler(); } @ApiOperation (value = "cron job settings" ) @PostMapping ( "/ startCron" ) public R & lt addTimer (@RequestBody WeeklyReportSetting weeklyReportSetting, the HttpServletRequest Request) { // timer record prior to empty 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); } } } }
Such parameters passed by the rear end of the front end processing to incoming corn expression can dynamically modify the timing of the task
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<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; }
Thank you for the strong support for the work of the credit card application licensed branch Ministry, TIME weekly Please click on the link <a href=\"http://cpbc.cs/#/weekly/history/TIME\"> http: //cpbc.cs / # / email / TIME </a> view. </ Div> "); weeklyReportSettingMapper.insert (weeklyReportSetting);} Thank you for the strong support for the work of the credit card application licensed branch Ministry, TIME weekly Please click on the link <a href=\"http://cpbc.cs/#/weekly/history/TIME\"> http: //cpbc.cs / # / email / TIME </a> view. </ Div> "); weeklyReportSettingMapper.insert (weeklyReportSetting);}
} }}