java - Spring Quartz Scheduler Service 要求仅在单个节点上运行,而不是在所有节点上运行

标签 java spring quartz-scheduler

有一个 Spring Web 应用程序 - Yevi。 Yevi部署在具有6个节点的集群环境中。所有节点使用共享的 Yevi 部署包。应用程序在 applicationContext.xml 中有以下配置:

<bean id="cronYeviTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
      <property name="jobDetail" ref="YeviJob"/>
        <!-- run every morning at 6 AM  -->
        <property name="cronExpression" value="0 0 6 * * ?"/>
    </bean>

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
      <property name="triggers">
        <list>
          <ref bean="YeviTrigger"/>
        </list>
      </property>
      <property name="configLocation" value="classpath:quartz.properties"/>
    </bean>

而且,quartz.properties 现在什么也没有。上述 CRON 作业设计为每天早上 6 点运行并执行一些业务操作,并在业务操作完成后 sleep 。并且,同时再次醒来并执行相同的业务操作。

我认为该问题与集群环境有关。一旦所有节点都启动并运行。创建了 6 个 CRON 作业 - 每个节点上一个,所需的业务操作执行 6 次,而不是一天一次。

我认为,必须进行某种配置才能在单个节点上仅运行一次而不是在所有节点上运行它。我需要它在早上 6 点仅在一个节点上运行此 CRON 作业,如果该节点已完成,则在故障转移时它应该检查下一个节点并在那里运行 CRON 作业。

谢谢!

最佳答案

使用 JDBC JobStore,您可以通过一个节点实现触发器触发。

http://quartz-scheduler.org/documentation/quartz-2.2.x/configuration/ConfigJDBCJobStoreClustering

编辑

Clustering currently only works with the JDBC-Jobstore (JobStoreTX or JobStoreCMT), and essentially works by having each node of the cluster share the same database.

Load-balancing occurs automatically, with each node of the cluster firing jobs as quickly as it can. When a trigger’s firing time occurs, the first node to acquire it (by placing a lock on it) is the node that will fire it.

Only one node will fire the job for each firing. What I mean by that is, if the job has a repeating trigger that tells it to fire every 10 seconds, then at 12:00:00 exactly one node will run the job, and at 12:00:10 exactly one node will run the job, etc. It won’t necessarily be the same node each time - it will more or less be random which node runs it. The load balancing mechanism is near-random for busy schedulers (lots of triggers) but favors the same node for non-busy (e.g. few triggers) schedulers.

关于java - Spring Quartz Scheduler Service 要求仅在单个节点上运行,而不是在所有节点上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20990490/

相关文章:

java - NPE 和 IAE 是否有不同处理方式的情况?

java - Hibernate 通用 DAO、通用服务和通用 View 层?

java - Spring Cloud 配置到 java util 属性

java - 在 spring 3 quartz 调度程序中我遇到了异常

java - Tomcat 何时将 war 文件解压缩到 ROOT 文件夹?

包含内容的文件夹上的 Java renameTo

java - 当您不需要提供证书时,为 SSL 连接配置 TcpNetClientConnectionFactory 的正确方法是什么?

java - 将 quartz 数据源连接到 Spring Boot bean

java - 如何使用 .jar 文件之外的本地文件 setPage() JEditorPane?

openshift中的Java RMI端口绑定(bind)错误