cron 定時任兩種配置方式
阿新 • • 發佈:2017-11-12
card 任務 init 1-1 rop java代碼 exp tor date
第一種:xml文件方式
<bean id="commonTimer" class="com.course.wx.timer.CommonTimer"></bean><!--定時任務Bean --> <bean name="startQuartz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="releaseQuestionTrigger" /> <ref bean="dealHistoryQuestionTrigger" /> </list> </property> </bean> <bean id="dealHistoryQuestionTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="dealHistoryQuestion" /> </property> <property name="cronExpression"> <value>0 23 15 * * ?</value> </property> </bean> <bean id="dealHistoryQuestion" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="commonTimer" /> </property> <property name="targetMethod"> <value>dealHistoryQuestion</value> </property> </bean>
第二種:註解方式
xml配置
<!-- Spring定時器註解開關--> <task:annotation-driven /> <!-- 此處對於定時時間的配置會被註解中的時間配置覆蓋,因此,以註解配置為準 --> <task:scheduled-tasks scheduler="myScheduler"> <task:scheduled ref="scheduledTaskManager" method="autoCardCalculate" cron="* */5 * * * *"/> </task:scheduled-tasks> <task:scheduler id="myScheduler" pool-size="10"/>
Java代碼
@Component("scheduledTaskManager") @Lazy(value=false) public class ScheduledTaskManager { public static final Integer RECOVER = 3; /** * cron表達式:* * * * * *(共6位,使用空格隔開,具體如下) * cron表達式:*(秒0-59) *(分鐘0-59) *(小時0-23) *(日期1-31) *(月份1-12或是JAN-DEC) *(星期1-7或是SUN-SAT) */ @Autowired ProcedureService procedureService; /** * 定時卡點計算。每天淩晨 02:00 執行一次 * @throws AdqException */ @Scheduled(cron = "* */5 * * * *") public void autoCardCalculate() throws AdqException { List<WorkOrder> suspendItems = procedureService.querySuspendItems(); if (suspendItems != null && suspendItems.size() > 0) { for (WorkOrder order : suspendItems) { order.setStateId(RECOVER); order.setNotes("恢復執行"); procedureService.updateState(order); } } } }
spring 定時任務 scheduled Cron表達式
http://blog.csdn.net/u011789653/article/details/51153536
cron 定時任兩種配置方式