SpringMvc整合Quartz实现定时任务项目源码

来源:互联网 发布:sql sp password 编辑:程序博客网 时间:2024/06/16 06:09
         项目中一直使用Quartz做定时任务,但是一直没有去仔细阅读详细参数配置说明以及它源码,最近花了一点时间做了一个整理。系统的看了一下源码实现并顺手做了一个简单的Demo。

一、说明

spring版本4.0.5、Quartz版本2.2.2
注意定时任务的配置需要spring-context-support-4.0.5.RELEASE.jar进行配置,所以项目中要加入此包。还需要log4j的包,这里版本用的是log4j-1.2.17.jar,slf4j-log4j12-1.6.1.jar,slf4j-api-1.6.4.jar当然还有一些依赖包,项目中都有,这里不再做说明。

二、安装配置

1)JOBXML配置

<?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/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            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName" default-lazy-init="true">                         <bean id="testJob" class="quartz.JobTest"></bean>             <!-- 定义调用对象和调用对象的方法 -->        <bean id="testTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">            <!-- 调用的类 -->            <property name="targetObject">                <ref bean="testJob"/>            </property>            <!-- 调用类中的方法 -->            <property name="targetMethod">                <value>work</value>            </property>        </bean>          <!-- 调度触发器 -->        <bean id="jibDoTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">            <property name="jobDetail">                <ref bean="testTask"/>            </property>            <!-- cron表达式 -->            <property name="cronExpression">                                        <value>0 0/1 * * * ?</value>            </property>        </bean>         <!-- 调度工厂  如果将lazy-init='false'那么容器启动就会执行调度程序 -->        <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">            <property name="triggers">                <list>                    <ref bean="jibDoTime"/>                </list>            </property>            <!--必须的设置 QuartzScheduler 延时启动,应用启动完后 QuartzScheduler 再启动 -->            <property name="startupDelay" value="5" />      </bean>      <!-- 配置任务并发执行线程池 -->          <bean id="executor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">                <property name="corePoolSize" value="3" />                <property name="maxPoolSize" value="5" />                <property name="queueCapacity" value="10" />          </bean></beans>

2)代码实现

package quartz;public class JobTest {        public void work(){                System.out.println("任务开始执行");        }}

3)web.xml配置

<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" 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">        <display-name>acts_quartz</display-name>        <context-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:applicationContext-job.xml</param-value>    </context-param>     <!-- 监听器 -->         <listener>                <listener-class>                                org.springframework.web.context.ContextLoaderListener                        </listener-class>         </listener>        <welcome-file-list>                <welcome-file>index.html</welcome-file>        </welcome-file-list></web-app>

关于cronExpression表达式: 
字段 允许值 允许的特殊字符 
秒 0-59 , - * / 
分 0-59 , - * / 
小时 0-23 , - * / 
日期 1-31 , - * ? / L W C 
月份 1-12 或者 JAN-DEC , - * / 
星期 1-7 或者 SUN-SAT , - * ? / L C # 
年(可选) 留空, 1970-2099 , - * / 
表达式意义 
"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触发 
每天早上6点 
0 6 * * * 
每两个小时 
0 */2 * * * 
晚上11点到早上8点之间每两个小时,早上八点 
0 23-7/2,8 * * * 
每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点 
0 11 4 * 1-3 
1月1日早上4点 
0 4 1 1 * 

科帮网

1 0
原创粉丝点击