一、版本說明
spring3.1以下的版本必須使用quartz1.x系列,3.1以上的版本才支援quartz 2.x,不然會出錯。
原因:spring對於quartz的支持實現,org.springframework.scheduling.quartz.CronTriggerBean繼承了org.quartz.CronTrigger,在quartz1.x系列中org.quartz.CronTrigger是個類,而在quartz2.x系列中org.quartz.CronTrigger是個類,而在quartz2.x系列中.quartz.CronTrigger變成了接口,從而造成無法用spring的方式配置quartz的觸發器(trigger)
此示例所選版本:spring版本號3.0.7.RELEASE,quartz版本1.8.6
二、新增jar包
我的是maven工程,pom.xml相關配置如下:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>3.0.7.RELEASE</spring.version> <quartz.version>1.8.6</quartz.version> </properties>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> <exclusions> <!-- Exclude Commons Logging in favor of SLF4j --> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency><!--3.0.7没这个包 --> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <type>jar</type> <scope>test</scope> </dependency>
三、整合實作
1、spring配置
spring 器工作類別實作
定義定時器作業類,該類別繼承自job類
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" />
定義定時器管理類別
package com.ld.nhmz.quartz; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; /** * quartz示例定时器类 * * @author Administrator * */ public class QuartzJobExample implements Job { @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "★★★★★★★★★★★"); } }
測試程式碼,這裡SchedulerFactory沒有使用spring中配置的bean,而是new出來的,做測試用
package com.ld.nhmz.quartz; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; /** * Quartz调度管理器 * * @author Administrator * */ public class QuartzManager { private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME"; private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME"; /** * @Description: 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名 * * @param sched * 调度器 * * @param jobName * 任务名 * @param cls * 任务 * @param time * 时间设置,参考quartz说明文档 * * @Title: QuartzManager.java */ public static void addJob(Scheduler sched, String jobName, @SuppressWarnings("rawtypes") Class cls, String time) { try { JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, cls);// 任务名,任务组,任务执行类 // 触发器 CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME);// 触发器名,触发器组 trigger.setCronExpression(time);// 触发器时间设定 sched.scheduleJob(jobDetail, trigger); // 启动 if (!sched.isShutdown()) { sched.start(); } } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description: 添加一个定时任务 * * @param sched * 调度器 * * @param jobName * 任务名 * @param jobGroupName * 任务组名 * @param triggerName * 触发器名 * @param triggerGroupName * 触发器组名 * @param jobClass * 任务 * @param time * 时间设置,参考quartz说明文档 * * @Title: QuartzManager.java */ public static void addJob(Scheduler sched, String jobName, String jobGroupName, String triggerName, String triggerGroupName, @SuppressWarnings("rawtypes") Class jobClass, String time) { try { JobDetail jobDetail = new JobDetail(jobName, jobGroupName, jobClass);// 任务名,任务组,任务执行类 // 触发器 CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);// 触发器名,触发器组 trigger.setCronExpression(time);// 触发器时间设定 sched.scheduleJob(jobDetail, trigger); } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description: 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名) * * @param sched * 调度器 * @param jobName * @param time * * @Title: QuartzManager.java */ @SuppressWarnings("rawtypes") public static void modifyJobTime(Scheduler sched, String jobName, String time) { try { CronTrigger trigger = (CronTrigger) sched.getTrigger(jobName, TRIGGER_GROUP_NAME); if (trigger == null) { return; } String oldTime = trigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(time)) { JobDetail jobDetail = sched.getJobDetail(jobName, JOB_GROUP_NAME); Class objJobClass = jobDetail.getJobClass(); removeJob(sched, jobName); addJob(sched, jobName, objJobClass, time); } } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description: 修改一个任务的触发时间 * * @param sched * 调度器 * * @param sched * 调度器 * @param triggerName * @param triggerGroupName * @param time * * @Title: QuartzManager.java */ public static void modifyJobTime(Scheduler sched, String triggerName, String triggerGroupName, String time) { try { CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerName, triggerGroupName); if (trigger == null) { return; } String oldTime = trigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(time)) { CronTrigger ct = (CronTrigger) trigger; // 修改时间 ct.setCronExpression(time); // 重启触发器 sched.resumeTrigger(triggerName, triggerGroupName); } } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description: 移除一个任务(使用默认的任务组名,触发器名,触发器组名) * * @param sched * 调度器 * @param jobName * * @Title: QuartzManager.java */ public static void removeJob(Scheduler sched, String jobName) { try { sched.pauseTrigger(jobName, TRIGGER_GROUP_NAME);// 停止触发器 sched.unscheduleJob(jobName, TRIGGER_GROUP_NAME);// 移除触发器 sched.deleteJob(jobName, JOB_GROUP_NAME);// 删除任务 } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description: 移除一个任务 * * @param sched * 调度器 * @param jobName * @param jobGroupName * @param triggerName * @param triggerGroupName * * @Title: QuartzManager.java */ public static void removeJob(Scheduler sched, String jobName, String jobGroupName, String triggerName, String triggerGroupName) { try { sched.pauseTrigger(triggerName, triggerGroupName);// 停止触发器 sched.unscheduleJob(triggerName, triggerGroupName);// 移除触发器 sched.deleteJob(jobName, jobGroupName);// 删除任务 } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description:启动所有定时任务 * * @param sched * 调度器 * * @Title: QuartzManager.java */ public static void startJobs(Scheduler sched) { try { sched.start(); } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description:关闭所有定时任务 * * * @param sched * 调度器 * * * @Title: QuartzManager.java */ public static void shutdownJobs(Scheduler sched) { try { if (!sched.isShutdown()) { sched.shutdown(); } } catch (Exception e) { throw new RuntimeException(e); } } }
顯示結果:
spring Control層代碼中實現定時器管理
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持PHP中文網。
更多Spring整合Quartz實作動態定時器的範例程式碼相關文章請關注PHP中文網!