ECM
ECM
总版主
总版主
  • 社区居民
  • 忠实会员
  • 原创写手
阅读:950回复:0

Quartz 2.2 multi scehduler and @DisallowConcurrentExecution

楼主#
更多 发布于:2016-05-16 10:53
Please consider this example. A sample web application calls scheduler.start() on its start up. The scheduler configured to store its jobs in DB. The application is copied on six web-servers. So if we start six web-servers we will have six scheduler with same name on a single DB. As mentioned in https://quartz-scheduler.org/documentation/quartz-2.1.x/cookbook/MultipleSchedulers: Never start (scheduler.start()) a non-clustered instance against the same set of database tables that any other instance with the same scheduler name is running (start()ed) against. You may get serious data corruption, and will definitely experience erratic behavior. So this will fail. My question is that if I am sure that all of my jobs have @DisallowConcurrentExecution will above work or it will still fail ?! If @DisallowConcurrentExecution does not help, I should do it manually configure one server as some how Master public class StartUp implements ServletContextListener {

   public void contextInitialized(ServletContextEvent event) {
       if(THIS_IS_MASTER_TOMCAT){
         scheduler.start()
       }
}
 Are there better ways?!
answer(解决方案):

Basically Rene M. is correct. Here are the docs in question vis-a-vis Quartz: http://quartz-scheduler.org/generated/2.2.1/html/qs-all/#page/Quartz_Scheduler_Documentation_Set%2Fco-adv_clustering.html Now some background and a conceptual example from our own use at my company. We use quartz clustering mode within a Wildfly Cluster. That is each wildfly cluster node runs quartz. Since quartz is running in cluster mode itself and is pointed at the same database schema we are guaranteed to run one job per cluster. Again, see the documentation. The key issues are this: A single quartz cluster must run against a single quartz database schema. You obviously must create the relational database tables per the documentation. No biggie. You must set the quartz.property files properly and a copy of which must exist for each node in the cluster. The same exact quartz.property file Lastly you must use a NonJTA datasource otherwise the quartz cluster will fail. That often means in Wildfly world quartz will require its own datasource. quartz.property example:     #============================================================================
# Configure Main Scheduler Properties
#============================================================================

org.quartz.scheduler.instanceName = BjondScheduler
org.quartz.scheduler.instanceId = AUTO

#============================================================================
# Configure ThreadPool
#============================================================================

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 5

#============================================================================
# Configure JobStore
#============================================================================

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreCMT
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.tablePrefix=QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 5000

org.quartz.scheduler.wrapJobExecutionInUserTransaction = true
org.quartz.scheduler.userTransactionURL = java:jboss/UserTransaction

org.quartz.jobStore.dataSource = PostgreSQLDS
org.quartz.jobStore.nonManagedTXDataSource = PostgreSQLDSNoJTA

org.quartz.dataSource.PostgreSQLDSNoJTA.jndiURL=java:jboss/datasources/PostgreSQLDSNoJTA
org.quartz.dataSource.PostgreSQLDS.jndiURL=java:jboss/datasources/PostgreSQLDS


#============================================================================
# Configure Logging
#============================================================================
#org.quartz.plugin.jobHistory.class=org.quartz.plugins.history.LoggingJobHistoryPlugin
#org.quartz.plugin.jobHistory.jobToBeFiredMessage=Bjond Job [{1}.{0}] to be fired by trigger [{4}.{3}] at: {2, date, HH:mm:ss MM/dd/yyyy} re-fire count: {7}
#org.quartz.plugin.jobHistory.jobSuccessMessage=Bjond Job [{1}.{0}] execution complete and reports: {8}
#org.quartz.plugin.jobHistory.jobFailedMessage=Bjond Job [{1}.{0}] execution failed with exception: {8}
#org.quartz.plugin.jobHistory.jobWasVetoedMessage=Bjond Job [{1}.{0}] was vetoed. It was to be fired by trigger [{4}.{3}] at: {2, date, dd-MM-yyyy HH:mm:ss.SSS}
 Now our datasource snippet within standalone.xml:             <datasource jta="false" jndi-name="java:jboss/datasources/PostgreSQLDSNoJTA" pool-name="PostgreSQLDSNoJTA" enabled="true" use-java-context="true" use-ccm="true">
 You fill in the rest of this datasource element per your requirements. The @DisallowConcurrentExecution is a good idea to prevent multiple jobs on a single node form executing a particular method but it is the quartz clustering that prevents the same job running on multiple VM's; not this annotation.

 

 

 

 

 

 

 

异常中心网是一家专门收集整理程序员编程过程中遇到的常见异常(exception)以及各种异常问答中心的网站。异常中心网旨在,减少程序员在编码遇到异常,处理各种异常时间和痛苦,让程序员能更愉快的、快速的定位异常并查找对应的异常解决方案。异常中心网诚心打造最完美的编程社区为程序员用户服务,努力成为最好的程序员乐园程序员社区程序异常中心程序bug中心异常问答中心

 

喜欢0 评分0
游客

返回顶部