1 大家都清楚quartz最基本的概念就是job,在job內調用具體service完成具體功能,quartz需要把每個job存儲起來,方便調度,quartz存儲job方式就分三種,我們最常用的也是quartz默認的是RAMJobStore,RAMJobStore顧名思義就是把job的相關信息存儲在內存裏,如果用spring配置quartz的job信息的話,所有信息是配置在xml裏,當spirng context啓動的時候就把xml裏的job信息裝入內存。這一性質就決定了一旦JVM掛掉或者容器掛掉,內存中的job信息就隨之消失,無法持久化。另外兩種方式是JobStoreTX和JobStoreCMT,暫時不討論這兩者的區別,使用這兩種JobStore,quartz就會通過jdbc直連或者應用服務器jndi連接數據庫,讀取配置在數據庫裏的job初始化信息,並且把job通過java序列化到數據庫裏,這樣就使得每個job信息得到了持久化,即使在jvm或者容器掛掉的情況下,也能通過數據庫感知到其他job的狀態和信息。
2 quartz集羣各節點之間是通過同一個數據庫實例(準確的說是同一個數據庫實例的同一套表)來感知彼此的。
由上可見,我們需要創建quartz要用的數據庫表,此sql文件在:quartz-1.8.6\docs\dbTables。此文件夾下有各個數據庫的sql文件,mysql選擇tables_mysql.sql。創建相應表。
接下來新建quartz.properties來覆蓋jar包中的此文件,新的properties文件放在src的根目錄下即可。下面是文件內容:
- #==============================================================
- #Configure Main Scheduler Properties
- #==============================================================
- org.quartz.scheduler.instanceName = quartzScheduler
- org.quartz.scheduler.instanceId = AUTO
- #==============================================================
- #Configure JobStore
- #==============================================================
- org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
- org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
- org.quartz.jobStore.tablePrefix = QRTZ_
- org.quartz.jobStore.isClustered = true
- org.quartz.jobStore.clusterCheckinInterval = 20000
- org.quartz.jobStore.dataSource = myDS
- #==============================================================
- #Configure DataSource
- #==============================================================
- org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
- org.quartz.dataSource.myDS.URL = jdbc:mysql://192.168.20.195:3306/database?useUnicode=true&characterEncoding=UTF-8
- org.quartz.dataSource.myDS.user = root
- org.quartz.dataSource.myDS.password = 123456
- org.quartz.dataSource.myDS.maxConnections = 30
- #==============================================================
- #Configure ThreadPool
- #==============================================================
- org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
- org.quartz.threadPool.threadCount = 10
- org.quartz.threadPool.threadPriority = 5
- org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
可以看到除了數據源、線程池等配置外,我們指定了一個scheduler實例,實例ID爲自動分配。
- #==============================================================
- #Configure Main Scheduler Properties
- #==============================================================
- org.quartz.scheduler.instanceName = quartzScheduler
- org.quartz.scheduler.instanceId = AUTO
此外,指定了集羣相應配置,檢查間隔爲20s:
- org.quartz.jobStore.isClustered = true
- org.quartz.jobStore.clusterCheckinInterval = 20000
最後配置applicant-context.xml文件。這裏特別要注意一點:
所以我們要自己實現MethodInvokingJobDetailFactoryBean 的功能,這裏用MyDetailQuartzJobBean 替換。
- import java.lang.reflect.Method;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.scheduling.quartz.QuartzJobBean;
- public class MyDetailQuartzJobBean extends QuartzJobBean {
- protected final Log logger = LogFactory.getLog(getClass());
- private String targetObject;
- private String targetMethod;
- private ApplicationContext ctx;
- @Override
- protected void executeInternal(JobExecutionContext context)
- throws JobExecutionException {
- try {
- logger.info("execute [" + targetObject + "] at once>>>>>>");
- Object otargetObject = ctx.getBean(targetObject);
- Method m = null;
- try {
- m = otargetObject.getClass().getMethod(targetMethod, new Class[] {JobExecutionContext.class});
- m.invoke(otargetObject, new Object[] {context});
- } catch (SecurityException e) {
- logger.error(e);
- } catch (NoSuchMethodException e) {
- logger.error(e);
- }
- } catch (Exception e) {
- throw new JobExecutionException(e);
- }
- }
- public void setApplicationContext(ApplicationContext applicationContext) {
- this.ctx = applicationContext;
- }
- public void setTargetObject(String targetObject) {
- this.targetObject = targetObject;
- }
- public void setTargetMethod(String targetMethod) {
- this.targetMethod = targetMethod;
- }
終於到配置spring文件這步了
- <bean id="mapScheduler" lazy-init="false" autowire="no"
- class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="triggers">
- <list>
- <ref bean="dailyTrigger" />
- <ref bean="billCountTrigger" />
- <ref bean="userAcctTrigger" />
- </list>
- </property>
- <property name="applicationContextSchedulerContextKey" value="applicationContext" />
- <property name="configLocation" value="classpath:quartz.properties" />
- </bean>
- <bean id="dailyBillJob" class="com.***.job.DailyBillJob" />
- <bean id="dailyBillJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
- <property name="jobClass">
- <value>com.autelan.auteview.lib.util.MyDetailQuartzJobBean
- </value>
- </property>
- <property name="jobDataAsMap">
- <map>
- <entry key="targetObject" value="dailyBillJob" />
- <entry key="targetMethod" value="execute" />
- </map>
- </property>
- </bean>
- <bean id="dailyTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
- <property name="jobDetail">
- <ref bean="dailyBillJobDetail" />
- </property>
- <property name="cronExpression">
- <value>11 11 11 * * ?</value>
- </property>
- </bean>
注:
1,如果報錯, java.lang.NoSuchMethodException沒有execute(org.quartz.JobExecutionContext),是需要一個參數爲JobExecutionContext類型的execute方法,在裏面加個參數就好了。
2,如果測試成功後,再添加新的任務時,需要重新執行數據庫腳本,否則無法加載新的任務。