org.springframework.boot
spring-boot-starter-quartz
2. 创建定时任务实体类
package com.chinamobilesz.quartz;
import lombok.Data;
/**
* @Description:
* @author: zhaolei
* @date: 2020/8/11 15:23
*/
@Data
public class QuartzBean {
/** 任务id */
private String id;
/** 任务名称 */
private String jobName;
/** 任务执行类 */
private String jobClass;
/** 任务状态 启动还是暂停*/
private Integer status;
/** 任务运行时间表达式 */
private String cronExpression;
}
3. 创建定时任务工具类
package com.chinamobilesz.quartz;
import org.quartz.*;
import java.util.Map;
/**
* @Description:
* @author: zhaolei
* @date: 2020/8/11 15:23
*/
public class QuartzUtils {
/**
* 创建定时任务 定时任务创建之后默认启动状态
* @param scheduler 调度器
* @param quartzBean 定时任务信息类
* @throws Exception
*/
public static void createScheduleJob(Scheduler scheduler, QuartzBean quartzBean, Map map){
try {
//获取到定时任务的执行类 必须是类的绝对路径名称
//定时任务类需要是job类的具体实现 QuartzJobBean是job的抽象类。
Class extends Job> jobClass = (Class extends Job>) Class.forName(quartzBean.getJobClass());
// 构建定时任务信息
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(quartzBean.getJobName()).build();
JobDataMap jobDataMap = jobDetail.getJobDataMap();
map.forEach((key,value) -> jobDataMap.put(key,value));
// 设置定时任务执行方式
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(quartzBean.getCronExpression());
// 构建触发器trigger
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(quartzBean.getJobName()).withSchedule(scheduleBuilder).build();
scheduler.scheduleJob(jobDetail, trigger);
} catch (ClassNotFoundException e) {
System.out.println("定时任务类路径出错:请输入类的绝对路径");
} catch (SchedulerException e) {
System.out.println("创建定时任务出错:"+e.getMessage());
}
}
/**
* 根据任务名称暂停定时任务
* @param scheduler 调度器
* @param jobName 定时任务名称
* @throws SchedulerException
*/
public static void pauseScheduleJob(Scheduler scheduler, String jobName){
JobKey jobKey = JobKey.jobKey(jobName);
try {
scheduler.pauseJob(jobKey);
} catch (SchedulerException e) {
System.out.println("暂停定时任务出错:"+e.getMessage());
}
}
/**
* 根据任务名称恢复定时任务
* @param scheduler 调度器
* @param jobName 定时任务名称
* @throws SchedulerException
*/
public static void resumeScheduleJob(Scheduler scheduler, String jobName) {
JobKey jobKey = JobKey.jobKey(jobName);
try {
scheduler.resumeJob(jobKey);
} catch (SchedulerException e) {
System.out.println("启动定时任务出错:"+e.getMessage());
}
}
/**
* 根据任务名称立即运行一次定时任务
* @param scheduler 调度器
* @param jobName 定时任务名称
* @throws SchedulerException
*/
public static void runOnce(Scheduler scheduler, String jobName){
JobKey jobKey = JobKey.jobKey(jobName);
try {
scheduler.triggerJob(jobKey);
} catch (SchedulerException e) {
System.out.println("运行定时任务出错:"+e.getMessage());
}
}
/**
* 更新定时任务
* @param scheduler 调度器
* @param quartzBean 定时任务信息类
* @throws SchedulerException
*/
public static void updateScheduleJob(Scheduler scheduler, QuartzBean quartzBean) {
try {
//获取到对应任务的触发器
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);
} catch (SchedulerException e) {
System.out.println("更新定时任务出错:"+e.getMessage());
}
}
/**
* 根据定时任务名称从调度器当中删除定时任务
* @param scheduler 调度器
* @param jobName 定时任务名称
* @throws SchedulerException
*/
public static void deleteScheduleJob(Scheduler scheduler, String jobName) {
JobKey jobKey = JobKey.jobKey(jobName);
try {
scheduler.deleteJob(jobKey);
} catch (SchedulerException e) {
System.out.println("删除定时任务出错:"+e.getMessage());
}
}
}
4. 创建定时任务task
package com.chinamobilesz.quartz.querytask;
import com.chinamobilesz.quartz.QuartzBean;
import com.chinamobilesz.quartz.QuartzUtils;
import com.chinamobilesz.socketIo.PushMessage;
import com.chinamobilesz.socketIo.SocketIOService;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.util.Date;
/**
* @Description:
* @author: zhaolei
* @date: 2020/8/11 15:25
*/
public class testTask implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext){
JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
jobDataMap.forEach((key,value) -> System.out.println("key"+key+" value"+value));
//测试定时任务是否生效
Date date = new Date();
String szDate = String.format("当前时间为:%tH时%tM分%tS秒", date,date,date);
System.out.println(szDate);
QuartzUtils.deleteScheduleJob(jobExecutionContext.getScheduler(),jobDataMap.get("jobName").toString());
}
}
5. 调用定时任务
package com.chinamobilesz;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinamobilesz.quartz.QuartzBean;
import com.chinamobilesz.quartz.QuartzUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
/**
* @Description:
* @author: zhaolei
* @date: 2020/8/10 18:02
*/
@RunWith(SpringRunner.class)
@SpringBootTest()
public class test1 {
@Autowired
private Scheduler scheduler;
@Test
public void quartzAdd(){
try {
QuartzBean quartzBean = new QuartzBean();
quartzBean.setJobClass("com.chinamobilesz.quartz.querytask.testTask");
quartzBean.setJobName("test1");
quartzBean.setCronExpression("*/10 * * * * ?");
Map map = new HashMap <>();
map.put("fileids","45678");
map.put("jobName","test1");
QuartzUtils.createScheduleJob(scheduler,quartzBean,map);
Thread.sleep(100000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}