Spring Quartz定時器
阿新 • • 發佈:2019-01-28
而在Spring裡很好的集成了Quartz,在xml檔案裡面配一下時間就可以自動執行,不需要寫一行程式碼。
Xml程式碼
confirmOrder定時執行的方法
0 0 6,12,20 * * ? 執行時間,一般寫在配置檔案中用${updateDate};
其中時間的定義按以下例子模仿
表示式
Java程式碼
如果出現了“Table 'database.qrtz_locks' doesn't exist”異常
有兩種處理方法
方法1:
則處理方法為在宣告中的
Xml程式碼
改為
Xml程式碼
方法2:
配置檔案在<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
多個autowire=no 屬性,如下
Xml程式碼
Xml程式碼
- <bean id="methodInvokingJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
- <property name="targetObject"><ref bean="financeDAO"/></property>
-
<
- </bean> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
- <property name="jobDetail">
-
<ref bean="methodInvokingJobDetail"/>
- </property>
- <property name="cronExpression">
- <value>0 0 6,12,20 * * ?</value>
- </property>
- </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="triggers">
-
<
- </property>
- </bean>
- <property name="targetObject"><ref bean="financeDAO"/></property>
- <property name="targetMethod"><value>confirmOrder</value></property>
confirmOrder定時執行的方法
0 0 6,12,20 * * ? 執行時間,一般寫在配置檔案中用${updateDate};
其中時間的定義按以下例子模仿
表示式
Java程式碼
- 意義
- "0/10 * * * * ?" 每十秒觸發
- "0 0/1 * * * ?" 每一分鐘觸發
- "0 0 12 * * ?" 每天中午12點觸發
- "0 15 10 ? * *" 每天上午10:15觸發
- "0 15 10 * * ?" 每天上午10:15觸發
- "0 15 10 * * ? *" 每天上午10:15觸發
- "0 15 10 * * ? 2005" 2005年的每天上午10:15觸發
- "0 * 14 * * ?" 在每天下午2點到下午2:59期間的每1分鐘觸發
- "0 0/5 14 * * ?" 在每天下午2點到下午2:55期間的每5分鐘觸發
- "0 0/5 14,18 * * ?" 在每天下午2點到2:55期間和下午6點到6:55期間的每5分鐘觸發
- "0 0-5 14 * * ?" 在每天下午2點到下午2:05期間的每1分鐘觸發
- "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44觸發
- "0 15 10 ? * MON-FRI" 週一至週五的上午10:15觸發
- "0 15 10 15 * ?" 每月15日上午10:15觸發
- "0 15 10 L * ?" 每月最後一日的上午10:15觸發
- "0 15 10 ? * 6L" 每月的最後一個星期五上午10:15觸發
- "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最後一個星期五上午10:15觸發
- "0 15 10 ? * 6#3" 每月的第三個星期五上午10:15觸發
如果出現了“Table 'database.qrtz_locks' doesn't exist”異常
有兩種處理方法
方法1:
則處理方法為在宣告中的
Xml程式碼
- default-autowire=byName
改為
Xml程式碼
- default-autowire="byName"
方法2:
配置檔案在<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
多個autowire=no 屬性,如下
Xml程式碼
- <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" autowire="no" >