为什么Quartz找不到CamelContext?

gblwokeq  于 2023-10-18  发布在  Apache
关注(0)|答案(1)|浏览(126)

我有一个使用CamelQuartz的简单Springboot应用程序:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.14</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-quartz</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-spring-boot-starter</artifactId>
        <version>3.20.6</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-quartz-starter</artifactId>
        <version>3.20.6</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

我建立了与DB的连接:

spring.datasource.url=jdbc:postgresql://localhost:5432/schedule_module
spring.datasource.username=postgres
spring.datasource.password=1111
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update

spring.quartz.job-store-type=jdbc
spring.quartz.properties.org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
spring.quartz.properties.org.quartz.jobStore.tablePrefix=qrtz_
spring.quartz.properties.org.quartz.jobStore.isClustered=true

并写了最简单的路线:

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class SimpleRouter extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("quartz://simpleRouterScheduler?trigger.repeatInterval=5000")
                .routeId("SimpleRouter")
                .log("SimpleRouter Hello");
    }
}

当应用程序第一次启动时,将启动路线并将其计划存储在数据库中:

2023-07-31 10:49:08.940  INFO 4744 --- [           main] r.s.n.p.s.ScheduleApp03Application       : Starting ScheduleApp03Application using Java 17.0.6 on DESKTOP-6SHGJTA with PID 4744 (C:\work\dev\testsapps\camel\schedule-app03\target\classes started by baa in C:\work\dev\testsapps\camel\schedule-app03)
2023-07-31 10:49:08.942  INFO 4744 --- [           main] r.s.n.p.s.ScheduleApp03Application       : No active profile set, falling back to 1 default profile: "default"
2023-07-31 10:49:10.309  INFO 4744 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-07-31 10:49:10.468  INFO 4744 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2023-07-31 10:49:10.498  INFO 4744 --- [           main] o.f.c.internal.license.VersionPrinter    : Flyway Community Edition 7.15.0 by Redgate
2023-07-31 10:49:10.499  INFO 4744 --- [           main] o.f.c.i.database.base.BaseDatabaseType   : Database: jdbc:postgresql://localhost:5432/schedule_module01 (PostgreSQL 9.6)
2023-07-31 10:49:10.536  INFO 4744 --- [           main] o.f.core.internal.command.DbValidate     : Successfully validated 2 migrations (execution time 00:00.015s)
2023-07-31 10:49:10.549  INFO 4744 --- [           main] o.f.c.i.s.JdbcTableSchemaHistory         : Creating Schema History table "public"."flyway_schema_history" ...
2023-07-31 10:49:10.584  INFO 4744 --- [           main] o.f.core.internal.command.DbMigrate      : Current version of schema "public": << Empty Schema >>
2023-07-31 10:49:10.603  INFO 4744 --- [           main] o.f.core.internal.command.DbMigrate      : Migrating schema "public" to version "1 - Init DB"
2023-07-31 10:49:10.768  INFO 4744 --- [           main] o.f.core.internal.command.DbMigrate      : Migrating schema "public" to version "2 - Insert DB"
2023-07-31 10:49:10.774  INFO 4744 --- [           main] o.f.core.internal.command.DbMigrate      : Successfully applied 2 migrations to schema "public", now at version v2 (execution time 00:00.194s)
2023-07-31 10:49:10.889  INFO 4744 --- [           main] org.quartz.impl.StdSchedulerFactory      : Using default implementation for ThreadExecutor
2023-07-31 10:49:10.905  INFO 4744 --- [           main] org.quartz.core.SchedulerSignalerImpl    : Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
2023-07-31 10:49:10.905  INFO 4744 --- [           main] org.quartz.core.QuartzScheduler          : Quartz Scheduler v.2.3.2 created.
2023-07-31 10:49:10.908  INFO 4744 --- [           main] o.s.s.quartz.LocalDataSourceJobStore     : Using db table-based data access locking (synchronization).
2023-07-31 10:49:10.910  INFO 4744 --- [           main] o.s.s.quartz.LocalDataSourceJobStore     : JobStoreCMT initialized.
2023-07-31 10:49:10.911  INFO 4744 --- [           main] org.quartz.core.QuartzScheduler          : Scheduler meta-data: Quartz Scheduler (v2.3.2) 'quartzScheduler' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.springframework.scheduling.quartz.LocalDataSourceJobStore' - which supports persistence. and is clustered.

