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 7bf874237266 CAMEL-27171: camel-quartz - the Quartz poll scheduler
uses a trigger that is already in the scheduler with the same cron (#30354)
7bf874237266 is described below
commit 7bf8742372662803dfb5c787f9b1cf83a4595872
Author: allthingssecurity <[email protected]>
AuthorDate: Thu Nov 19 21:02:58 2026 +0530
CAMEL-27171: camel-quartz - the Quartz poll scheduler uses a trigger that
is already in the scheduler with the same cron (#30354)
A polling consumer with scheduler=quartz and a triggerId failed to start
with an
ObjectAlreadyExistsException when its trigger was already in a non-clustered
scheduler with the same cron: after a stop with deleteJob=false, or with a
persistent job store after the application did not stop gracefully. The
stored trigger is now used. The check that the trigger is not used by
another
route reads the route id from the job, where it is stored.
A start that fails on that check now reports it: startScheduler (which the
file
consumer calls also after a failed start) does nothing without a trigger,
unscheduleTask no longer fails without one, and doStart clears the trigger
of an
earlier start, so the stop after a failed start does not unschedule the
trigger
of the other route.
When the existing trigger was removed between the start and startScheduler
and
its durable job is still in the store, only the trigger is scheduled for the
stored job.
Upgrade guide 4.24: the start no longer fails, and a trigger key used by
another
route fails the start with an IllegalArgumentException naming that route,
also
with a clustered job store.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../QuartzScheduledPollConsumerScheduler.java | 50 +++++-
...ConsumerQuartzSchedulerExistingTriggerTest.java | 173 +++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 11 ++
3 files changed, 229 insertions(+), 5 deletions(-)
diff --git
a/components/camel-quartz/src/main/java/org/apache/camel/pollconsumer/quartz/QuartzScheduledPollConsumerScheduler.java
b/components/camel-quartz/src/main/java/org/apache/camel/pollconsumer/quartz/QuartzScheduledPollConsumerScheduler.java
index e3c87949fbfe..b4a7064f0cab 100644
---
a/components/camel-quartz/src/main/java/org/apache/camel/pollconsumer/quartz/QuartzScheduledPollConsumerScheduler.java
+++
b/components/camel-quartz/src/main/java/org/apache/camel/pollconsumer/quartz/QuartzScheduledPollConsumerScheduler.java
@@ -73,6 +73,9 @@ public class QuartzScheduledPollConsumerScheduler extends
ServiceSupport
private volatile JobDetail job;
private volatile boolean scheduled;
private volatile TriggerKey rescheduleKey;
+ // the trigger was already in the scheduler when this scheduler started
(kept by deleteJob=false, or left in a
+ // persistent job store), so it is not scheduled again
+ private volatile boolean triggerExists;
@Override
public void onInit(Consumer consumer) {
@@ -92,7 +95,10 @@ public class QuartzScheduledPollConsumerScheduler extends
ServiceSupport
@Override
public void unscheduleTask() {
- LOG.debug("Unscheduling trigger: {}", trigger.getKey());
+ if (trigger != null) {
+ // no trigger when the start failed before it was set up (a
trigger key used by another route)
+ LOG.debug("Unscheduling trigger: {}", trigger.getKey());
+ }
try {
unscheduleJob();
} catch (SchedulerException e) {
@@ -102,11 +108,36 @@ public class QuartzScheduledPollConsumerScheduler extends
ServiceSupport
@Override
public void startScheduler() {
+ if (trigger == null) {
+ // the start of this scheduler failed before it set up the trigger
(a trigger key used by another route);
+ // the file consumer starts the scheduler also when its start
failed, which must not hide that failure
+ LOG.debug("Not scheduling as there is no trigger");
+ return;
+ }
if (!scheduled) {
try {
if (rescheduleKey != null) {
LOG.debug("Re-scheduling job: {} with trigger: {}", job,
trigger.getKey());
quartzScheduler.rescheduleJob(rescheduleKey, trigger);
+ } else if (triggerExists) {
+ // the trigger (and its job) are already in the scheduler
with the same cron: scheduling them
+ // again would fail with ObjectAlreadyExistsException, so
use the stored trigger
+ TriggerKey triggerKey = trigger.getKey();
+ CronTrigger existing = (CronTrigger)
quartzScheduler.getTrigger(triggerKey);
+ if (existing == null) {
+ // removed from the scheduler since this scheduler
started. Quartz deletes the job together
+ // with its last trigger only when the job is not
durable: a durable job is still there, and
+ // scheduling it again with the trigger would fail
with ObjectAlreadyExistsException
+ LOG.debug("Scheduling job: {} with trigger: {}", job,
triggerKey);
+ if (quartzScheduler.checkExists(job.getKey())) {
+ quartzScheduler.scheduleJob(trigger);
+ } else {
+ quartzScheduler.scheduleJob(job, trigger);
+ }
+ } else {
+ LOG.debug("Job: {} with trigger: {} is already
scheduled", job, triggerKey);
+ trigger = existing;
+ }
} else {
LOG.debug("Scheduling job: {} with trigger: {}", job,
trigger.getKey());
try {
@@ -232,6 +263,11 @@ public class QuartzScheduledPollConsumerScheduler extends
ServiceSupport
id = "trigger-" +
getCamelContext().getUuidGenerator().generateUuid();
}
+ triggerExists = false;
+ // the trigger of an earlier start: if this start fails (a trigger key
used by another route), the stop that
+ // follows must not unschedule the trigger with that key, which is now
the other route's, and startScheduler
+ // must not schedule it again
+ trigger = null;
CronTrigger existingTrigger = null;
TriggerKey triggerKey = null;
if (triggerId != null && triggerGroup != null) {
@@ -274,10 +310,10 @@ public class QuartzScheduledPollConsumerScheduler extends
ServiceSupport
PropertyBindingSupport.bindProperties(camelContext, trigger,
copy);
}
} else {
- checkTriggerIsNonConflicting(existingTrigger);
+ job = quartzScheduler.getJobDetail(existingTrigger.getJobKey());
+ checkTriggerIsNonConflicting(existingTrigger, job);
LOG.debug("Trigger with key {} is already present in scheduler.
Only updating it.", triggerKey);
- job = quartzScheduler.getJobDetail(existingTrigger.getJobKey());
JobDataMap jobData = job.getJobDataMap();
jobData.put(QuartzConstants.QUARTZ_TRIGGER_CRON_EXPRESSION,
getCron());
jobData.put(QuartzConstants.QUARTZ_TRIGGER_CRON_TIMEZONE,
getTimeZone().getID());
@@ -292,6 +328,8 @@ public class QuartzScheduledPollConsumerScheduler extends
ServiceSupport
if (hasTriggerChanged(existingTrigger, trigger)) {
rescheduleKey = triggerKey;
+ } else {
+ triggerExists = true;
}
}
}
@@ -315,10 +353,12 @@ public class QuartzScheduledPollConsumerScheduler extends
ServiceSupport
}
scheduled = false;
rescheduleKey = null;
+ triggerExists = false;
}
- private void checkTriggerIsNonConflicting(Trigger trigger) {
- JobDataMap jobDataMap = trigger.getJobDataMap();
+ private void checkTriggerIsNonConflicting(Trigger trigger, JobDetail
jobDetail) {
+ // the route id is stored on the job (see doStart), not on the trigger
+ JobDataMap jobDataMap = jobDetail != null ? jobDetail.getJobDataMap()
: trigger.getJobDataMap();
String routeIdFromTrigger = jobDataMap.getString("routeId");
if (routeIdFromTrigger != null && !routeIdFromTrigger.equals(routeId))
{
throw new IllegalArgumentException(
diff --git
a/components/camel-quartz/src/test/java/org/apache/camel/pollconsumer/quartz/FileConsumerQuartzSchedulerExistingTriggerTest.java
b/components/camel-quartz/src/test/java/org/apache/camel/pollconsumer/quartz/FileConsumerQuartzSchedulerExistingTriggerTest.java
new file mode 100644
index 000000000000..163d5d49dc3d
--- /dev/null
+++
b/components/camel-quartz/src/test/java/org/apache/camel/pollconsumer/quartz/FileConsumerQuartzSchedulerExistingTriggerTest.java
@@ -0,0 +1,173 @@
+/*
+ * 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.pollconsumer.quartz;
+
+import java.nio.file.Path;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.file.FileConsumer;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.quartz.QuartzComponent;
+import org.apache.camel.component.quartz.QuartzHelper;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.apache.camel.test.junit6.TestSupport;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.quartz.CronScheduleBuilder;
+import org.quartz.JobBuilder;
+import org.quartz.JobDetail;
+import org.quartz.Scheduler;
+import org.quartz.Trigger;
+import org.quartz.TriggerBuilder;
+import org.quartz.TriggerKey;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+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;
+
+/**
+ * A route whose trigger is already in the Quartz scheduler when it starts,
with the same cron, starts and polls with
+ * that trigger: the trigger kept by deleteJob=false when the route stopped,
or a trigger left in a persistent job store
+ * by an application that did not stop gracefully.
+ */
+class FileConsumerQuartzSchedulerExistingTriggerTest extends CamelTestSupport {
+
+ private static final String CRON = "0/1 * * * * ?";
+ private static final TriggerKey TRIGGER_KEY = new TriggerKey("myId",
"myGroup");
+
+ @TempDir
+ Path testDirectory;
+
+ @Test
+ void testRestartWithDeleteJobFalse() throws Exception {
+ getMockEndpoint("mock:result").expectedMessageCount(1);
+ template.sendBodyAndHeader(TestSupport.fileUri(testDirectory), "Hello
World", Exchange.FILE_NAME, "hello.txt");
+ context.getRouteController().startRoute("foo");
+ MockEndpoint.assertIsSatisfied(context);
+
+ context.getRouteController().stopRoute("foo");
+ // the trigger is kept as deleteJob=false
+ assertNotNull(scheduler().getTrigger(TRIGGER_KEY));
+ MockEndpoint.resetMocks(context);
+
+ getMockEndpoint("mock:result").expectedMessageCount(1);
+ template.sendBodyAndHeader(TestSupport.fileUri(testDirectory), "Bye
World", Exchange.FILE_NAME, "bye.txt");
+ context.getRouteController().startRoute("foo");
+ MockEndpoint.assertIsSatisfied(context);
+ assertEquals("Started",
context.getRouteController().getRouteStatus("foo").toString());
+ }
+
+ @Test
+ void testStartWithTriggerLeftInScheduler() throws Exception {
+ // the trigger and job of route foo, as left in a persistent job store
by an earlier run of the application
+ JobDetail job = JobBuilder.newJob(QuartzScheduledPollConsumerJob.class)
+ .usingJobData("routeId", "foo").build();
+ QuartzHelper.updateJobDataMap(context, job, null);
+ Trigger trigger = TriggerBuilder.newTrigger().withIdentity("myId",
"myGroup")
+ .withSchedule(CronScheduleBuilder.cronSchedule(CRON)).build();
+ scheduler().scheduleJob(job, trigger);
+
+ getMockEndpoint("mock:result").expectedMessageCount(1);
+ template.sendBodyAndHeader(TestSupport.fileUri(testDirectory), "Hello
World", Exchange.FILE_NAME, "hello.txt");
+ context.getRouteController().startRoute("foo");
+ MockEndpoint.assertIsSatisfied(context);
+ assertEquals("Started",
context.getRouteController().getRouteStatus("foo").toString());
+ }
+
+ @Test
+ void testTriggerOfAnotherRoute() throws Exception {
+ // route bar (deleteJob=true) used the trigger key first, and deleted
its trigger when it stopped
+ context.getRouteController().startRoute("bar");
+ context.getRouteController().stopRoute("bar");
+ assertNull(scheduler().getTrigger(TRIGGER_KEY));
+ // route foo then used the same key, and kept its trigger when it
stopped (deleteJob=false)
+ context.getRouteController().startRoute("foo");
+ context.getRouteController().stopRoute("foo");
+
+ // route bar uses the trigger id and group that route foo kept
+ Exception e = assertThrows(Exception.class, () ->
context.getRouteController().startRoute("bar"));
+ Throwable cause = e;
+ while (cause.getCause() != null && !(cause instanceof
IllegalArgumentException)) {
+ cause = cause.getCause();
+ }
+ assertInstanceOf(IllegalArgumentException.class, cause, "Unexpected
exception: " + e);
+ assertTrue(cause.getMessage().contains("is already used by route:
foo"), cause.getMessage());
+ // the failed start of route bar did not unschedule the trigger of
route foo
+ assertNotNull(scheduler().getTrigger(TRIGGER_KEY));
+ }
+
+ @Test
+ void testTriggerRemovedBeforeSchedulerStartsWithDurableJob() throws
Exception {
+ // the trigger and durable job of route baz, as left in a persistent
job store by an earlier run
+ TriggerKey triggerKey = new TriggerKey("durableId", "myGroup");
+ JobDetail job = JobBuilder.newJob(QuartzScheduledPollConsumerJob.class)
+ .usingJobData("routeId", "baz").storeDurably().build();
+ QuartzHelper.updateJobDataMap(context, job, null);
+ Trigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey)
+ .withSchedule(CronScheduleBuilder.cronSchedule(CRON)).build();
+ scheduler().scheduleJob(job, trigger);
+
+ // route baz finds the trigger when it starts, and starts its
scheduler later (startScheduler=false)
+ context.getRouteController().startRoute("baz");
+ // the trigger is removed in between: Quartz keeps a durable job that
has no trigger left
+ scheduler().unscheduleJob(triggerKey);
+ assertNull(scheduler().getTrigger(triggerKey));
+ assertTrue(scheduler().checkExists(job.getKey()));
+
+ getMockEndpoint("mock:baz").expectedMessageCount(1);
+ template.sendBodyAndHeader(TestSupport.fileUri(testDirectory, "baz"),
"Hello World", Exchange.FILE_NAME,
+ "hello.txt");
+ FileConsumer consumer = (FileConsumer)
context.getRoute("baz").getConsumer();
+ assertFalse(consumer.isSchedulerStarted());
+ consumer.startScheduler();
+ MockEndpoint.assertIsSatisfied(context);
+ assertTrue(consumer.isSchedulerStarted());
+ assertEquals(job.getKey(),
scheduler().getTrigger(triggerKey).getJobKey());
+ }
+
+ private Scheduler scheduler() {
+ return context.getComponent("quartz",
QuartzComponent.class).getScheduler();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ String options = "?scheduler=quartz&scheduler.cron=" +
CRON.replace(' ', '+')
+ +
"&scheduler.triggerGroup=myGroup&scheduler.triggerId=myId";
+ from(TestSupport.fileUri(testDirectory, options +
"&scheduler.deleteJob=false"))
+ .routeId("foo").autoStartup(false)
+ .to("mock:result");
+ from(TestSupport.fileUri(testDirectory, "other" + options +
"&scheduler.deleteJob=true"))
+ .routeId("bar").autoStartup(false)
+ .to("mock:other");
+ from(TestSupport.fileUri(testDirectory,
+ "baz?scheduler=quartz&scheduler.cron=" +
CRON.replace(' ', '+')
+ +
"&scheduler.triggerGroup=myGroup&scheduler.triggerId=durableId"
+ +
"&scheduler.deleteJob=false&startScheduler=false"))
+ .routeId("baz").autoStartup(false)
+ .to("mock:baz");
+ }
+ };
+ }
+}
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 bb4dfcfb3986..fc6935615147 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
@@ -9266,6 +9266,17 @@ starts, as a trigger that fires before its route has
started is still lost (with
`spring.quartz.auto-startup=false` to let Camel start it), and when the
options `properties`, `propertiesRef` or
`propertiesFile` are set, as they are not used with such a scheduler.
+=== camel-quartz - the Quartz poll scheduler uses a trigger that is already in
the scheduler
+
+A polling consumer with `scheduler=quartz` and a `scheduler.triggerId` whose
trigger is already in the Quartz scheduler
+when it starts, with the same cron, now uses that trigger. Before, the start
failed with an
+`ObjectAlreadyExistsException` (unless the job store is clustered): the
trigger is kept when the route stops with
+`scheduler.deleteJob=false`, and a persistent job store keeps it when the
application did not stop gracefully.
+
+A trigger that is in the scheduler for the job of another route (the same
`scheduler.triggerId` and
+`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-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