java代码:
package com.malq;
import java.util.Date;
public class SayHelloJob {
public void sayHello(){
Date date = new Date();
System.out.println(date +" ::::: ------this is SayHelloJob!");
}
}
package com.malq;
import java.util.Date;
public class MyQuartzJobBean{
public void sayHello(){
Date date = new Date();
System.out.println(date +" ::::: ------this is MyQuartzJobBean!");
}
}
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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class><!--这句一定要加上,不然不会起作用-->
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
spring配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="simpleHelloTest" class="com.malq.SayHelloJob" />
<bean id="helloTest" class="com.malq.MyQuartzJobBean" />
<bean name="simpleSayHelloDetailBean" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="simpleHelloTest" />
<property name="targetMethod" value="sayHello"/>
</bean>
<bean name="sayHelloDetailBean" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="helloTest" />
<property name="targetMethod" value="sayHello"/>
</bean>
<bean name="simpleTriggerBean" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<!--这里定义定时任务的对象的位置-->
<property name="jobDetail" ref="simpleSayHelloDetailBean"/>
<!--这里定义每六秒钟程序执行一次-->
<property name="repeatInterval" value="30000"/>
<!--这里定义程序启动两秒钟后开始执行-->
<property name="startDelay" value="1000"/>
</bean>
<bean id="helloCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="sayHelloDetailBean"/>
</property>
<property name="cronExpression">
<!-- 关键在配置此表达式 -->
<value>0 0/1 * * * ?</value>
</property>
</bean>
<bean name="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTriggerBean"/>
<ref bean="helloCronTrigger"/>
</list>
</property>
</bean>
</beans>
jar包,注意:commons-collections-3.1.jar 有网友说不能是2.1的。