2023-07-31 10:49:10.911  INFO 4744 --- [           main] org.quartz.impl.StdSchedulerFactory      : Quartz scheduler 'quartzScheduler' initialized from an externally provided properties instance.
2023-07-31 10:49:10.911  INFO 4744 --- [           main] org.quartz.impl.StdSchedulerFactory      : Quartz scheduler version: 2.3.2
2023-07-31 10:49:10.911  INFO 4744 --- [           main] org.quartz.core.QuartzScheduler          : JobFactory set to: org.springframework.scheduling.quartz.SpringBeanJobFactory@55f4887d
2023-07-31 10:49:10.961  INFO 4744 --- [           main] o.s.s.quartz.SchedulerFactoryBean        : Starting Quartz Scheduler now
2023-07-31 10:49:10.982  INFO 4744 --- [           main] org.quartz.core.QuartzScheduler          : Scheduler quartzScheduler_$_NON_CLUSTERED started.
2023-07-31 10:49:11.136  INFO 4744 --- [           main] .c.i.e.DefaultAutowiredLifecycleStrategy : Autowired property: scheduler on component: quartz as exactly one instance of type: org.quartz.Scheduler (org.quartz.impl.StdScheduler) found in the registry
2023-07-31 10:49:11.186  INFO 4744 --- [           main] o.a.c.impl.engine.AbstractCamelContext   : Apache Camel 3.20.6 (camel-1) is starting
2023-07-31 10:49:11.225  INFO 4744 --- [           main] o.a.c.component.quartz.QuartzEndpoint    : Job Camel_camel-1.simpleRouterScheduler (cron=null, triggerType=SimpleTriggerImpl, jobClass=CamelJob) is scheduled. Next fire date is 2023-07-31T10:49:11.693+0300
2023-07-31 10:49:11.237  INFO 4744 --- [           main] o.a.c.impl.engine.AbstractCamelContext   : Routes startup (started:1)
2023-07-31 10:49:11.237  INFO 4744 --- [           main] o.a.c.impl.engine.AbstractCamelContext   :     Started SimpleRouter (quartz://simpleRouterScheduler)
2023-07-31 10:49:11.237  INFO 4744 --- [           main] o.a.c.impl.engine.AbstractCamelContext   : Apache Camel 3.20.6 (camel-1) started in 242ms (build:51ms init:141ms start:50ms)
2023-07-31 10:49:11.240  INFO 4744 --- [           main] o.a.c.component.quartz.QuartzComponent   : The Quartz scheduler: org.quartz.impl.StdScheduler@315c081 has already been started
2023-07-31 10:49:11.244  INFO 4744 --- [           main] r.s.n.p.s.ScheduleApp03Application       : Started ScheduleApp03Application in 2.641 seconds (JVM running for 2.994)
2023-07-31 10:49:11.709  INFO 4744 --- [eduler_Worker-1] SimpleRouter                             : SimpleRouter Hello
2023-07-31 10:49:16.698  INFO 4744 --- [eduler_Worker-2] SimpleRouter                             : SimpleRouter Hello
2023-07-31 10:49:21.700  INFO 4744 --- [eduler_Worker-3] SimpleRouter                             : SimpleRouter Hello

但是当我停止应用程序并重新启动它时,我得到以下消息:

2023-07-31 10:51:04.883  INFO 21732 --- [           main] r.s.n.p.s.ScheduleApp03Application       : Starting ScheduleApp03Application using Java 17.0.6 on DESKTOP-6SHGJTA with PID 21732 (C:\work\dev\testsapps\camel\schedule-app03\target\classes started by baa in C:\work\dev\testsapps\camel\schedule-app03)
2023-07-31 10:51:04.885  INFO 21732 --- [           main] r.s.n.p.s.ScheduleApp03Application       : No active profile set, falling back to 1 default profile: "default"
2023-07-31 10:51:06.040  INFO 21732 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-07-31 10:51:06.168  INFO 21732 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2023-07-31 10:51:06.209  INFO 21732 --- [           main] o.f.c.internal.license.VersionPrinter    : Flyway Community Edition 7.15.0 by Redgate
2023-07-31 10:51:06.210  INFO 21732 --- [           main] o.f.c.i.database.base.BaseDatabaseType   : Database: jdbc:postgresql://localhost:5432/schedule_module01 (PostgreSQL 9.6)
2023-07-31 10:51:06.251  INFO 21732 --- [           main] o.f.core.internal.command.DbValidate     : Successfully validated 2 migrations (execution time 00:00.022s)
2023-07-31 10:51:06.260  INFO 21732 --- [           main] o.f.core.internal.command.DbMigrate      : Current version of schema "public": 2
2023-07-31 10:51:06.261  INFO 21732 --- [           main] o.f.core.internal.command.DbMigrate      : Schema "public" is up to date. No migration necessary.
2023-07-31 10:51:06.375  INFO 21732 --- [           main] org.quartz.impl.StdSchedulerFactory      : Using default implementation for ThreadExecutor
2023-07-31 10:51:06.385  INFO 21732 --- [           main] org.quartz.core.SchedulerSignalerImpl    : Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
2023-07-31 10:51:06.385  INFO 21732 --- [           main] org.quartz.core.QuartzScheduler          : Quartz Scheduler v.2.3.2 created.
2023-07-31 10:51:06.388  INFO 21732 --- [           main] o.s.s.quartz.LocalDataSourceJobStore     : Using db table-based data access locking (synchronization).
2023-07-31 10:51:06.391  INFO 21732 --- [           main] o.s.s.quartz.LocalDataSourceJobStore     : JobStoreCMT initialized.
2023-07-31 10:51:06.392  INFO 21732 --- [           main] org.quartz.core.QuartzScheduler          : Scheduler meta-data: Quartz Scheduler (v2.3.2) 'quartzScheduler' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.springframework.scheduling.quartz.LocalDataSourceJobStore' - which supports persistence. and is clustered.

2023-07-31 10:51:06.392  INFO 21732 --- [           main] org.quartz.impl.StdSchedulerFactory      : Quartz scheduler 'quartzScheduler' initialized from an externally provided properties instance.
2023-07-31 10:51:06.392  INFO 21732 --- [           main] org.quartz.impl.StdSchedulerFactory      : Quartz scheduler version: 2.3.2
2023-07-31 10:51:06.393  INFO 21732 --- [           main] org.quartz.core.QuartzScheduler          : JobFactory set to: org.springframework.scheduling.quartz.SpringBeanJobFactory@93824eb
2023-07-31 10:51:06.433  INFO 21732 --- [           main] o.s.s.quartz.SchedulerFactoryBean        : Starting Quartz Scheduler now
2023-07-31 10:51:06.450  INFO 21732 --- [           main] o.s.s.quartz.LocalDataSourceJobStore     : ClusterManager: detected 1 failed or restarted instances.
2023-07-31 10:51:06.451  INFO 21732 --- [           main] o.s.s.quartz.LocalDataSourceJobStore     : ClusterManager: Scanning for instance "NON_CLUSTERED"'s failed in-progress jobs.
2023-07-31 10:51:06.458  INFO 21732 --- [           main] o.s.s.quartz.LocalDataSourceJobStore     : ClusterManager: ......Freed 1 acquired trigger(s).
2023-07-31 10:51:06.459  INFO 21732 --- [           main] org.quartz.core.QuartzScheduler          : Scheduler quartzScheduler_$_NON_CLUSTERED started.
2023-07-31 10:51:06.530 ERROR 21732 --- [eduler_Worker-1] o.a.camel.component.quartz.CamelJob      : Failed to execute CamelJob.

org.quartz.JobExecutionException: No CamelContext could be found with name: camel-1
    at org.apache.camel.component.quartz.CamelJob.getCamelContext(CamelJob.java:103) ~[camel-quartz-3.20.6.jar:3.20.6]
    at org.apache.camel.component.quartz.CamelJob.execute(CamelJob.java:60) ~[camel-quartz-3.20.6.jar:3.20.6]
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[quartz-2.3.2.jar:na]
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) ~[quartz-2.3.2.jar:na]

