配置任务调度器 (对应的文件名为quartz-task.xml)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="" xmlns:xsi="" xsi:schemaLocation=" " default-lazy-init="false"> <!-- 调度器 --> <bean name="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- 通过applicationContextSchedulerContextKey属性配置spring上下文 --> <property name="applicationContextSchedulerContextKey"> <value>applicationContext</value> </property> </bean> <!--加载可执行的任务--> <bean id="loadTask" class="com.quartz.LoadTask" init-method="initTask" /> </beans>
加载可执行任务的类LoadTask.java
public class LoadTask { public void initTask() throws Exception { Scheduler scheduler = schedulerFactoryBean.getScheduler(); // 可执行的任务列表,Task是自定义的要执行的任务 Collection<Task> taskList = taskService.findTask(); for (Task task : taskList) { // 任务名称和任务组设置规则: // 名称:task_1 .. // 组 :group_1 .. TriggerKey triggerKey = TriggerKey.triggerKey( "task_" + task.getId(), "group_" + task.getId()); CronTrigger trigger = (CronTrigger) scheduler .getTrigger(triggerKey); // 不存在,创建一个 if (null == trigger) { JobDetail jobDetail = JobBuilder .newJob(QuartzJobFactory.class) .withIdentity("task_" + task.getId(), "group_" + task.getId()).build(); jobDetail.getJobDataMap().put("scheduleJob", task); // 表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder .cronSchedule(getCronExpression()); // 按新的表达式构建一个新的trigger trigger = TriggerBuilder .newTrigger() .withIdentity("task_" + task.getId(), "group_" + task.getId()) .withSchedule(scheduleBuilder).build(); scheduler.scheduleJob(jobDetail, trigger); } else { // trigger已存在,则更新相应的定时设置 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder .cronSchedule(taskService.getCronExpression()); // 按新的cronExpression表达式重新构建trigger trigger = trigger.getTriggerBuilder().withIdentity(triggerKey) .withSchedule(scheduleBuilder).build(); // 按新的trigger重新设置job执行 scheduler.rescheduleJob(triggerKey, trigger); } } } @Autowired private SchedulerFactoryBean schedulerFactoryBean; @Autowired private TaskService taskService; } //一个scheduler可以对应一个trigger也可以对应多个trigger //对应多个trigger时会产生并发
调度任务的入口
@DisallowConcurrentExecution //不支持并发,即此job是StatefulJob而不是普通job public class QuartzTaskFactory implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // TODO Auto-generated method stub try { System.out.println("任务运行..."); Task task = (Task) context.getMergedJobDataMap().get( "scheduleJob"); System.out.println("任务名称: [" + task.getTaskName() + "]"); //在这里执行你的任务... } catch (Exception e) { e.printStackTrace(); } } }
暂停任务
Scheduler scheduler = schedulerFactoryBean.getScheduler(); JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); scheduler.pauseJob(jobKey);
恢复任务
Scheduler scheduler = schedulerFactoryBean.getScheduler();
JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
scheduler.resumeJob(jobKey);
删除任务
Scheduler scheduler = schedulerFactoryBean.getScheduler();
JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
scheduler.deleteJob(jobKey);
立即运行任务
Scheduler scheduler = schedulerFactoryBean.getScheduler(); JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); scheduler.triggerJob(jobKey);
更新任务(时间表达式)
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);