界面
步骤:首先创建一个javabean---》创建Quartz工厂---》xmlSchedulerFactoryBean---》配置通过反射机制---》执行相应的service
1、bean
package com.oppo.common.quartz;/*** quartz任务类* @author xuchangcheng* 2018年8月24日**/
public class ScheduleJob {/** 任务id */private String jobId;/** 任务名称 */private String jobName;/** 任务分组 */private String jobGroup;/** 任务状态 0禁用 1启用 2删除*/private String jobStatus;/** 任务运行时间表达式 */private String cronExpression;/** 任务描述 */private String desc;/** 类全城 */private String classPath;/** 方法名 */private String methodName;public ScheduleJob() {super();}public String getJobId() {return jobId;}public void setJobId(String jobId) {this.jobId = jobId;}public String getJobName() {return jobName;}public void setJobName(String jobName) {this.jobName = jobName;}public String getJobGroup() {return jobGroup;}public void setJobGroup(String jobGroup) {this.jobGroup = jobGroup;}public String getJobStatus() {return jobStatus;}public void setJobStatus(String jobStatus) {this.jobStatus = jobStatus;}public String getCronExpression() {return cronExpression;}public void setCronExpression(String cronExpression) {this.cronExpression = cronExpression;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc;}public String getClassPath() {return classPath;}public void setClassPath(String classPath) {this.classPath = classPath;}public String getMethodName() {return methodName;}public void setMethodName(String methodName) {this.methodName = methodName;}}
2、工厂(如图是在web端应用,所以都是通过注入的形式获取相应的bean)
package com.oppo.common.quartz;import java.lang.reflect.Method;import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ReflectionUtils;import com.oppo.common.global.listener.WebContextListener;
import com.oppo.common.tools.StringUtils;/*** QuartzJob工厂* @author xuchangcheng* 2018年8月24日**/
@PersistJobDataAfterExecution
public class QuartzJobFactory implements Job {private static final Logger logger = LoggerFactory.getLogger(QuartzJobFactory.class);@Overridepublic void execute(JobExecutionContext context) throws JobExecutionException {ScheduleJob scheduleJob = (ScheduleJob) context.getMergedJobDataMap().get("scheduleJob");Class<?> clazz;try {clazz = Class.forName(scheduleJob.getClassPath());String className = StringUtils.firstToLower(clazz.getSimpleName());Object bean = (Object)WebContextListener.getBean(className);Method method = ReflectionUtils.findMethod(bean.getClass(), scheduleJob.getMethodName());ReflectionUtils.invokeMethod(method, bean);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}logger.info("----------定时开始----------");}}
3、xml
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-4.0.xsd"><!-- ===================================== 日志定时任务 ===================================== --><!-- 日志任务配置:task任务扫描注解 --><context:annotation-config /> <!-- 日志任务配置:task任务扫描注解 --><task:annotation-driven /> <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" /></beans>
4、任务添加
/*** 开启定时任务* @param id*/public void startQurtaz(String id){//定时任务调度Scheduler scheduler = (Scheduler)WebContextListener.getBean("schedulerFactoryBean");Map<String,Object> map = qurtaztDao.findById(id);ScheduleJob scheduleJob = new ScheduleJob();scheduleJob.setClassPath(map.get("job_class").toString());scheduleJob.setMethodName(map.get("job_method").toString());scheduleJob.setJobName(map.get("job_name").toString());scheduleJob.setCronExpression(map.get("job_cron").toString());scheduleJob.setJobStatus(map.get("job_status").toString());TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName());CronTrigger trigger;try {trigger = (CronTrigger) scheduler.getTrigger(triggerKey);} catch (SchedulerException e1) {e1.printStackTrace();}JobDetail jobDetail = JobBuilder.newJob(QuartzJobFactory.class).withIdentity(scheduleJob.getJobName()).build();jobDetail.getJobDataMap().put("scheduleJob", scheduleJob);//表达式调度构建器CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression());//按新的cronExpression表达式构建一个新的triggertrigger = TriggerBuilder.newTrigger().withIdentity(scheduleJob.getJobName()).withSchedule(scheduleBuilder).build();try {scheduler.scheduleJob(jobDetail, trigger);} catch (SchedulerException e) {e.printStackTrace();}}
5、任务修改
/*** 更新定时任务* @param id*/public void updateQurtazTime(String id){//定时任务调度Scheduler scheduler = (Scheduler)WebContextListener.getBean("schedulerFactoryBean");Map<String,Object> map = qurtaztDao.findById(id);ScheduleJob scheduleJob = new ScheduleJob();scheduleJob.setClassPath(map.get("job_class").toString());scheduleJob.setMethodName(map.get("job_method").toString());scheduleJob.setJobName(map.get("job_name").toString());scheduleJob.setCronExpression(map.get("job_cron").toString());TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName());CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression());CronTrigger trigger;try {trigger = (CronTrigger) scheduler.getTrigger(triggerKey);//按新的cronExpression表达式重新构建triggertrigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();//按新的trigger重新设置job执行scheduler.rescheduleJob(triggerKey, trigger);} catch (SchedulerException e) {e.printStackTrace();}}
6、任务暂停
/*** 暂停定时任务* @param id*/public void pauseQurtazTask(String id){Scheduler scheduler = (Scheduler)WebContextListener.getBean("schedulerFactoryBean");Map<String,Object> map = qurtaztDao.findById(id);ScheduleJob scheduleJob = new ScheduleJob();scheduleJob.setClassPath(map.get("job_class").toString());scheduleJob.setMethodName(map.get("job_method").toString());scheduleJob.setJobName(map.get("job_name").toString());scheduleJob.setCronExpression(map.get("job_cron").toString());JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName());try {scheduler.pauseJob(jobKey);} catch (SchedulerException e) {e.printStackTrace();}}
7、任务恢复
/*** 恢复定时任务* @param id*/public void resumeQurtazTask(String id){Scheduler scheduler = (Scheduler)WebContextListener.getBean("schedulerFactoryBean");Map<String,Object> map = qurtaztDao.findById(id);ScheduleJob scheduleJob = new ScheduleJob();scheduleJob.setClassPath(map.get("job_class").toString());scheduleJob.setMethodName(map.get("job_method").toString());scheduleJob.setJobName(map.get("job_name").toString());scheduleJob.setCronExpression(map.get("job_cron").toString());JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName());try {scheduler.resumeJob(jobKey);} catch (SchedulerException e) {e.printStackTrace();}}
8、任务删除
/*** 删除定时任务* @param id*/public void deleteQurtazTask(String id){Scheduler scheduler = (Scheduler)WebContextListener.getBean("schedulerFactoryBean");Map<String,Object> map = qurtaztDao.findById(id);ScheduleJob scheduleJob = new ScheduleJob();scheduleJob.setClassPath(map.get("job_class").toString());scheduleJob.setMethodName(map.get("job_method").toString());scheduleJob.setJobName(map.get("job_name").toString());scheduleJob.setCronExpression(map.get("job_cron").toString());JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName());try {scheduler.deleteJob(jobKey);} catch (SchedulerException e) {e.printStackTrace();}}
参数中的id为数据库中获取的任务id,如图
有喜欢的朋友可以关注下头条号《老徐聊技术》