一、该方式利弊
该方式通过MethodInvokingJobDetailFactoryBean在运行中动态生成,需要配置执行任务的目标类、目标方法。但是这种方法动态生成的JobBean不支持序列号,也就是说Job不能存到持久化。
该方式通常用于调用特定对象的一个方法。不用创建单独的job对象,只需要建立正常的业务对象,用这样方式去调用其中的一个方法。
该方式通过透明的使用Quartz达到定时任务开发的目的,总体说第二种对开发人员更方便!
二、整合方式二示例步骤
1、jar导入
导入Spring的核心jar包和quartz.jar以及Spring-context-support.jar
2、编写job类(不用继承任何父类)
该类不需要继承任何父类,也不需要实现任何接口,只是一个普通的java类
public class AutoTask { public void checkLine() { //我们的任务执行体 System.out.println("执行任务1"); } public void checkClient() { //我们的任务执行体 System.out.println("执行任务2"); } }
3、编写quartz.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd" default-lazy-init="false"> <!-- task --> <!-- 定义了一个任务JobDetail --> <bean id="quartzLine" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="autoReconciliation" /> <property name="targetMethod" value="checkLine" /> </bean> <bean id="quartzClient" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="autoReconciliation" /> <property name="targetMethod" value="checkClient" /> </bean> <!-- 第二步 调度定时任务 的触发器--> <!-- 这种配置可以精确几点执行定时任务 --> <bean id="cronQuartzLine" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="quartzLine"></ref> </property> <property name="cronExpression"> <value>${lineTime}</value> </property> </bean> <bean id="cronQuartzClient" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="quartzClient"></ref> </property> <property name="cronExpression"> <value>${hanlderTime}</value> </property> <!--<property name="cronExpression" value="*/1 * * * * ?"/> 每隔1秒钟触发一次 --> </bean> <!--第三步 启动定时任务的调度器,注意这里的ref bean --> <bean id="schedulerFactoryBean" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronQuartzLine"></ref> <ref bean="cronQuartzClient"></ref> </list> </property> <!-- <property name="configLocation" value="classpath:quartz.properties"/>--> </bean> <!--这个地方你作业的实现 --> <bean id="autoReconciliation" class="com.huateng.hbmis.task.AutoTask" /> </beans>
说明:
1)Cron表达式的格式:秒 分 时 日 月 周 年(可选)。
字段名 允许的值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日 1-31 , - * ? / L W C
月 1-12 or JAN-DEC , - * /
周几 1-7 or SUN-SAT , - * ? / L C #
年 (可选字段) empty, 1970-2099 , - * /
“?”字符:表示不确定的值
“,”字符:指定数个值
“-”字符:指定一个值的范围
“/”字符:指定一个值的增加幅度。n/m表示从n开始,每次增加m
“L”字符:用在日表示一个月中的最后一天,用在周表示该月最后一个星期X
“W”字符:指定离给定日期最近的工作日(周一到周五)
“#”字符:表示该月第几个周X。6#3表示该月第3个周五
2)Cron表达式范例:
每隔5秒执行一次:*/5 * * * * ?
每隔1分钟执行一次:0 */1 * * * ?
每天23点执行一次:0 0 23 * * ?
每天凌晨1点执行一次:0 0 1 * * ?
每月1号凌晨1点执行一次:0 0 1 1 * ?
每月最后一天23点执行一次:0 0 23 L * ?
每周星期天凌晨1点实行一次:0 0 1 ? * L
在26分、29分、33分执行一次:0 26,29,33 * * * ?
每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ?
4、让容器加载quartz.xml
在web.xml中添加:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/quartz.xml</param-value>
</context-param>
###其实quartz.xml文件的内容完全可以写在applicationContext.xml中的,不过那样会显得杂乱。
三、注意事项
在Spring配置和Quartz集成内容时,有两点需要注意
1、在<Beans>中不能够设置default-lazy-init="true",否则定时任务不触发,如果不明确指明default-lazy-init的值,默认是false。
2、在<Beans>中不能够设置default-autowire="byName"的属性,否则后台会报org.springframework.beans.factory.BeanCreationException错误,这样就不能通过Bean名称自动注入,必须通过明确引用注入。