使用Spring3+Quartz实现定时任务
在本文中,我们使用Spring3+Quartz实现定时任务。spring中定义了一些接口来支持Quartz,可以使你的业务逻辑类和Quartz解耦。
这个例子中使用了如下maven的依赖项:
<dependencies> <!-- Spring 3 dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.1.2.RELEASE</version> </dependency> <!-- QuartzJobBean in spring-context-support.jar --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.1.2.RELEASE</version> </dependency> <!-- Spring + Quartz need transaction --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>3.1.2.RELEASE</version> </dependency> <!-- Quartz framework --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>1.8.6</version> </dependency></dependencies>
添加依赖之后需要创建一个java类,该类完成要执行的定时任务。
File : RunMeTask.java
package com.mkyong.common;public class RunMeTask { public void printMe() { System.out.println("Spring 3 + Quartz 1.8.6 ~"); }}
有了要执行的任务,我们需要声明Quartz Scheduler job。
使用spring有两种方法来声明Quartz的job
方法一是使用MethodInvokingJobDetailFactoryBean类
<bean id="runMeJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="runMeTask" /> <property name="targetMethod" value="printMe" /></bean>
方法二是从org.springframework.scheduling.quartz.QuartzJobBean
类继承,如下RumMeJob类实现
File : RunMeJob.java
package com.mkyong.common;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.springframework.scheduling.quartz.QuartzJobBean;public class RunMeJob extends QuartzJobBean { private RunMeTask runMeTask; public void setRunMeTask(RunMeTask runMeTask) { this.runMeTask = runMeTask; } protected void executeInternal(JobExecutionContext context) throws JobExecutionException { runMeTask.printMe(); }}
Configure the target class via jobClass and method to run via jobDataAsMap.
<bean name="runMeJob" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="com.mkyong.common.RunMeJob" /> <property name="jobDataAsMap"> <map> <entry key="runMeTask" value-ref="runMeTask" /> </map> </property></bean>
配置Quartz触发器来定义执行调度任务的时机。支持两种类型的触发器。
一种是SimpleTrigger,你可以指定任务的开始时间,结束时间以及重复的间隔。
<!-- Simple Trigger, run every 5 seconds --> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="runMeJob" /> <property name="repeatInterval" value="5000" /> <property name="startDelay" value="1000" /> </bean>
另一种是CronTrigger,这种方式类似于linux的crontab的语法
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="runMeJob" /> <property name="cronExpression" value="0/5 * * * * ?" /> </bean>
linux的crontab表达式是非常强大的,你可以参考下面两篇文章 http://en.wikipedia.org/wiki/CRON_expression http://www.quartz-scheduler.org/docs/examples/Example3.html
最后一步需要创建SchedulerFactoryBean来定义执行的任务,和任务的触发时机。
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="jobDetails"> <list> <ref bean="runMeJob" /> </list> </property> <property name="triggers"> <list> <ref bean="simpleTrigger" /> </list> </property> </bean>
完整的Spring配置文件如下
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="runMeTask" class="com.mkyong.common.RunMeTask" /> <!-- Spring Quartz --> <bean name="runMeJob" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="com.mkyong.common.RunMeJob" /> <property name="jobDataAsMap"> <map> <entry key="runMeTask" value-ref="runMeTask" /> </map> </property> </bean> <!-- <bean id="runMeJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="runMeTask" /> <property name="targetMethod" value="printMe" /> </bean> --> <!-- Simple Trigger, run every 5 seconds --> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="runMeJob" /> <property name="repeatInterval" value="5000" /> <property name="startDelay" value="1000" /> </bean> <!-- Cron Trigger, run every 5 seconds --> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="runMeJob" /> <property name="cronExpression" value="0/5 * * * * ?" /> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="jobDetails"> <list> <ref bean="runMeJob" /> </list> </property> <property name="triggers"> <list> <ref bean="simpleTrigger" /> </list> </property> </bean></beans>
你可以通过下面的类来运行示例:
package com.mkyong.common;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App { public static void main( String[] args ) throws Exception { new ClassPathXmlApplicationContext("Spring-Quartz.xml"); }}
控制台输出内容类似:
Jul 25, 2012 3:23:09 PM org.springframework.scheduling.quartz.SchedulerFactoryBean startScheduler INFO: Starting Quartz Scheduler nowSpring 3 + Quartz 1.8.6 ~ //run every 5 secondsSpring 3 + Quartz 1.8.6 ~
来自:http://outofmemory.cn/code-snippet/3877/spring3.0-quartz-for-schedule-jobs