通过集群环境中的Camel Quartz实现单个消费者

ncecgwcz  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(126)

我希望在集群中有一个单一的主消费者。我已经在我的应用程序中使用了camel-quartz,以便以集群的方式运行cron作业。
我的用例是,当我的 Boot 应用程序在集群环境中启动时,我需要运行一次单个消费者,
我正在使用简单触发器repeatCount = 0。但群集无法正常工作,因为我在群集中的两个节点上都获取日志。

路线

@Component
class QuartzRoutes(
    private val schedulerFactoryBean: SchedulerFactoryBean,
    private val camelContext: CamelContext
) : EndpointRouteBuilder() {

    private val logger = KotlinLogging.logger { }

    fun getCamelQuartzEndpoint(groupName: String, triggerName: String, camelContext: CamelContext): Endpoint {
        val quartzComponent = QuartzComponent(camelContext)
        quartzComponent.scheduler = schedulerFactoryBean.scheduler
        quartzComponent.isInterruptJobsOnShutdown = true
        quartzComponent.isAutoStartScheduler = false

        val endpoint =
            quartzComponent.createEndpoint("quartz://$QUARTZ_GROUP_NAME$triggerName?stateful=true&durableJob=true&trigger.repeatInterval=3000&trigger.repeatCount=0&trigger.misfireInstruction=4")

        if (!camelContext.componentNames.contains("quartz")) {
            camelContext.addComponent("quartz", quartzComponent)
        }
        return endpoint
    }

    override fun configure() {

        val triggerName = "myTimerTrigger"
        val routeId = "${triggerName}_route"

        val endpoint = getCamelQuartzEndpoint(QUARTZ_GROUP_NAME, triggerName, camelContext)

        from(endpoint)
            .routeId(routeId)
            .process { exchange ->
                logger.info { "Simple trigger fired Connecting to Endpoint 1: $exchange" }
                Thread.sleep(240000) // mimic task
            }
            .end()

    }
}

Spring Boot 石英配置

spring:
  lifecycle:
    timeout-per-shutdown-phase: 45s
  datasource:
    url: jdbc:postgresql://localhost/quartztest?stringtype=unspecified&reWriteBatchedInserts=true
  quartz:
    startup-delay: 5s
    wait-for-jobs-to-complete-on-shutdown: true
    overwrite-existing-jobs: true
    job-store-type: JDBC
    jdbc:
      initialize-schema: ALWAYS
    properties:
      org.quartz.scheduler.instanceId: AUTO
      org.quartz.scheduler.skipUpdateCheck: true
      org.quartz.jobStore.isClustered: true
      org.quartz.jobStore.useProperties: false
      org.quartz.threadPool.threadCount: 5
      org.quartz.jobStore.driverDelegateClass: 'org.quartz.impl.jdbcjobstore.PostgreSQLDelegate'

我已经在GitHub Link上为这个问题创建了一个示例应用程序

kpbwa7wx

kpbwa7wx1#

Camel具有一些内置的集群功能-请参见here
在您的特定情况下,您可以建立一个路由模型,该路由在启动Quartz消费者时占据主导地位,从而防止其他节点在相同的预定时间启动cron作业。

相关问题