Spring + Quartz
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<task:executor id="threadPoolTaskExecutor" pool-size="1" />
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobFactory">
<bean class="org.springframework.scheduling.quartz.SpringBeanJobFactory" />
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<property name="startupDelay" value="0" />
<property name="overwriteExistingJobs" value="true" />
<property name="exposeSchedulerInRepository" value="true" />
<property name="taskExecutor" ref="threadPoolTaskExecutor" />
<property name="triggers">
<list>
<ref bean="cronTrigger_1" />
</list>
</property>
</bean>
<bean id="cronTrigger_1"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail_1" />
<property name="cronExpression" value="* * * * * ?" />
</bean>
<bean id="jobDetail_1"
class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="cn.zno.job.Breathe" />
</bean>
<bean id="breath" class="cn.zno.job.Breathe" />
</beans>
详解:
applicationContextSchedulerContextKey
Register ApplicationContext in Scheduler context.
从Scheduler上下文中获取ApplicationContext, 通过其指定的key获取。
startupDelay
延迟秒
overwriteExistingJobs
重写已存在的任务(我认为会更新数据库)
dataSource
设置数据源
configLocation
设置配置文件例如:classpath:quartz/quartz.properties
exposeSchedulerInRepository
监控
taskExecutor
执行器
triggers
触发器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 一个cron表达式有到少6个(也可能是7个)由空格分隔的时间元素.从左到右,这些元素的定义如下:
1.秒(0-59)
2.分钟(0-59)
3.小时(0-23)
4.月份中的是期(1-31)
5.月份(1-12或SUN-DEC)
6.星期中的日期(1-7或SUN-SAT)
7.年份(1970-2099)
例子:
0 0 10,14,16 * * ? 每天上午10点,下午2点和下午4点
0 0,15,30,45 * 1-10 * ? 每月前10天每隔15分钟
30 0 0 1 1 ? 2012 在2012年1月1日午夜过30秒时
0 0 8-5 ? * MON-FRI 每个工作日的工作时间
- 区间
* 通配符
? 你不想设置那个字段
-->
<!--每一秒钟执行的定时任务 -->
<bean id="forOneSTimer" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="durability" value="true"/>
<property name="jobClass" value="com.by.business.timer.ForOneSTimer" />
</bean>
<bean id="forOneSTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="forOneSTimer" />
<property name="cronExpression">
<value>0/1 * * * * ?</value><!--每一秒钟执行一次 -->
</property>
</bean>
<!--每一分钟执行的定时任务 -->
<bean id="forOneMTimer" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="durability" value="true"/>
<property name="jobClass" value="com.by.business.timer.ForOneMTimer" />
</bean>
<bean id="forOneMTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="forOneMTimer" />
<property name="cronExpression">
<value>0 0/1 * * * ?</value><!--每一分钟执行一次 -->
</property>
</bean>
<!--每十分钟执行一次的定时任务-->
<bean id="forTenMTimer" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="durability" value="true"/>
<property name="jobClass" value="com.by.business.timer.ForTenMTimer" />
</bean>
<bean id="forTenTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="forTenMTimer" />
<property name="cronExpression">
<value>0 0/10 * * * ?</value><!--每10分钟执行一次 -->
</property>
</bean>
<!--每小时执行的定时任务 -->
<bean id="forHourTimer" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="durability" value="true"/>
<property name="jobClass" value="com.by.business.timer.ForHourTimer" />
</bean>
<bean id="forHourTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="forHourTimer" />
<property name="cronExpression">
<value>0 0 0/1 * * ?</value><!--每小时执行一次 -->
</property>
</bean>
<!--每天晚上执行的定时任务-->
<bean id="dayLateTimer" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="durability" value="true"/>
<property name="jobClass" value="com.by.business.timer.DayLateTimer" />
</bean>
<bean id="dayLateTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="dayLateTimer" />
<property name="cronExpression">
<value>0 0/1 * * * ?</value><!--每天晚上11点执行 -->
</property>
</bean>
<!--每天凌晨执行的定时任务 -->
<bean id="dayWeeTimer" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="durability" value="true"/>
<property name="jobClass" value="com.by.business.timer.DayWeeTimer" />
</bean>
<bean id="dayWeeTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="dayWeeTimer" />
<property name="cronExpression">
<value>0 0 6 * * ?</value><!--每天凌晨6点执行 -->
</property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" autowire="no">
<property name="triggers">
<list>
<!--<ref bean="forOneSTrigger"></ref>-->
<ref bean="forOneMTrigger"></ref>
<!--<ref bean="forTenTrigger"></ref>-->
<!--<ref bean="forHourTrigger"></ref>-->
<!--<ref bean="dayLateTrigger"></ref>-->
<!--<ref bean="dayWeeTrigger"></ref>-->
</list>
</property>
</bean>
</beans>