This is an automated email from the ASF dual-hosted git repository.

Croway pushed a commit to branch camel-4.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.22.x by this push:
     new adda9def142a CAMEL-27077: camel-quartz - a scheduler from the registry 
gets the CamelContext when the component starts (#30236)
adda9def142a is described below

commit adda9def142a3de5e4f6ddbfe6febbdd2dd29199
Author: Federico Mariani <[email protected]>
AuthorDate: Tue Nov 17 13:21:25 2026 +0100

    CAMEL-27077: camel-quartz - a scheduler from the registry gets the 
CamelContext when the component starts (#30236)
    
    * CAMEL-27077: camel-quartz - a scheduler from the registry gets the 
CamelContext when the component starts
    
    With a Quartz Scheduler set on the quartz component or autowired from the 
registry, such as the scheduler of
    spring-boot-starter-quartz, the component stored the CamelContext in the 
scheduler context only once the CamelContext
    was fully started, while the endpoints add their jobs when they start. 
Spring starts that scheduler before Camel, so a
    trigger that fired in between failed with "No CamelContext could be found" 
and the fire was lost.
    
    The component now stores the CamelContext in such a scheduler when it 
starts. It logs a WARN when the scheduler is
    already running at that point, as a trigger that fires before its route has 
started is still lost
    (spring.quartz.auto-startup=false lets Camel start it once the routes have 
started), and when the options properties,
    propertiesRef or propertiesFile are set, as they are not used with such a 
scheduler.
    
    * CAMEL-27077: camel-quartz - QuartzInjectedSchedulerTest counts only the 
WARNs about its own scheduler
    
    The test classes of camel-quartz run at the same time, and 
QuartzComponentCamelContextSharedSchedulerTest logs the
    WARN of a running scheduler too, so the counts of 
QuartzInjectedSchedulerTest could include it. They now count only the
    WARNs that name the scheduler of the test.
    
    The docs of an injected scheduler now also name the options 
schedulerFactory, enableJmx, prefixInstanceName and
    interruptJobsOnShutdown, which are not used with such a scheduler either.
    
    (cherry picked from commit 2c2d09516feb0f838e586d290fa039bc031fd428)
---
 .../camel/catalog/docs/quartz-component.adoc       |  21 ++
 .../src/main/docs/quartz-component.adoc            |  21 ++
 .../camel/component/quartz/QuartzComponent.java    |  48 ++++
 .../quartz/QuartzInjectedSchedulerTest.java        | 250 +++++++++++++++++++++
 4 files changed, 340 insertions(+)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/quartz-component.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/quartz-component.adoc
index d81134749eb2..b992ec28d195 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/quartz-component.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/quartz-component.adoc
@@ -82,6 +82,27 @@ To do this, you can configure this in Spring XML as follows
 </bean>
 ----
 
+=== Using your own scheduler (spring-boot-starter-quartz)
+
+The component uses the Quartz `Scheduler` set with its `scheduler` option, or 
else the single `Scheduler` of the
+registry, such as the scheduler of `spring-boot-starter-quartz`. It does not 
create a scheduler of its own then,
+so the options `properties`, `propertiesRef` and `propertiesFile` are not 
used, and it logs a WARN when one of them is set.
+The options `schedulerFactory`, `enableJmx`, `prefixInstanceName` and 
`interruptJobsOnShutdown` are not used either.
+Configure that scheduler instead, for example with the 
`spring.quartz.properties.*` properties of Spring Boot.
+
+The component starts this scheduler once the routes have started, unless 
`autoStartScheduler` is `false`
+or the scheduler is already running. The routes add their jobs when they 
start, so a scheduler that is already running
+can fire a trigger before its route has started, and this fire is lost. The 
component logs a WARN when the scheduler is
+already running when it starts. Spring Boot starts the scheduler of 
`spring-boot-starter-quartz` before Camel,
+so let Camel start it:
+
+[source,properties]
+----
+spring.quartz.auto-startup=false
+----
+
+The Quartz jobs of the application that are not Camel routes then also start 
when Camel starts the scheduler.
+
 === Enabling Quartz scheduler in JMX
 
 You need to configure the quartz scheduler properties to enable JMX.
diff --git a/components/camel-quartz/src/main/docs/quartz-component.adoc 
b/components/camel-quartz/src/main/docs/quartz-component.adoc
index d81134749eb2..b992ec28d195 100644
--- a/components/camel-quartz/src/main/docs/quartz-component.adoc
+++ b/components/camel-quartz/src/main/docs/quartz-component.adoc
@@ -82,6 +82,27 @@ To do this, you can configure this in Spring XML as follows
 </bean>
 ----
 
+=== Using your own scheduler (spring-boot-starter-quartz)
+
+The component uses the Quartz `Scheduler` set with its `scheduler` option, or 
else the single `Scheduler` of the
+registry, such as the scheduler of `spring-boot-starter-quartz`. It does not 
create a scheduler of its own then,
+so the options `properties`, `propertiesRef` and `propertiesFile` are not 
used, and it logs a WARN when one of them is set.
+The options `schedulerFactory`, `enableJmx`, `prefixInstanceName` and 
`interruptJobsOnShutdown` are not used either.
+Configure that scheduler instead, for example with the 
`spring.quartz.properties.*` properties of Spring Boot.
+
+The component starts this scheduler once the routes have started, unless 
`autoStartScheduler` is `false`
+or the scheduler is already running. The routes add their jobs when they 
start, so a scheduler that is already running
+can fire a trigger before its route has started, and this fire is lost. The 
component logs a WARN when the scheduler is
+already running when it starts. Spring Boot starts the scheduler of 
`spring-boot-starter-quartz` before Camel,
+so let Camel start it:
+
+[source,properties]
+----
+spring.quartz.auto-startup=false
+----
+
+The Quartz jobs of the application that are not Camel routes then also start 
when Camel starts the scheduler.
+
 === Enabling Quartz scheduler in JMX
 
 You need to configure the quartz scheduler properties to enable JMX.
diff --git 
a/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java
 
b/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java
index 3c3a9126422f..e20f6c0758cf 100644
--- 
a/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java
+++ 
b/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java
@@ -60,6 +60,7 @@ public class QuartzComponent extends DefaultComponent 
implements ExtendedStartup
     private final List<SchedulerInitTask> schedulerInitTasks = new 
ArrayList<>();
     private volatile boolean schedulerInitTasksDone;
     private volatile boolean shouldStopScheduler;
+    private volatile boolean schedulerStartedByComponent;
 
     @Metadata(label = "advanced", autowired = true)
     private Scheduler scheduler;
@@ -460,6 +461,52 @@ public class QuartzComponent extends DefaultComponent 
implements ExtendedStartup
 
         if (scheduler == null) {
             createAndInitScheduler();
+        } else if (!shouldStopScheduler) {
+            // a scheduler set on this component, or autowired from the 
registry, can already run (Spring starts the
+            // scheduler of spring-boot-starter-quartz before Camel), and the 
endpoints add their jobs when they start:
+            // store the CamelContext now, so their jobs find it before 
onCamelContextFullyStarted
+            storeCamelContextInQuartzContext();
+            warnIfSchedulerOptionsAreIgnored();
+            warnIfSchedulerIsRunning();
+        }
+    }
+
+    /**
+     * Called when the scheduler was set on this component, or autowired from 
the registry: the options that configure
+     * the scheduler this component creates are not used.
+     */
+    private void warnIfSchedulerOptionsAreIgnored() throws SchedulerException {
+        List<String> ignored = new ArrayList<>(3);
+        if (properties != null) {
+            ignored.add("properties");
+        }
+        if (propertiesRef != null) {
+            ignored.add("propertiesRef");
+        }
+        if (propertiesFile != null) {
+            ignored.add("propertiesFile");
+        }
+        if (!ignored.isEmpty()) {
+            LOG.warn("The options {} of the quartz component are ignored, as 
the Quartz scheduler {} is set on the component"
+                     + " or autowired from the registry. Configure that 
scheduler instead.",
+                    String.join(", ", ignored), scheduler.getSchedulerName());
+        }
+    }
+
+    /**
+     * Called when the scheduler was set on this component, or autowired from 
the registry. When it already runs, a job
+     * that fires before the consumer of its route has started is lost (the 
route is not started yet, or this component
+     * is not started yet for a trigger in a persistent job store). No WARN 
for a scheduler that this component started,
+     * before a restart of the CamelContext.
+     */
+    private void warnIfSchedulerIsRunning() throws SchedulerException {
+        if (!schedulerStartedByComponent && scheduler.isStarted() && 
!scheduler.isInStandbyMode()) {
+            LOG.warn("The Quartz scheduler {} set on the quartz component or 
autowired from the registry is already running,"
+                     + " so a trigger that fires before its route has started 
is lost."
+                     + " Let the quartz component start the scheduler once the 
routes have started"
+                     + " (autoStartScheduler=true, the default): with 
spring-boot-starter-quartz,"
+                     + " set spring.quartz.auto-startup=false.",
+                    scheduler.getSchedulerName());
         }
     }
 
@@ -552,6 +599,7 @@ public class QuartzComponent extends DefaultComponent 
implements ExtendedStartup
             } else {
                 LOG.info("Starting Quartz scheduler: {}", scheduler);
                 scheduler.start();
+                schedulerStartedByComponent = true;
             }
         }
     }
