Quartz 是一个Java的定时任务框架,使用它可以方便地实现计划任务,即在某个时间或是每隔一定时间运行一个任务。
Spring Quartz 类库
spring.jar
quartz-all-1.6.1.jar
log4j-1.2.15.jar
commons-logging.jar
commons-collections.jar
简单应用示例
1.启动类
package com.wisdombrave.spring.quartz;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestMain {
public static void main(String[] args) {
System.out.println("---开始初始化--- ");
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("---完成初始化---");
// 死循环,查看定时高度情况,本例高度为每分钟一次
while (true) {
}
}
}
2.Spring上下文配置(applicationContext.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: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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- bean实例 --> <bean id="testQuartz" class="com.wisdombrave.spring.quartz.TestQuartz" /> <!-- bean触发方法配置 --> <bean name="quartzBean" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- bean名字 --> <property name="targetObject" ref="testQuartz" /> <!-- bean方法 --> <property name="targetMethod"> <value>test</value> </property> <property name="concurrent"> <value>false</value> </property> </bean> <!-- bean触发时间配置 --> <bean id="quartzTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <!-- 触发bean配置 --> <property name="jobDetail"> <ref bean="quartzBean" /> </property> <!-- 触发时间配置 --> <property name="cronExpression"> <value>0/5 * * * * ?</value> </property> </bean> <!-- quartz触发器管理 --> <bean id="sfb" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- 添加触发器 --> <property name="triggers"> <list> <ref local="quartzTrigger" /> </list> </property> </bean> </beans>
WEB应用示例
1.调度类(TestQuartz)
package com.wisdombrave.spring.quartz;
import java.util.Date;
public class TestQuartz {
public void test() {
System.out.println(new Date() + "调用");
}
}
2.Spring应用上下文配置 (applicationContext.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: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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 调度的对象 -->
<bean id="testQuartz" class="com.wisdombrave.spring.quartz.TestQuartz" />
<import resource="springQuartz.xml"/>
</beans>
3. Spring资源文件(springQuartz.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: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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- bean触发方法配置 -->
<bean name="quartzBean"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- bean名字 -->
<property name="targetObject" ref="testQuartz" />
<!-- bean方法 -->
<property name="targetMethod">
<value>test</value>
</property>
<property name="concurrent">
<value>false</value>
</property>
</bean>
<!-- bean触发时间配置 -->
<bean id="quartzTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 触发bean配置 -->
<property name="jobDetail">
<ref bean="quartzBean" />
</property>
<!-- 触发时间配置 -->
<property name="cronExpression">
<value>0/1 * * * * ?</value>
</property>
</bean>
<!-- quartz触发器管理 -->
<bean id="sfb"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 添加触发器 -->
<property name="triggers">
<list>
<ref local="quartzTrigger" />
</list>
</property>
</bean>
</beans>
3.WEB容器配置(web.xml)
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
日期规则
"0 0 12 * * ?" 每天中午12点触发
"0 15 10 ? * *" 每天上午10:15触发
"0 15 10 * * ?" 每天上午10:15触发
"0 15 10 * * ? *" 每天上午10:15触发
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L * ?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发