导入Jar包
基于包含Spring的项目中,需要引入quartz的jar包:[quartz-2.2.1.jar]
开发定时器类
class TriggerUtil{ public triggerUtil(){ } public targetMonitorTask(){ System.out.println("Hello Trigger"); } }
配置定时任务
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:task="http://www.springframework.org/schema/task" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.5.xsd "> <!-- 数据库检测定时器 --> <bean id="targetMonitorDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="triggerUtil" /> </property> <property name="targetMethod"> <value>targetMonitorTask</value> </property> </bean> <!-- 定义触发时间 --> <bean id="targetMonitorTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="targetMonitorDetail" /> </property> <!-- 每 3 分钟触发一次 --> <property name="cronExpression"> <value>0 0/3 * * * ?</value> </property> </bean> <!-- 定时器执行集合 --> <bean lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <!-- 这里添加多个定时任务触发器,对应第2步的bean id 触发时间 <ref bean="cleanDetailTask" /> <ref bean="exportCardNOTask" /> --> <ref bean="targetMonitorTrigger" /> </list> </property> </bean> </beans>
附:query 定时器的时间设置
时间的配置如下:<value>0 26 16 * * ?</value>
时间大小由小到大排列,从秒开始,顺序为 秒,分,时,天,月,年 *为任意 ?为无限制。 由此上面所配置的内容就是,在每天的16点26分启动buildSendHtml() 方法
具体时间设定可参考
"0/10 * * * * ?" 每10秒触发
"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触发
"0 0 06,18 * * ? " 在每天上午6点和下午6点触发
"0 30 5 * * ? * " 在每天上午5:30触发
"0 0/3 * * * ? " 每3分钟触发
转载于:https://my.oschina.net/idarex/blog/667362