diff --git 
a/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzInjectedSchedulerTest.java
 
b/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzInjectedSchedulerTest.java
new file mode 100644
index 000000000000..7252babe6b4d
--- /dev/null
+++ 
b/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzInjectedSchedulerTest.java
@@ -0,0 +1,250 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.quartz;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.UUID;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.CamelEvent;
+import org.apache.camel.support.EventNotifierSupport;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.apache.camel.test.junit6.TestExecutionConfiguration;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.Logger;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.config.Property;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobExecutionException;
+import org.quartz.Scheduler;
+import org.quartz.SchedulerException;
+import org.quartz.impl.StdSchedulerFactory;
+import org.quartz.listeners.JobListenerSupport;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A Quartz scheduler from the registry (such as the one of 
spring-boot-starter-quartz) is autowired into the quartz
+ * component, and is often started before the CamelContext.
+ */
+class QuartzInjectedSchedulerTest extends CamelTestSupport {
+
+    // a part of the WARN for a scheduler that is running when the component 
starts
+    private static final String RUNNING = "spring.quartz.auto-startup=false";
+    // a part of the WARN for options that are not used with such a scheduler
+    private static final String IGNORED = "are ignored";
+
+    private final List<JobExecutionException> jobFailures = new 
CopyOnWriteArrayList<>();
+    private final List<String> jobsExecuted = new CopyOnWriteArrayList<>();
+    private final List<String> warnings = new CopyOnWriteArrayList<>();
+    private final AbstractAppender appender
+            = new AbstractAppender("QuartzInjectedScheduler-" + 
UUID.randomUUID(), null, null, true, Property.EMPTY_ARRAY) {
+                @Override
+                public void append(LogEvent event) {
+                    if (event.getLevel() == Level.WARN) {
+                        warnings.add(event.getMessage().getFormattedMessage());
+                    }
+                }
+            };
+
+    private Scheduler injected;
+    private volatile boolean camelStartedEventReached;
+
+    @Override
+    public void configureTest(TestExecutionConfiguration 
testExecutionConfiguration) {
+        // each test configures the registry and the routes before it starts 
the context
+        testExecutionConfiguration.withUseRouteBuilder(false);
+    }
+
+    @BeforeEach
+    void captureWarnings() {
+        appender.start();
+        componentLogger().addAppender(appender);
+        // addAppender creates a non-additive logger config, which would hide 
this logger from the root appenders
+        componentLogger().setAdditive(true);
+    }
+
+    @AfterEach
+    void stopCapture() throws SchedulerException {
+        componentLogger().removeAppender(appender);
+        appender.stop();
+        if (injected != null) {
+            // Camel does not shut down a scheduler it did not create: stop 
Camel first, as an application does
+            context.stop();
+            injected.shutdown();
+        }
+    }
+
+    @Test
+    void 
runningInjectedSchedulerRunsJobThatFiresBeforeCamelContextIsFullyStarted() 
throws Exception {
+        injected = createScheduler(2);
+        // as the SchedulerFactoryBean of Spring does before Camel starts
+        injected.start();
+        context.getRegistry().bind("myScheduler", injected);
+        // holds the start of Camel at the CamelContextStartedEvent, after the 
routes have started and before the
+        // CamelContext is fully started, until the trigger has fired (the job 
waits for this point, see
+        // createScheduler)
+        context.getManagementStrategy().addEventNotifier(new 
EventNotifierSupport() {
+            @Override
+            public void notify(CamelEvent event) {
+                camelStartedEventReached = true;
+                await().atMost(10, TimeUnit.SECONDS).until(() -> 
!jobsExecuted.isEmpty());
+            }
+
+            @Override
+            public boolean isEnabled(CamelEvent event) {
+                return event instanceof CamelEvent.CamelContextStartedEvent;
+            }
+        });
+        MockEndpoint mock = addRoute();
+
+        context.start();
+
+        assertSame(injected, context.getComponent("quartz", 
QuartzComponent.class).getScheduler());
+        assertEquals(List.of(), jobFailures);
+        mock.assertIsSatisfied();
+        assertEquals(1, warningsContaining(injected, RUNNING), 
warnings::toString);
+    }
+
+    @Test
+    void injectedSchedulerThatIsNotStartedIsStartedByCamel() throws Exception {
+        injected = createScheduler(2);
+        context.getRegistry().bind("myScheduler", injected);
+        camelStartedEventReached = true;
+        MockEndpoint mock = addRoute();
+
+        context.start();
+
+        mock.assertIsSatisfied();
+        assertSame(injected, context.getComponent("quartz", 
QuartzComponent.class).getScheduler());
+        assertTrue(injected.isStarted());
+        assertEquals(List.of(), jobFailures);
+        assertEquals(0, warningsContaining(injected, RUNNING), 
warnings::toString);
+    }
+
+    @Test
+    void schedulerOfComponentRunsJob() throws Exception {
+        MockEndpoint mock = addRoute();
+
+        context.start();
+
+        mock.assertIsSatisfied();
+        Scheduler scheduler = context.getComponent("quartz", 
QuartzComponent.class).getScheduler();
+        assertEquals(0, warningsContaining(scheduler, RUNNING), 
warnings::toString);
+    }
+
+    @Test
+    void warnsWhenPropertiesAreSetWithInjectedScheduler() throws Exception {
+        injected = createScheduler(2);
+        context.getRegistry().bind("myScheduler", injected);
+        QuartzComponent quartz = context.getComponent("quartz", 
QuartzComponent.class);
+        quartz.setProperties(Map.of("org.quartz.threadPool.threadCount", "3"));
+        quartz.setPropertiesFile("classpath:org/quartz/quartz.properties");
+
+        context.start();
+
+        assertSame(injected, quartz.getScheduler());
+        assertEquals(2, 
quartz.getScheduler().getMetaData().getThreadPoolSize());
+        assertEquals(1, warningsContaining(injected, IGNORED), 
warnings::toString);
+        assertEquals(1, warningsContaining(injected, "properties, 
propertiesFile"), warnings::toString);
+    }
+
+    @Test
+    void usesPropertiesWithoutInjectedScheduler() throws Exception {
+        QuartzComponent quartz = context.getComponent("quartz", 
QuartzComponent.class);
+        quartz.setProperties(Map.of(
+                "org.quartz.scheduler.instanceName", "MyScheduler",
+                "org.quartz.threadPool.class", 
"org.quartz.simpl.SimpleThreadPool",
+                "org.quartz.threadPool.threadCount", "3"));
+
+        context.start();
+
+        assertEquals(3, 
quartz.getScheduler().getMetaData().getThreadPoolSize());
+        assertEquals(0, warningsContaining(quartz.getScheduler(), IGNORED), 
warnings::toString);
+    }
+
+    private Scheduler createScheduler(int threadCount) throws 
SchedulerException {
+        Properties props = new Properties();
+        props.setProperty("org.quartz.scheduler.instanceName", 
"MyInjectedScheduler-" + UUID.randomUUID());
+        props.setProperty("org.quartz.scheduler.skipUpdateCheck", "true");
+        props.setProperty("org.quartz.threadPool.class", 
"org.quartz.simpl.SimpleThreadPool");
+        props.setProperty("org.quartz.threadPool.threadCount", 
String.valueOf(threadCount));
+        props.setProperty("org.quartz.jobStore.class", 
"org.quartz.simpl.RAMJobStore");
+        Scheduler scheduler = new StdSchedulerFactory(props).getScheduler();
+        scheduler.getListenerManager().addJobListener(new JobListenerSupport() 
{
+            @Override
+            public String getName() {
+                return "jobsOfTest";
+            }
+
+            @Override
+            public void jobToBeExecuted(JobExecutionContext ctx) {
+                // the job runs once the routes have started, while Camel 
waits at the CamelContextStartedEvent
+                await().atMost(10, TimeUnit.SECONDS).until(() -> 
camelStartedEventReached);
+            }
+
+            @Override
+            public void jobWasExecuted(JobExecutionContext ctx, 
JobExecutionException jobException) {
+                if (jobException != null) {
+                    jobFailures.add(jobException);
+                }
+                jobsExecuted.add(ctx.getJobDetail().getKey().toString());
+            }
+        });
+        return scheduler;
+    }
+
+    private MockEndpoint addRoute() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() {
+                // fires once, at once
+                
from("quartz://myGroup/myTimer?trigger.repeatCount=0&triggerStartDelay=-1000").routeId("myRoute")
+                        .to("mock:result");
+            }
+        });
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        return mock;
+    }
+
+    /**
+     * Counts the WARNs about this scheduler only: the appender also gets the 
WARNs of the other test classes, which run
+     * at the same time (QuartzComponentCamelContextSharedSchedulerTest logs 
the WARN of a running scheduler).
+     */
+    private long warningsContaining(Scheduler scheduler, String text) throws 
SchedulerException {
+        String name = " " + scheduler.getSchedulerName() + " ";
+        return warnings.stream().filter(message -> message.contains(name) && 
message.contains(text)).count();
+    }
+
+    private static Logger componentLogger() {
+        return (Logger) LogManager.getLogger(QuartzComponent.class);
+    }
+}

Reply via email to