spring定时任务处理的类有两个:
org.springframework.scheduling.quartz.CronTriggerBean
一般是定时的调度,比如要求每天的早上6点10分执行一次或每月的2号晚上6点执行一次,当然也可以配置1个小时内每5分钟执行一次,等...
org.springframework.scheduling.quartz.SimpleTriggerBean
一般是循环执行,比如应用启动后10秒钟开始,每10分钟执行一次,等等...
下面是配置:
<!-- 定时任务调用的类ProcessFileUitl 配置 -->
<bean id="dealBillTask" class="billDeal.business.ProcessFileUitl">
<property name="dealBill">
<ref bean="dealBill" />
</property>
</bean>
<!-- 配置调用的spring的定时任务 -->
<bean id="dealBillJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类,对应上面的配置的bean id -->
<property name="targetObject">
<ref bean="dealBillTask" />
</property>
<!-- 调用的类里的方法名,类ProcessFileUitl 里面有方法autoDealFile,也就是入口 -->
<property name="targetMethod">
<value>autoDealFile</value>
</property>
</bean>
<!-- 调用定时任务的类型,上面提到的两种,只能用其中一种 -->
<!-- 第一种:-->
<bean id="dealBillTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<!-- see the example of method invoking job above -->
<ref bean="dealBillJobDetail" />
</property>
<property name="startDelay">
<!-- 应用启动后 10 seconds -->
<value>60000</value>
</property>
<property name="repeatInterval">
<!-- repeat every 500 seconds -->
<value>72000000</value>
</property>
</bean>
<!-- 第二种:-->
<bean id="dealBillTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<!-- see the example of method invoking job above -->
<ref bean="dealBillJobDetail" />
</property>
<property name="cronExpression">
<!-- 代表 每月2号 18点执行一次 -->
<value>0 0 18 2 * ?</value>
<!-- 测试可用 每天18时执行 每5分钟执行一次 (0/5)
<value>0 0/5 18 * * ?</value> -->
</property>
</bean>
<!-- 配置应用的定时机制 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="logTrigger" />
<ref local="dealBillTrigger" />
</list>
</property>
</bean>