二手技术:
JDK 1.6.0_21
春天3.1.1
石英1.8.5 Maven的3.0.2
步骤1:建立已完成的专案
创建一个Maven项目,如下所示。 (可以使用Maven或IDE插件创建)
步骤2:图书馆
Spring依赖项已添加到Maven的pom.xml中。
<!-- Spring 3 dependencies -->
<dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${spring.version}</version>
</dependency>
第3步:创建任务类别
将创建一个FirstTask类。
package com.otv.task;import org.apache.log4j.Logger;/*** First Task** @author onlinetechvision.com* @since 24 Feb 2012* @version 1.0.0**/
public class FirstTask {private static Logger log = Logger.getLogger(FirstTask.class);/*** Execute this task**/public void execute() {log.debug("FirstTask runs successfully...");}
}
将创建SecondTask类。
package com.otv.task;import org.apache.log4j.Logger;/*** Second Task** @author onlinetechvision.com* @since 24 Feb 2012* @version 1.0.0**/
public class SecondTask {private static Logger log = Logger.getLogger(SecondTask.class);/*** Execute this task**/public void execute() {log.debug("SecondTask runs successfully...");}
}
第4步:创建ISchedulerService接口
ISchedulerService接口已创建。
package com.otv.service;/*** Scheduler Service Interface** @author onlinetechvision.com* @since 24 Feb 2012* @version 1.0.0**/
public interface ISchedulerService {/*** Execute First Task** @param* @throws* @return*/public void executeFirstTask();/*** Execute Second Task** @param* @throws* @return*/public void executeSecondTask();
}
步骤5:创建SchedulerService类别
SchedulerService类是通过实现ISchedulerService接口创建的。 该服务计划任务。
package com.otv.service;import com.otv.task.FirstTask;
import com.otv.task.SecondTask;/*** Scheduler Service Implementation** @author onlinetechvision.com* @since 24 Feb 2012* @version 1.0.0**/
public class SchedulerService implements ISchedulerService {private FirstTask firstTask;private SecondTask secondTask;/*** Execute First Task**/public void executeFirstTask() {getFirstTask().execute();}/*** Execute Second Task**/public void executeSecondTask() {getSecondTask().execute();}/*** Get First Task** @return FirstTask*/public FirstTask getFirstTask() {return firstTask;}/*** Set First Task** @param firstTask First Task*/public void setFirstTask(FirstTask firstTask) {this.firstTask = firstTask;}/*** Get Second Task** @return SecondTask*/public SecondTask getSecondTask() {return secondTask;}/*** Set Second Task** @param secondTask Second Task*/public void setSecondTask(SecondTask secondTask) {this.secondTask = secondTask;}
}
步骤6:建立应用程式类别
应用程序类已创建。 此类运行应用程序。
package com.otv.starter;import org.springframework.context.support.ClassPathXmlApplicationContext;/*** Application Starter Class** @author onlinetechvision.com* @since 24 Feb 2012* @version 1.0.0**/
public class Application {/*** Main method of the Application**/public static void main(String[] args) {new ClassPathXmlApplicationContext("applicationContext.xml");}
}
步骤7:定义作业详细信息配置
在Spring中,可以通过两种方式定义作业详细信息 。 通过使用MethodInvokingJobDetailFactoryBean或扩展QuartzJobBean 。 在此示例中,已使用MethodInvokingJobDetailFactoryBean方法。 将targetObject和targetMethod属性提供给MethodInvokingJobDetailFactoryBean 。
<!-- Job Details-->
<bean id="FirstTaskJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject" ref="SchedulerService" /><property name="targetMethod" value="executeFirstTask" />
</bean><bean id="SecondTaskJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject" ref="SchedulerService" /><property name="targetMethod" value="executeSecondTask" />
</bean>
步骤8:定义触发配置
在Spring中也可以通过两种方式定义触发器 。 通过定义SimpleTriggerBean或CronTriggerBean 。 使用SimpleTriggerBean时 ,将定义jobDetail , repeatInterval和startDelay属性。 使用CronTriggerBean时 ,将定义jobDetail和cronExpression属性。 在此示例中,第一个任务的重复间隔已设置为5秒 ,第二个任务的重复间隔已设置为12秒 。
<!-- Simple Trigger -->
<bean id="FirstSimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"><property name="jobDetail" ref="FirstTaskJobDetail" /><property name="repeatInterval" value="5000" /><property name="startDelay" value="1000" />
</bean><!-- <bean id="SecondSimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"><property name="jobDetail" ref="SecondTaskJobDetail" /><property name="repeatInterval" value="12000" /><property name="startDelay" value="1000" /></bean> --> <!-- Cron Trigger -->
<bean id="SecondSimpleTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"><property name="jobDetail" ref="SecondTaskJobDetail" /><property name="cronExpression" value="0/12 * * * * ?" />
</bean>
步骤9:定义SchedulerFactoryBean配置
最后,通过创建SchedulerFactoryBean配置作业详细信息和触发器 。
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="jobDetails"><list><ref bean="FirstTaskJobDetail" /><ref bean="SecondTaskJobDetail" /></list></property><property name="triggers"><list><ref bean="FirstSimpleTrigger" /><ref bean="SecondSimpleTrigger" /></list></property>
</bean>
步骤10:创建applicationContext.xml
所有applicationContext.xml内容如下所示。
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!-- Beans Declaration --><bean id="FirstTask" class="com.otv.task.FirstTask"></bean><bean id="SecondTask" class="com.otv.task.SecondTask"></bean><bean id="SchedulerService" class="com.otv.service.SchedulerService"><property name="firstTask" ref="FirstTask" /><property name="secondTask" ref="SecondTask" /></bean><!-- Job Details--><bean id="FirstTaskJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject" ref="SchedulerService" /><property name="targetMethod" value="executeFirstTask" /></bean><bean id="SecondTaskJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject" ref="SchedulerService" /><property name="targetMethod" value="executeSecondTask" /></bean><!-- Simple Trigger --><bean id="FirstSimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"><property name="jobDetail" ref="FirstTaskJobDetail" /><property name="repeatInterval" value="5000" /><property name="startDelay" value="1000" /></bean>
<!--<bean id="SecondSimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"><property name="jobDetail" ref="SecondTaskJobDetail" /><property name="repeatInterval" value="12000" /><property name="startDelay" value="1000" /></bean>
--><!-- Cron Trigger --><bean id="SecondSimpleTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"><property name="jobDetail" ref="SecondTaskJobDetail" /><property name="cronExpression" value="0/12 * * * * ?" /></bean><bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="jobDetails"><list><ref bean="FirstTaskJobDetail" /><ref bean="SecondTaskJobDetail" /></list></property><property name="triggers"><list><ref bean="FirstSimpleTrigger" /><ref bean="SecondSimpleTrigger" /></list></property></bean></beans>
步骤11:运行项目
启动应用程序类后,将在下面显示输出日志:
25.02.2012 00:17:18 DEBUG (FirstTask.java:23) - FirstTask runs successfully...
25.02.2012 00:17:23 DEBUG (FirstTask.java:23) - FirstTask runs successfully...
25.02.2012 00:17:24 DEBUG (SecondTask.java:22) - SecondTask runs successfully...
25.02.2012 00:17:28 DEBUG (FirstTask.java:23) - FirstTask runs successfully...
25.02.2012 00:17:33 DEBUG (FirstTask.java:23) - FirstTask runs successfully...
25.02.2012 00:17:36 DEBUG (SecondTask.java:22) - SecondTask runs successfully...
25.02.2012 00:17:38 DEBUG (FirstTask.java:23) - FirstTask runs successfully...
25.02.2012 00:17:43 DEBUG (FirstTask.java:23) - FirstTask runs successfully...
25.02.2012 00:17:48 DEBUG (SecondTask.java:22) - SecondTask runs successfully...
25.02.2012 00:17:48 DEBUG (FirstTask.java:23) - FirstTask runs successfully...
25.02.2012 00:17:53 DEBUG (FirstTask.java:23) - FirstTask runs successfully...
25.02.2012 00:17:58 DEBUG (FirstTask.java:23) - FirstTask runs successfully...
25.02.2012 00:18:00 DEBUG (SecondTask.java:22) - SecondTask runs successfully...
步骤12:下载
OTV_SpringQuartz_MultiJobScheduling
参考: JCG合作伙伴提供的 使用Spring和Quartz的多作业计划服务 在线技术愿景博客上的Eren Avsarogullari。
翻译自: https://www.javacodegeeks.com/2012/02/spring-and-quartz-multi-job-scheduling.html