spring定时任务Quartz多任务配置

时间:2022-11-06 19:04:06

突然感觉自身虚无缥缈,全身没有力气......很多往事想不起来,记录下来已经不可或缺

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 要调用的工作类 :定时任务:定时执行订单分仓任务-->
<bean id="calculateOrdersTask" class="com.epeit.api.service.inner.CalculateOrdersTask"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="calculateOrdersJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 目标类 -->
<property name="targetObject">
<ref bean="calculateOrdersTask"/>
</property>
<!-- 目标类中的目标方法 -->
<property name="targetMethod">
<value>calculateOrders</value>
</property>
<!--指定concurrent设为false,多个job不会并发运行,第二个job将不会在第一个job完成之前开始-->
<property name="concurrent" value="false"/>
</bean>
<!-- 定义触发时间 -->
<bean id="calculateOrdersTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!--给定时任务分发开始时间-->
<property name="jobDetail">
<ref bean="calculateOrdersJob"/>
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<value>0 30 8,14 * * ?</value>
</property>
</bean>


<!-- 要调用的工作类 :定时任务:定时恢复库存-->
<bean id="recoveryStockTask" class="com.epeit.api.service.inner.RecoveryStockTask"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="recoveryStockJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="recoveryStockTask"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>doRecoveryStock</value>
</property>
<property name="concurrent" value="false"/>
</bean>
<!-- 定义触发时间 -->
<bean id="recoveryStockTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="recoveryStockJob"/>
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<value>0 0 00 * * ?</value>
</property>
</bean>


<bean id="pushTask" class="com.epeit.api.service.tomcat.PushTask"></bean>
<bean id="pushJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="pushTask"/>
</property>
<property name="targetMethod">
<value>push</value>
</property>
<property name="concurrent" value="false"/>
</bean>
<bean id="pushTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="pushJob"/>
</property>
<!-- cron表达式 每天-->
<property name="cronExpression">
<value>0 0 8 * * ?</value>
<!--<value>10 * * * * ?</value>-->
</property>
</bean>

<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuartz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="calculateOrdersTime"/>
<ref bean="recoveryStockTime"/>
<ref bean="pushTime"/>
</list>
</property>
</bean>

</beans>