Spring定时器quartz,动态添加定时任务
QuartzUtils:
import com.demo.bean.QuartzBean;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
/**
* 创建定时任务暂停,修改,启动,单次启动工具类
*/
public class QuartzUtils {
private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();
/**
* 创建定时任务 定时任务创建之后默认启动状态
* @param quartzBean 定时任务信息类
*/
@SuppressWarnings("unchecked")
public static void createScheduleJob(QuartzBean quartzBean) throws ClassNotFoundException, SchedulerException {
Scheduler scheduler = schedulerFactory.getScheduler();
//获取到定时任务的执行类 必须是类的绝对路径名称
//定时任务类需要是job类的具体实现 QuartzJobBean是job的抽象类。
Class<? extends Job> jobClass = (Class<? extends Job>) quartzBean.getJobClass();
// 构建定时任务信息
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(quartzBean.getJobName()).build();
if(quartzBean.getParams() != null){
jobDetail.getJobDataMap().put("params",quartzBean.getParams());
}
// 设置定时任务执行方式
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(quartzBean.getCronExpression());
// 构建触发器trigger
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(quartzBean.getJobName()).withSchedule(scheduleBuilder).build();
scheduler.scheduleJob(jobDetail,trigger);
if(!scheduler.isShutdown()){
scheduler.start();
}
}
/**
* 根据任务名称暂停定时任务
* @param jobName 定时任务名称
*/
public static void pauseScheduleJob(String jobName) throws SchedulerException {
JobKey jobKey = JobKey.jobKey(jobName);
schedulerFactory.getScheduler().pauseJob(jobKey);
}
/**
* 根据任务名称恢复定时任务
* @param jobName 定时任务名称
*/
public static void resumeScheduleJob(String jobName) throws SchedulerException {
JobKey jobKey = JobKey.jobKey(jobName);
schedulerFactory.getScheduler().resumeJob(jobKey);
}
/**
* 根据任务名称立即运行一次定时任务
* @param jobName 定时任务名称
*/
public static void runOnce(String jobName) throws SchedulerException {
JobKey jobKey = JobKey.jobKey(jobName);
schedulerFactory.getScheduler().triggerJob(jobKey);
}
/**
* 更新定时任务
* @param quartzBean 定时任务信息类
*/
public static void updateScheduleJob(QuartzBean quartzBean) throws SchedulerException {
Scheduler scheduler = schedulerFactory.getScheduler();
//获取到对应任务的触发器
TriggerKey triggerKey = TriggerKey.triggerKey(quartzBean.getJobName());
//设置定时任务执行方式
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(quartzBean.getCronExpression());
//重新构建任务的触发器trigger
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
//重置对应的job
scheduler.rescheduleJob(triggerKey, trigger);
}
/**
* 根据定时任务名称从调度器当中删除定时任务
* @param jobName 定时任务名称
*/
public static void deleteScheduleJob(String jobName) throws SchedulerException {
JobKey jobKey = JobKey.jobKey(jobName);
schedulerFactory.getScheduler().deleteJob(jobKey);
}
}
QuartzBean
public class QuartzBean {
/** 任务id */
private String id;
/** 任务名称 */
private String jobName;
/** 任务执行类 */
private Class jobClass;
/** 任务状态 启动还是暂停*/
private Integer status;
/** 任务运行时间表达式 */
private String cronExpression;
/** 任务执行参数 **/
private Object params;
}
@RestController
@RequestMapping("/quartz/job/")
public class QuartzController {
@GetMapping("/create")
@ResponseBody
public String createJob() throws SchedulerException, ClassNotFoundException {
QuartzBean quartzBean = new QuartzBean();
quartzBean.setId("1");
quartzBean.setCronExpression("0/5 * * * * ? *");
quartzBean.setStatus(1);
quartzBean.setJobClass(HelloJob.class);
quartzBean.setJobName("Hello的动态定时任务");
Params params = new Params();
params.setCount(100);
params.setLiveRoomCode("10010");
quartzBean.setParams(params);
QuartzUtils.createScheduleJob(quartzBean);
return "已创建任务";
}
@PostMapping("/pause")
@ResponseBody
public String pauseJob(String jobName) throws SchedulerException {
QuartzUtils.pauseScheduleJob (jobName);
return "已暂停成功";
}
@PostMapping("/run")
@ResponseBody
public String runOnce(String jobName) throws SchedulerException {
QuartzUtils.runOnce (jobName);
return "运行任务" + jobName + "成功";
}
@PostMapping("/resume")
@ResponseBody
public String resume(String jobName) throws SchedulerException {
QuartzUtils.resumeScheduleJob(jobName);
return "恢复定时任务成功:" + jobName;
}
@PostMapping("/update")
@ResponseBody
public String update(@RequestBody QuartzBean quartzBean) throws SchedulerException {
QuartzUtils.updateScheduleJob(quartzBean);
return "更新定时任务调度信息成功";
}
}
public class HelloJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("---------------------------------");
System.out.println(LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
System.out.println(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(String.format("Tom at %s say : %s",formatter.format(LocalDateTime.now()),"Hello!"));
Params params = (Params)jobExecutionContext.getJobDetail().getJobDataMap().get("params");
System.out.println("parms is :" + params == null ? "null" : params.toString());
}
}
以上是 Spring定时器quartz,动态添加定时任务 的全部内容, 来源链接: utcz.com/z/518287.html