需要的jar包
com.springsource.org.apache.commons.logging-1.1.1.jar
commons-collections-3.1.jar
org.springframework.asm-3.0.2.RELEASE.jar
org.springframework.beans-3.0.2.RELEASE.jar
org.springframework.context.support-3.0.2.RELEASE.jar
org.springframework.context-3.0.2.RELEASE.jar
org.springframework.core-3.0.2.RELEASE.jar
org.springframework.expression-3.0.2.RELEASE.jar
org.springframework.transaction-3.0.2.RELEASE.jar
org.springframework.web-3.0.2.RELEASE.jar
quartz-all-1.6.1-RC1.jar
1、web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 配置当web容器启动的时候,自动加载spring容器 -->
<!-- 获取spring容器的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 启动spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2、bean.xml配置文件在bean里定义定时器
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!--
2、 在beans.xml文件中配置实例化CurDateBean的代码
值得注意的是,在这里并没有直接声明一个CurDateBean,而是声明了一个JobDetailBean。
这是使用Quartz时的一个特点。JobDetailBean是Quartz的org.quartz.JobDetail的子类,
它要求通过jobClass属性来设置一个Job对象
-->
<bean id="jobDetailBean" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>
king.quartz.CurDateBean
</value>
</property>
</bean>
<!--
3、调度工作
Quartz的org.quartz.Trigger类描述了何时及以怎样的频度运行一个Quartz工作。Spring提供了两个触发器:
SimpleTriggerBean和CronTriggerBean。
A、SimpleTriggerBean用来指定一个工作应该以怎样的频度运行,以及(可选地)在第一次运行工作之前应该等待多久。
例如,要生成报表工作每24小时运行一次,第一次在1小时之后开始运行 ,该触发器只能指定工作执行的频度,而不能准确指定它于何时运行
B、CronTriggerBean能更精确地控制任务的运行时间。例如:每天早晨6点发送邮件等
-->
<bean id="triggerBean" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="jobDetailBean"></property>
<!-- 设置多长时间后开始执行,单位是毫秒 -->
<property name="startDelay" value="5000"></property>
<!-- 设置多长时间执行一次触发器,单位是毫秒 -->
<property name="repeatInterval" value="3000"></property>
</bean>
<!--
4、启动工作 ,使用Spring的SchedulerFactoryBean类
属性triggers接受一组触发器。
由于目前只有一个触发器,因此只需简单地装配一个包含triggerBean的一个引用的列表即可
-->
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<array>
<ref bean="triggerBean"/>
</array>
</property>
</bean>
</beans>
3、java实现类
package king.quartz;
import java.util.Date;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
/**
* 1、创建一个执行任务的类。要做到这一点,你需要从Spring的QuartzJobBean中派生子类
*/
public class CurDateBean extends QuartzJobBean {
public CurDateBean(){
System.out.println("这是CurDateBean类的构造方法!");
}
/**任务执行的方法,(清理日志,发送短信邮件等)*/
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
System.out.println("执行任务:当前时间是"+new Date());
}
}