Quartz集羣配置

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/erdongritian/article/details/49330029
先看看quartz的持久化基本介紹: 
引用

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的根目錄下即可。下面是文件內容: 

Java代碼  收藏代碼
  1. #==============================================================    
  2. #Configure Main Scheduler Properties    
  3. #==============================================================     
  4. org.quartz.scheduler.instanceName = quartzScheduler  
  5. org.quartz.scheduler.instanceId = AUTO  
  6.   
  7. #==============================================================    
  8. #Configure JobStore    
  9. #==============================================================   
  10. org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX  
  11. org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate  
  12. org.quartz.jobStore.tablePrefix = QRTZ_  
  13. org.quartz.jobStore.isClustered = true  
  14. org.quartz.jobStore.clusterCheckinInterval = 20000    
  15. org.quartz.jobStore.dataSource = myDS  
  16.    
  17. #==============================================================    
  18. #Configure DataSource    
  19. #==============================================================   
  20. org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver  
  21. org.quartz.dataSource.myDS.URL = jdbc:mysql://192.168.20.195:3306/database?useUnicode=true&characterEncoding=UTF-8  
  22. org.quartz.dataSource.myDS.user = root  
  23. org.quartz.dataSource.myDS.password = 123456  
  24. org.quartz.dataSource.myDS.maxConnections = 30  
  25.   
  26. #==============================================================    
  27. #Configure ThreadPool    
  28. #==============================================================   
  29. org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool  
  30. org.quartz.threadPool.threadCount = 10  
  31. org.quartz.threadPool.threadPriority = 5  
  32. org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true  


可以看到除了數據源、線程池等配置外,我們指定了一個scheduler實例,實例ID爲自動分配。 
Java代碼  收藏代碼
  1. #==============================================================    
  2. #Configure Main Scheduler Properties    
  3. #==============================================================     
  4. org.quartz.scheduler.instanceName = quartzScheduler  
  5. org.quartz.scheduler.instanceId = AUTO  


此外,指定了集羣相應配置,檢查間隔爲20s: 
Java代碼  收藏代碼
  1. org.quartz.jobStore.isClustered = true  
  2. org.quartz.jobStore.clusterCheckinInterval = 20000    


最後配置applicant-context.xml文件。這裏特別要注意一點: 
引用
MethodInvokingJobDetailFactoryBean 類中的 methodInvoking 方法,是不支持序列化的,因此在把 QUARTZ 的 TASK 序列化進入數據庫時就會拋錯。

所以我們要自己實現MethodInvokingJobDetailFactoryBean 的功能,這裏用MyDetailQuartzJobBean 替換。 

Java代碼  收藏代碼
  1. import java.lang.reflect.Method;  
  2.   
  3. import org.apache.commons.logging.Log;  
  4. import org.apache.commons.logging.LogFactory;  
  5. import org.quartz.JobExecutionContext;  
  6. import org.quartz.JobExecutionException;  
  7. import org.springframework.context.ApplicationContext;  
  8. import org.springframework.scheduling.quartz.QuartzJobBean;  
  9.   
  10. public class MyDetailQuartzJobBean extends QuartzJobBean {  
  11.     protected final Log logger = LogFactory.getLog(getClass());  
  12.     private String targetObject;  
  13.     private String targetMethod;  
  14.     private ApplicationContext ctx;  
  15.   
  16.     @Override  
  17.     protected void executeInternal(JobExecutionContext context)  
  18.             throws JobExecutionException {  
  19.         try {  
  20.             logger.info("execute [" + targetObject + "] at once>>>>>>");  
  21.             Object otargetObject = ctx.getBean(targetObject);  
  22.             Method m = null;  
  23.   
  24.             try {  
  25.                 m = otargetObject.getClass().getMethod(targetMethod, new Class[] {JobExecutionContext.class});  
  26.                 m.invoke(otargetObject, new Object[] {context});  
  27.             } catch (SecurityException e) {  
  28.                 logger.error(e);  
  29.             } catch (NoSuchMethodException e) {  
  30.                 logger.error(e);  
  31.             }  
  32.         } catch (Exception e) {  
  33.             throw new JobExecutionException(e);  
  34.         }  
  35.     }  
  36.   
  37.     public void setApplicationContext(ApplicationContext applicationContext) {  
  38.         this.ctx = applicationContext;  
  39.     }  
  40.   
  41.     public void setTargetObject(String targetObject) {  
  42.         this.targetObject = targetObject;  
  43.     }  
  44.   
  45.     public void setTargetMethod(String targetMethod) {  
  46.         this.targetMethod = targetMethod;  
  47.     }  


終於到配置spring文件這步了 
Java代碼  收藏代碼
  1. <bean id="mapScheduler" lazy-init="false" autowire="no"  
  2.         class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  3.         <property name="triggers">  
  4.             <list>  
  5.                 <ref bean="dailyTrigger" />  
  6.                 <ref bean="billCountTrigger" />  
  7.                 <ref bean="userAcctTrigger" />  
  8.             </list>  
  9.         </property>  
  10.         <property name="applicationContextSchedulerContextKey" value="applicationContext" />  
  11.         <property name="configLocation" value="classpath:quartz.properties" />  
  12.     </bean>  
  13.   
  14.   
  15.     <bean id="dailyBillJob" class="com.***.job.DailyBillJob" />  
  16.   
  17.     <bean id="dailyBillJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">  
  18.         <property name="jobClass">  
  19.             <value>com.autelan.auteview.lib.util.MyDetailQuartzJobBean  
  20.             </value>  
  21.         </property>  
  22.         <property name="jobDataAsMap">  
  23.             <map>  
  24.                 <entry key="targetObject" value="dailyBillJob" />  
  25.                 <entry key="targetMethod" value="execute" />  
  26.             </map>  
  27.         </property>  
  28.     </bean>  
  29.   
  30.     <bean id="dailyTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
  31.         <property name="jobDetail">  
  32.             <ref bean="dailyBillJobDetail" />  
  33.         </property>  
  34.         <property name="cronExpression">  
  35.             <value>11 11 11 * * ?</value>  
  36.         </property>  
  37.     </bean>  
  38.  

注:

1,如果報錯, java.lang.NoSuchMethodException沒有execute(org.quartz.JobExecutionContext),是需要一個參數爲JobExecutionContext類型的execute方法,在裏面加個參數就好了。

2,如果測試成功後,再添加新的任務時,需要重新執行數據庫腳本,否則無法加載新的任務。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章