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

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 23e8d0ac014d CAMEL-27287: camel-quartz - a durable job left without 
its trigger is scheduled again (#30534)
23e8d0ac014d is described below

commit 23e8d0ac014d86ac00e01a635ed347ca1b811bf7
Author: allthingssecurity <[email protected]>
AuthorDate: Sat Nov 21 14:37:50 2026 +0530

    CAMEL-27287: camel-quartz - a durable job left without its trigger is 
scheduled again (#30534)
    
    With durableJob=true and the default deleteJob=true, the endpoint
    unschedules its trigger when it stops, and Quartz keeps the durable job.
    The next endpoint with that trigger key found no trigger and called
    scheduleJob(job, trigger), which failed with ObjectAlreadyExistsException
    because the job was still stored: a route removed and added again (such
    as by a reload) could not be added, and with a persistent job store the
    application failed to start after it had stopped once.
    
    When the trigger is missing and only the job is in the scheduler, the
    job of the endpoint now replaces the stored one (its data names this
    endpoint and CamelContext) and is scheduled with the trigger.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../camel/component/quartz/QuartzEndpoint.java     |  28 ++++-
 .../quartz/QuartzDurableJobScheduleAgainTest.java  | 125 +++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc    |  16 +++
 3 files changed, 166 insertions(+), 3 deletions(-)

diff --git 
a/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzEndpoint.java
 
b/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzEndpoint.java
index 82124b0920da..9350315fb9fa 100644
--- 
a/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzEndpoint.java
+++ 
b/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzEndpoint.java
@@ -20,6 +20,7 @@ import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Set;
 import java.util.TimeZone;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -46,6 +47,7 @@ import org.quartz.Job;
 import org.quartz.JobBuilder;
 import org.quartz.JobDataMap;
 import org.quartz.JobDetail;
+import org.quartz.JobPersistenceException;
 import org.quartz.ObjectAlreadyExistsException;
 import org.quartz.Scheduler;
 import org.quartz.SchedulerException;
@@ -427,11 +429,31 @@ public class QuartzEndpoint extends DefaultEndpoint {
                     scheduler.scheduleJob(jobDetail, trigger);
                 }
             } catch (ObjectAlreadyExistsException ex) {
-                // some other VM might may have stored the job & trigger in DB 
in clustered mode, in the mean time
-                if (!(getComponent().isClustered())) {
+                Trigger storedTrigger = scheduler.getTrigger(triggerKey);
+                JobDetail storedJob = null;
+                if (storedTrigger == null) {
+                    try {
+                        storedJob = scheduler.getJobDetail(jobDetail.getKey());
+                    } catch (JobPersistenceException e) {
+                        // a persistent job store cannot read the stored job 
(its class or data cannot be loaded, or a
+                        // database error): it is not taken as a Camel job
+                        ex.addSuppressed(e);
+                    }
+                }
+                if (storedJob != null && 
CamelJob.class.isAssignableFrom(storedJob.getJobClass())) {
+                    // only the job is in the scheduler: Quartz keeps a 
durable job (durableJob=true) when its trigger
+                    // is removed, as the endpoint does when it stops with 
deleteJob=true (the route is removed, such as
+                    // by a reload, or CamelContext stops with a persistent 
job store). Store the job of this endpoint in
+                    // its place (its data names this endpoint and 
CamelContext), with the trigger. Only a Camel job is
+                    // replaced: a job of another class with the same name and 
group (such as one of the application,
+                    // in a shared scheduler) is kept, and the start fails as 
before
+                    LOG.debug("Job {} is in the scheduler without its trigger. 
Scheduling it again.", jobDetail.getKey());
+                    scheduler.scheduleJob(jobDetail, Set.of(trigger), true);
+                } else if (!(getComponent().isClustered())) {
                     throw ex;
                 } else {
-                    trigger = scheduler.getTrigger(triggerKey);
+                    // some other VM may have stored the job & trigger in DB 
in clustered mode, in the mean time
+                    trigger = storedTrigger;
                     if (trigger == null) {
                         throw new SchedulerException("Trigger could not be 
found in quartz scheduler.");
                     }
diff --git 
a/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzDurableJobScheduleAgainTest.java
 
b/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzDurableJobScheduleAgainTest.java
new file mode 100644
index 000000000000..67725f2fabf5
--- /dev/null
+++ 
b/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzDurableJobScheduleAgainTest.java
@@ -0,0 +1,125 @@
+/*
+ * 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 org.apache.camel.FailedToCreateRouteException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.util.ObjectHelper;
+import org.junit.jupiter.api.Test;
+import org.quartz.Job;
+import org.quartz.JobBuilder;
+import org.quartz.JobDetail;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobKey;
+import org.quartz.ObjectAlreadyExistsException;
+import org.quartz.Scheduler;
+import org.quartz.TriggerKey;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Quartz keeps a durable job (durableJob=true) when its trigger is removed, 
as the endpoint does when it stops
+ * (deleteJob=true). The endpoint must schedule the job again when it starts, 
instead of failing with
+ * ObjectAlreadyExistsException. A job of another class with the same name and 
group is not replaced.
+ */
+class QuartzDurableJobScheduleAgainTest extends BaseQuartzTest {
+
+    private static final TriggerKey TRIGGER_KEY = 
TriggerKey.triggerKey("myTimer", "durable");
+    private static final JobKey JOB_KEY = JobKey.jobKey("myTimer", "durable");
+
+    @Test
+    void durableJobScheduledAgainWhenTheRouteIsAddedAgain() throws Exception {
+        context.addRoutes(route());
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMinimumMessageCount(1);
+        mock.assertIsSatisfied();
+
+        // a reload: the endpoint of the removed route unschedules its 
trigger, and Quartz keeps the durable job
+        context.getRouteController().stopRoute("durable");
+        context.removeRoute("durable");
+        Scheduler scheduler = context.getComponent("quartz", 
QuartzComponent.class).getScheduler();
+        assertNull(scheduler.getTrigger(TRIGGER_KEY));
+        assertTrue(scheduler.checkExists(JOB_KEY));
+
+        context.addRoutes(route());
+
+        // removing the route removed its mock endpoint too, the route added 
again has a new one
+        MockEndpoint again = getMockEndpoint("mock:result");
+        again.expectedMinimumMessageCount(1);
+        again.assertIsSatisfied();
+        assertNotNull(scheduler.getTrigger(TRIGGER_KEY));
+    }
+
+    @Test
+    void durableJobLeftWithoutTriggerScheduledAgain() throws Exception {
+        // the durable job without its trigger, as a persistent job store 
keeps it after the application stopped (here
+        // of a stateful endpoint, a subclass of CamelJob)
+        Scheduler scheduler = context.getComponent("quartz", 
QuartzComponent.class).getScheduler();
+        
scheduler.addJob(JobBuilder.newJob(StatefulCamelJob.class).withIdentity(JOB_KEY).storeDurably()
+                .usingJobData(QuartzConstants.QUARTZ_ENDPOINT_URI, 
"quartz://durable/myTimer?durableJob=true")
+                .build(), false);
+
+        context.addRoutes(route());
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMinimumMessageCount(1);
+        mock.assertIsSatisfied();
+
+        // the job of the endpoint replaced the stored one
+        assertEquals(CamelJob.class, 
scheduler.getJobDetail(JOB_KEY).getJobClass());
+        
assertEquals(context.getRoute("durable").getEndpoint().getEndpointUri(),
+                
scheduler.getJobDetail(JOB_KEY).getJobDataMap().getString(QuartzConstants.QUARTZ_ENDPOINT_URI));
+    }
+
+    @Test
+    void durableJobOfAnotherClassIsNotReplaced() throws Exception {
+        // a durable job of the application with the same name and group, in a 
shared scheduler: it must not be
+        // replaced by the job of the endpoint, and the start fails as before
+        Scheduler scheduler = context.getComponent("quartz", 
QuartzComponent.class).getScheduler();
+        
scheduler.addJob(JobBuilder.newJob(ApplicationJob.class).withIdentity(JOB_KEY).storeDurably()
+                .usingJobData("owner", "application").build(), false);
+
+        Exception ex = assertThrows(FailedToCreateRouteException.class, () -> 
context.addRoutes(route()));
+        
assertNotNull(ObjectHelper.getException(ObjectAlreadyExistsException.class, 
ex));
+
+        JobDetail stored = scheduler.getJobDetail(JOB_KEY);
+        assertEquals(ApplicationJob.class, stored.getJobClass());
+        assertEquals("application", stored.getJobDataMap().getString("owner"));
+        assertNull(scheduler.getTrigger(TRIGGER_KEY));
+    }
+
+    static class ApplicationJob implements Job {
+        @Override
+        public void execute(JobExecutionContext context) {
+            // never fires, it has no trigger
+        }
+    }
+
+    private static RouteBuilder route() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                
from("quartz://durable/myTimer?durableJob=true&trigger.repeatInterval=100").routeId("durable")
+                        .to("mock:result");
+            }
+        };
+    }
+}
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index aefced92c578..cfe0169fd322 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -9560,6 +9560,22 @@ A trigger that is in the scheduler for the job of 
another route (the same `sched
 `scheduler.triggerGroup` in two routes) now fails the start with an 
`IllegalArgumentException` that names that route,
 also with a clustered job store, where the second route used to start and 
never poll.
 
+=== camel-quartz - a durable job left without its trigger is scheduled again
+
+With `durableJob=true` and the default `deleteJob=true`, a route removes its 
trigger when it stops (the route is
+removed, or the CamelContext stops), and Quartz keeps the durable job. A route 
with the same job then failed to start
+with an `ObjectAlreadyExistsException`: a route that was removed and added 
again (a reload), and, with a persistent job
+store, every start after a graceful stop. Now, when the trigger is not in the 
scheduler and the stored job is a Camel job
+(its class is `CamelJob` or a subclass), the job of the endpoint replaces the 
stored job when the route starts, and is
+scheduled with the trigger. The stored job therefore takes the job data, the 
class and the `jobParameters` of the
+endpoint that starts. A job that is not a Camel job with the same name and 
group, such as a job of the application in a
+shared scheduler, is not replaced, and the start fails as before.
+
+This also applies to a durable job whose trigger Quartz removed because it 
will not fire again, such as a simple trigger
+that fired its last repeat (`trigger.repeatCount`): the job is now scheduled 
again when the route starts, and the
+trigger fires again from the start. With a clustered job store, where a route 
does not remove its trigger when it stops,
+this is the case that changes: the start used to fail with `Trigger could not 
be found in quartz scheduler`.
+
 === camel-langchain4j-ingest - a media document whose type cannot be told is 
filtered
 
 With `modality=media` and no `contentType` set, a document whose media type 
cannot be told from the extension of its

Reply via email to