Quartz是一個完全由java編寫的開源作業調度框架。不要讓作業調度這個術語嚇著你。盡管Quartz框架整合了許多額外功能, 但就其簡易形式看,你會發現它易用得簡直讓人受不了!Quartz整合在sPRing mvc的步驟:
1)準備spring.jar包
2)在WebRoot——>WEB-INF目錄下創建spring-listener.xml文件
3)在該xml文件添加以下代碼:
使用CronTrigger:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans> <bean id="quartzJob" class="com.quartz.CommonQuartz" </bean> <bean id="quartzTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="quartzJob"></ref> </property> <property name="targetMethod"> <value>TaskFunction</value> </property> </bean> <bean id="startQuartzTime" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="quartzTask"></ref> </property> <property name="cronExpression"> <value>*/1 * * * * ? </value> </property> </bean> <bean id="startQuartz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="startQuartzTime"></ref> </list> </property> </bean></beans>
其中cronExpression表達式對特殊字符的大小寫不敏感,對代表星期的縮寫英文大小寫也不敏。
表示式 | 說明 |
"0 0 12 * * ? " | 每天12點運行 |
"0 15 10 ? * *" | 每天10:15運行 |
"0 15 10 * * ?" | 每天10:15運行 |
"0 15 10 * * ? *" | 每天10:15運行 |
"0 15 10 * * ? 2008" | 在2008年的每天10:15運行 |
"0 * 14 * * ?" | 每天14點到15點之間每分鐘運行一次,開始于14:00,結束于14:59。 |
"0 0/5 14 * * ?" | 每天14點到15點每5分鐘運行一次,開始于14:00,結束于14:55。 |
"0 0/5 14,18 * * ?" | 每天14點到15點每5分鐘運行一次,此外每天18點到19點每5鐘也運行一次。 |
"0 0-5 14 * * ?" | 每天14:00點到14:05,每分鐘運行一次。 |
"0 10,44 14 ? 3 WED" | 3月每周三的14:10分到14: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 2007-2009" | 在2007,2008,2009年每個月的最后一個星期五的10:15分運行。 |
"0 15 10 ? * 6#3" | 每月第三個星期五的10:15分運行。 |
<bean id="simpleTriggerTime" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail"> <ref bean="quartzTask"></ref>
</property>
<property name="startDelay" value="0" />
<property name="repeatInterval">
<value>3600000</value>
</property>
</bean>
4)在com.quartz.CommonQuartz類下添加TaskFunction方法,該方法即作業調度方法。
新聞熱點
疑難解答