该文章属于本人原创,转载请注明出处。
spring + Quartz 设置定时任务时要求没一个半小时执行一次
设置两个相同的定时任务
第一个从整点开始每三小时执行一次
<!--每三小时执行一次任务,从整点开始-->
<property name ="cronExpression" value="0 0 0/3 * * ?"></property>
第二个从一个半小时后开始每三小时执行一次
<!--每三小时执行一次任务,从一个半小时后开始 -->
<property name ="cronExpression" value="0 30 1/3 * * ?"></property>
spring核心代码示例:
<!--获取demoTask定时任务start --> <bean id ="demoTask" class="com.bdx.itpl.wcpay.quartz.task.OrdinaryAccessTokenAndJsApiTicketTask" /> <bean id ="demoMethod" class= "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" > <property name ="targetObject"> <ref bean ="demoTask"/> </property > <property name ="targetMethod"> <value >execute </value ><!-- 要执行的方法名称 --> </property > </bean > <bean id ="demoBean" class= "org.springframework.scheduling.quartz.CronTriggerFactoryBean" > <property name ="jobDetail" ref= "demoMethod" ></property > <!--每三小时执行一次任务,从整点开始--> <property name ="cronExpression" value="0 0 0/3 * * ?"></property> </bean > <bean id ="demoBean2" class= "org.springframework.scheduling.quartz.CronTriggerFactoryBean" > <property name ="jobDetail" ref= "demoMethod" ></property > <!--每三小时执行一次任务,从一个半小时后开始 --> <property name ="cronExpression" value="0 30 1/3 * * ?"></property> </bean > <!--获取OrdinaryAccessTokenAndJsApiTicketTask定时任务end--> <!-- ======================== 调度工厂 ======================== --> <bean id ="SpringJobSchedulerFactoryBean" class= "org.springframework.scheduling.quartz.SchedulerFactoryBean" > <property name ="triggers"> <list > <ref bean= "demoBean" /> <ref bean= "demoBean2" /> </list > </property >
时间: 2024-10-06 06:18:42