小试spring整合quartz
在这里就不写别的spring配置文件了,就只写一个quartz_config.xml 作为spring的配置文件
我整合的时候用的是spring2.5的,不过我用3.0的jar也是一样可行的
第一步:web.xml 里面加载spring的listener 和 quartz_config.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/quartz_config.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
第二步 就是配置quartz_config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- helloworld -->
<bean id="hellWordTaskTimerBean" class="com.yeshun.quartz.HelloWordTask" />
<bean id="hellWordTaskTimerMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="hellWordTaskTimerBean" />
<!-- targetMethod 配置定时执行的方法名 -->
<property name="targetMethod" value="executeAction" />
<property name="concurrent" value="false" />
</bean>
<!-- Quartz中有几种triggers,Spring提供两个子类 CronTriggerBean 和 SimpleTriggerBean (补充)-->
<bean id="hellWordTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="hellWordTaskTimerMethod" />
<property name="cronExpression" value="0/10 * * * * ?" />
</bean>
<bean id="helloWordTaskTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="hellWordTaskTimerMethod" />
<!-- 延迟10秒启动 -->
<property name="startDelay" value="10000" />
<!-- 每60秒执行一次 -->
<property name="repeatInterval" value="60000" />
</bean>
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="hellWordTaskTrigger" />
</list>
</property>
</bean>
</beans>
这样一看就很明了了吧
同样我只配置了一个定时作业任务 ,可以配置多个,然后加到list里面
<property name="triggers">
<list>
<ref bean="hellWordTaskTrigger" />
</list>
</property>
第三部 配置定时作业的类 需要实现spring的QuartzJobBean接口 里面可以写上配置文件targetMethod里面的方法
package com.yeshun.quartz;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class HelloWordTask extends QuartzJobBean {
public void executeAction() {
//to do something...
System.out.println("hello world!!");
}
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
// TODO Auto-generated method stub
}
}
那么启动项目后 每隔十秒就自动打印
hello world!!
附上源码地址点击打开链接