最近項目使用任務調度的功能很多,將spring集成任務調度的配置記錄一下,以備不時之需。
需要的jar包:quartz-1.5.2.jar(spring的jar包就不用說了)
配置如下:
<!--任務調度配置-->
<!--定義jobDetail,定時執行createFileStatusService這個bean中的deleteAndDownloadProgram方法-->
<bean id="defineJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!--調度的對象-->
<property name="targetObject">
<ref bean="createFileStatusService"/>
</property>
<!--調度對象的方法-->
<property name="targetMethod">
<value>deleteAndDownloadProgram</value>
</property>
</bean>
<!--觸發器設置,設置觸發的jobDetail是defineJobDetail,觸發的時間爲每天凌晨2:00-->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="defineJobDetail"/>
</property>
<property name="cronExpression">
<!--觸發時間表達式,從左到右,秒、分、時、日、月、星期,*號爲通配符,?號爲不設置該字段-->
<value>0 0 2 * * ?</value>
</property>
</bean>
<!--管理觸發器列表,可以在bean的list中放置多個觸發器-->
<bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="cronTrigger" />
</list>
</property>
</bean>