2023-07-31 10:51:06.531  INFO 21732 --- [eduler_Worker-1] org.quartz.core.JobRunShell              : Job Camel_camel-1.simpleRouterScheduler threw a JobExecutionException: 

org.quartz.JobExecutionException: No CamelContext could be found with name: camel-1
    at org.apache.camel.component.quartz.CamelJob.getCamelContext(CamelJob.java:103) ~[camel-quartz-3.20.6.jar:3.20.6]
    at org.apache.camel.component.quartz.CamelJob.execute(CamelJob.java:60) ~[camel-quartz-3.20.6.jar:3.20.6]
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[quartz-2.3.2.jar:na]
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) ~[quartz-2.3.2.jar:na]

2023-07-31 10:51:06.615  INFO 21732 --- [           main] .c.i.e.DefaultAutowiredLifecycleStrategy : Autowired property: scheduler on component: quartz as exactly one instance of type: org.quartz.Scheduler (org.quartz.impl.StdScheduler) found in the registry
2023-07-31 10:51:06.654  INFO 21732 --- [           main] o.a.c.impl.engine.AbstractCamelContext   : Apache Camel 3.20.6 (camel-1) is starting
2023-07-31 10:51:06.663  INFO 21732 --- [           main] o.a.c.component.quartz.QuartzEndpoint    : Job Camel_camel-1.simpleRouterScheduler (cron=null, triggerType=SimpleTriggerImpl, jobClass=CamelJob) is scheduled. Next fire date is null
2023-07-31 10:51:06.672  INFO 21732 --- [           main] o.a.c.impl.engine.AbstractCamelContext   : Routes startup (started:1)
2023-07-31 10:51:06.672  INFO 21732 --- [           main] o.a.c.impl.engine.AbstractCamelContext   :     Started SimpleRouter (quartz://simpleRouterScheduler)
2023-07-31 10:51:06.672  INFO 21732 --- [           main] o.a.c.impl.engine.AbstractCamelContext   : Apache Camel 3.20.6 (camel-1) started in 192ms (build:39ms init:136ms start:17ms)
2023-07-31 10:51:06.673  INFO 21732 --- [           main] o.a.c.component.quartz.QuartzComponent   : The Quartz scheduler: org.quartz.impl.StdScheduler@7e8279e5 has already been started
2023-07-31 10:51:06.677  INFO 21732 --- [           main] r.s.n.p.s.ScheduleApp03Application       : Started ScheduleApp03Application in 2.087 seconds (JVM running for 2.392)
2023-07-31 10:51:06.705  INFO 21732 --- [eduler_Worker-4] SimpleRouter                             : SimpleRouter Hello
2023-07-31 10:51:11.699  INFO 21732 --- [eduler_Worker-5] SimpleRouter                             : SimpleRouter Hello
2023-07-31 10:51:16.704  INFO 21732 --- [eduler_Worker-6] SimpleRouter                             : SimpleRouter Hello

我是否正确地理解了Quartz无法找到CamelContext,它第一次从CamelContext接收有关路线和时间表的数据?
如何设置Camel,使CamelQuartz每次启动时都能协商相同的上下文?

gdrx4gfi

gdrx4gfi1#

原因是spring-boot-starter-quartz默认启动调度程序。它在配置Camel的Quartz组件并将CamelContext传递到调度程序示例上下文之前执行此操作。
我没有发现在文档中明确提到,但事情是,你应该让 Camel 控制调度器的生命周期。为了实现这一点,请通过设置以下属性来禁用调度程序的自动启动

spring:
  quartz:
    auto-startup: false

相关问题