以前在项目中的时候都是用的静态定时任务,即在配置文件中写好了什么时候由谁来做什么事。后来无聊的时候就觉得这样不能满足所有的需求,就像QQ的提醒,应该也是个动态的,由用户去创建了一个定时通知任务,然后QQ服务器在指定的时间就会通知QQ用户。当然QQ里面的使用的不只是动态的定时任务了。
废话不多说,还是直接上代码。首先是spring的配置
声明一个quartz的工厂就行,其它的由程序来动态完成。
定时任务实体类(可存放到数据库中),
import com.xyan.frame.base.model.BaseModel;public class ScheduleJobModel extends BaseModel{ /** 任务名称 */ private String jobName; /** 任务分组 */ private String jobGroup; /** 任务状态 0禁用 1启用 2删除*/ private String jobStatus; /** 任务运行时间表达式 */ private String cronExpression; /** 任务描述 */ private String remark; 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 getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; }}
注:BaseModel是我自定义的类,用于统一主键的。
然后就是任务执行监控了,任务运行入口,即Job实现类,在这里我把它看作工厂类:
import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import com.xyan.frame.quartz.model.ScheduleJobModel;public class QuartzJobFactory implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println(); System.out.println("任务成功运行"); ScheduleJobModel scheduleJob = (ScheduleJobModel)context.getMergedJobDataMap().get("scheduleJob"); System.out.println("任务名称 = [" + scheduleJob.getJobName() + "]"); /* 此处可以添加自己真正的业务逻辑 */ System.out.println(); }}
之后就是调用工具了,工具类代码如下:
import java.util.ArrayList;import java.util.List;import java.util.Set;import org.quartz.CronScheduleBuilder;import org.quartz.CronTrigger;import org.quartz.JobBuilder;import org.quartz.JobDetail;import org.quartz.JobExecutionContext;import org.quartz.JobKey;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.Trigger;import org.quartz.TriggerBuilder;import org.quartz.TriggerKey;import org.quartz.impl.matchers.GroupMatcher;import org.springframework.scheduling.quartz.SchedulerFactoryBean;import com.xyan.frame.quartz.model.ScheduleJobModel;import com.xyan.frame.util.SpringUtil;/** * @author wangming */public class JobUtils { private static org.springframework.scheduling.quartz.SchedulerFactoryBean schedulerFactoryBean= SpringUtil.getBean(SchedulerFactoryBean.class); /** * 添加任务并立即执行 * @param jobList * @throws SchedulerException */ public static void addJob(ListjobList) throws SchedulerException { // schedulerFactoryBean 由spring创建注入 Scheduler scheduler = schedulerFactoryBean.getScheduler(); for (ScheduleJobModel job : jobList) { TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(), job.getJobGroup()); // 获取trigger,即在spring配置文件中定义的 bean id="myTrigger" CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); // 不存在,创建一个 if (null == trigger) { JobDetail jobDetail = JobBuilder.newJob(QuartzJobFactory.class).withIdentity(job.getJobName(),job.getJobGroup()).build(); jobDetail.getJobDataMap().put("scheduleJob", job); // 表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression()); // 按新的cronExpression表达式构建一个新的trigger trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobName(), job.getJobGroup()).withSchedule(scheduleBuilder).build(); scheduler.scheduleJob(jobDetail, trigger); } else { // Trigger已存在,那么更新相应的定时设置 // 表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder .cronSchedule(job.getCronExpression()); // 按新的cronExpression表达式重新构建trigger trigger = trigger.getTriggerBuilder().withIdentity(triggerKey) .withSchedule(scheduleBuilder).build(); // 按新的trigger重新设置job执行 scheduler.rescheduleJob(triggerKey, trigger); } } } /** *@Auhor:wangming *@Description:获取计划中的任务 *@return *@throws SchedulerException *@Date:2016年3月30日 下午3:11:56 */ public static List getPlanTask() throws SchedulerException { Scheduler scheduler = schedulerFactoryBean.getScheduler(); GroupMatcher matcher = GroupMatcher.anyJobGroup(); Set jobKeys = scheduler.getJobKeys(matcher); List jobList = new ArrayList (); for (JobKey jobKey : jobKeys) { List triggers = scheduler.getTriggersOfJob(jobKey); for (Trigger trigger : triggers) { ScheduleJobModel job = new ScheduleJobModel(); job.setJobName(jobKey.getName()); job.setJobGroup(jobKey.getGroup()); job.setRemark("触发器:" + trigger.getKey()); Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey()); job.setJobStatus(triggerState.name()); if (trigger instanceof CronTrigger) { CronTrigger cronTrigger = (CronTrigger) trigger; String cronExpression = cronTrigger.getCronExpression(); job.setCronExpression(cronExpression); } jobList.add(job); } } return jobList; } /** *@Auhor:wangming *@Description:获取运行中的任务 *@throws SchedulerException *@Date:2016年3月30日 下午3:13:10 */ public static void getRunningTask() throws SchedulerException { Scheduler scheduler = schedulerFactoryBean.getScheduler(); List executingJobs = scheduler .getCurrentlyExecutingJobs(); List jobList = new ArrayList ( executingJobs.size()); for (JobExecutionContext executingJob : executingJobs) { ScheduleJobModel job = new ScheduleJobModel(); JobDetail jobDetail = executingJob.getJobDetail(); JobKey jobKey = jobDetail.getKey(); Trigger trigger = executingJob.getTrigger(); job.setJobName(jobKey.getName()); job.setJobGroup(jobKey.getGroup()); job.setRemark("触发器:" + trigger.getKey()); Trigger.TriggerState triggerState = scheduler .getTriggerState(trigger.getKey()); job.setJobStatus(triggerState.name()); if (trigger instanceof CronTrigger) { CronTrigger cronTrigger = (CronTrigger) trigger; String cronExpression = cronTrigger.getCronExpression(); job.setCronExpression(cronExpression); } jobList.add(job); } } /** *@Auhor:wangming *@Description:暂停一个任务 *@param scheduleJob *@throws SchedulerException *@Date:2016年3月30日 下午3:13:48 */ public static void pauseJob(ScheduleJobModel scheduleJob) throws SchedulerException { Scheduler scheduler = schedulerFactoryBean.getScheduler(); JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); scheduler.pauseJob(jobKey); } /** *@Auhor:wangming *@Description:恢复一个任务 *@param scheduleJob *@throws SchedulerException *@Date:2016年3月30日 下午3:14:02 */ public static void resume(ScheduleJobModel scheduleJob) throws SchedulerException { Scheduler scheduler = schedulerFactoryBean.getScheduler(); JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); scheduler.resumeJob(jobKey); } /** *@Auhor:wangming *@Description:删除一个任务 *@param scheduleJob *@throws SchedulerException *@Date:2016年3月30日 下午3:14:21 */ public static void delete(ScheduleJobModel scheduleJob) throws SchedulerException { Scheduler scheduler = schedulerFactoryBean.getScheduler(); JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); scheduler.deleteJob(jobKey); } /** *@Auhor:wangming *@Description:(用一句话描述) *@param scheduleJob *@throws SchedulerException *@Date:2016年3月30日 下午3:14:38 */ public static void noWaitRun(ScheduleJobModel scheduleJob) throws SchedulerException { Scheduler scheduler = schedulerFactoryBean.getScheduler(); JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); scheduler.triggerJob(jobKey); } /** *@Auhor:wangming *@Description:更新一个任务 *@param scheduleJob *@throws SchedulerException *@Date:2016年3月30日 下午3:15:37 */ public static void update(ScheduleJobModel scheduleJob) throws SchedulerException { Scheduler scheduler = schedulerFactoryBean.getScheduler(); TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); // 获取trigger,即在spring配置文件中定义的 bean id="myTrigger" CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); // 表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder .cronSchedule(scheduleJob.getCronExpression()); // 按新的cronExpression表达式重新构建trigger trigger = trigger.getTriggerBuilder().withIdentity(triggerKey) .withSchedule(scheduleBuilder).build(); // 按新的trigger重新设置job执行 scheduler.rescheduleJob(triggerKey, trigger); }}
完成以上的步骤以后就可以在项目中动态的调用了,如我的测试代码:
@Autowired private ScheduleJobService jobServicee; @RequestMapping(value = "testJob") @ResponseBody public String testJob() throws SchedulerException { ListjobList=jobServicee.selectByExample(null); JobUtils.addJob(jobList); return "test"; } @RequestMapping(value = "pauseJob") @ResponseBody public String pauseJob() throws SchedulerException { ScheduleJobModel job=jobServicee.selectByPrimaryKey(3L); JobUtils.pause(job); return "test"; }
到这里,quartz 2的动态定时任务功能算是告一段落了,对常用的一些功能进行了实现,相信可以满足一般项目的需求了。当然了,有很多地方还是可以再优化的。
参考: