Java Quartz 2.2多计划程序和@DisallowConcurrentExecution 请考虑这个例子。

Java Quartz 2.2多计划程序和@DisallowConcurrentExecution 请考虑这个例子。,java,quartz-scheduler,Java,Quartz Scheduler,一个示例web应用程序在启动时调用scheduler.start()。调度程序配置为将其作业存储在数据库中 应用程序被复制到六台web服务器上 所以,如果我们启动六个web服务器,那么在一个DB上就会有六个同名的调度器。如中所述: 从不针对任何其他具有相同调度程序名称的实例(start()ed)运行的同一组数据库表启动(scheduler.start())非群集实例。您可能会遇到严重的数据损坏,并且肯定会遇到不稳定的行为 因此,这将失败 我的问题是,如果我确信我所有的工作都有@DisallowC

一个示例web应用程序在启动时调用
scheduler.start()
。调度程序配置为将其作业存储在数据库中

应用程序被复制到六台web服务器上

所以,如果我们启动六个web服务器,那么在一个DB上就会有六个同名的调度器。如中所述:

从不针对任何其他具有相同调度程序名称的实例(start()ed)运行的同一组数据库表启动(scheduler.start())非群集实例。您可能会遇到严重的数据损坏,并且肯定会遇到不稳定的行为

因此,这将失败

我的问题是,如果我确信我所有的工作都有
@DisallowConcurrentExecution
将正常工作,或者仍然会失败


如果
@DisallowConcurrentExecution
没有帮助,我应该手动将一台服务器配置为主机

public class StartUp implements ServletContextListener {

   public void contextInitialized(ServletContextEvent event) {
       if(THIS_IS_MASTER_TOMCAT){
         scheduler.start()
       }
}

还有更好的方法吗

基本上Rene M.是正确的。以下是与石英相关的文件:

下面是一些背景和我们在我公司使用的概念性示例。我们在Wildfly集群中使用quartz集群模式。也就是说,每个wildfly集群节点都运行quartz。由于quartz本身以集群模式运行,并且指向同一个数据库模式,因此我们保证每个集群运行一个作业。同样,请参阅文档。关键问题是:

  • 单个quartz群集必须针对单个quartz数据库运行
    模式。显然,必须根据创建关系数据库表 文件。没什么大不了的
  • 您必须设置quartz.property文件 中的每个节点都必须存在其副本 簇相同的quartz.property文件
  • 最后,必须使用非JTA数据源,否则quartz群集将失败。这通常意味着在荒野世界 将需要自己的 数据源
  • quartz.property示例:

        #============================================================================
    # 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}
    
    现在,standalone.xml中的数据源代码片段:

                <datasource jta="false" jndi-name="java:jboss/datasources/PostgreSQLDSNoJTA" pool-name="PostgreSQLDSNoJTA" enabled="true" use-java-context="true" use-ccm="true">
    
    
    

    您可以根据需要填写此数据源元素的其余部分。@DisallowConcurrentExecution是防止单个节点上的多个作业执行特定方法的一个好主意,但quartz群集阻止同一作业在多个VM上运行;不是此注释。

    DisSalowConcurrentExecution只会阻止类在同一节点上运行两次。当您仅在一个节点上启动调度程序时,此节点将是唯一一个调度某些内容的节点。您需要的是Quartz群集设置,其中每个计划程序都有唯一的名称。有一种方法可以说quartz在启动时生成节点名。我不能完全确定这一切,因为我上次使用quartz的项目是很久以前的事了,但它是集群的;)