本文档主要对Spring内置的Quartz如何使用进行一个简单的介绍,在阅读文档之前,请首先确认你的项目里面是否引入了Spring框架。
1.首先我们写好需要被调用的方法,做为示例,所以我做了两个方法,一个方法每隔五秒被调用一次,另一个方法每小时的45分00秒被调用一次,两个方法都是在控制台上打印一句话,以表示方法已经被调用,在实际运用过程中,你需要根据实际需求来写你的方法。(在这里,方法名需要与配置文件中的targetMethod一致)
public class QuartzDemo {
public void lunxun(){
System.out.println("这是轮询任务(每5秒调用一次)————————————————————");
}
public void dingshi(){
System.out.println("这是定时任务(每小时的45分调用一次)————————————————————");
}
}
2.现在你需要写一个配置文件,并在applicationContext中引入它(只要能保证这个配置文件在项目启动时能够被Spring所加载就够了,在哪里引入并不重要),我在项目里新建了一个spring-config-quartz.xml做为quartz的配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
">
</beans>
3.现在我们来一步一步的配置这个文件,使我们在一开始写的代码能够在指定的时间运行。首先配置好要调用的方法所在的bean
<bean id="testJob" class="com.br.wfykj.cws.controller.DHDemo.QuartzDemo"/>
id是之后需要引用的,class指定了方法所在的类
4.下一步,配置要触发的方法和一些参数
<bean id="lunxunJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="testJob"/>
<property name="targetMethod" value="lunxun"/>
<property name="concurrent" value="true"/>
</bean>
targetObject:指定方法所在类的bean
targetMethod:指定要触发的方法名
concurrent:是否允许任务并发执行,如果值为false,表示必须等待当前线程执行完毕才能启动下一个线程
5.配置方法的触发规则
<bean id="dingshiTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="dingshiJobDetail"/>
<property name="cronExpression" value="0 45 * * * ?"/>
</bean>
jobDetail:指定要执行的方法bean
cronExpression:指定要在什么时间执行,上面的例子表示在每小时的45分00秒执行代码.
corn表达式可以在线生成。
6.将trigger配置到SchedulerFactoryBean中,使代码能够被Quartz执行。
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="lunxunTrigger"/>
<ref bean="dingshiTrigger"/>
</list>
</property>
<property name="autoStartup" value="true"/>
</bean>
7.现在就可以启动你的tomcat了,启动成功后你会在控制台中看到如下输出:
到这里,Quartz的整个配置过程就结束了。定时任务和轮询任务的配置方法都是一致的,区别在于corn表达式的设置。
上面的配置文件并不完整,下面贴出完整的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
">
<!--指定任务所在的类-->
<bean id="testJob" class="com.br.wfykj.cws.controller.DHDemo.QuartzDemo"/>
<!--指定轮询任务要触发的方法与其他参数-->
<bean id="lunxunJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="testJob"/>
<property name="targetMethod" value="lunxun"/>
<property name="concurrent" value="true"/>
<!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
</bean>
<!--指定定时任务要触发的方法与其他参数-->
<bean id="dingshiJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="testJob"/>
<property name="targetMethod" value="dingshi"/>
<property name="concurrent" value="true"/>
</bean>
<!--轮询任务触发器,指定调用时间-->
<bean id="lunxunTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="lunxunJobDetail"/>
<property name="cronExpression" value="*/5 * * * * ?"/><!--每隔5秒钟触发一次-->
</bean>
<!--定时任务触发器,指定调用时间-->
<bean id="dingshiTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="dingshiJobDetail"/>
<property name="cronExpression" value="0 45 * * * ?"/>
</bean>
<!--指定trigger列表,可以指定多个trigger-->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="lunxunTrigger"/>
<ref bean="dingshiTrigger"/>
</list>
</property>
<property name="autoStartup" value="true"/>
</bean>
</beans>