allthingssecurity commented on code in PR #30354:
URL: https://github.com/apache/camel/pull/30354#discussion_r4629668156
##########
components/camel-quartz/src/main/java/org/apache/camel/pollconsumer/quartz/QuartzScheduledPollConsumerScheduler.java:
##########
@@ -102,11 +108,30 @@ public void unscheduleTask() {
@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
+ LOG.debug("Scheduling job: {} with trigger: {}", job,
triggerKey);
+ quartzScheduler.scheduleJob(job, trigger);
Review Comment:
Good catch, this one is real. I checked it in Quartz 2.5.2:
`RAMJobStore.removeTrigger` and `JobStoreSupport.removeTrigger` delete the job
with its last trigger only when the job is not durable, and
`storeJobAndTrigger` then throws `ObjectAlreadyExistsException` for a durable
job that is still there. Fixed in a930e25ef4c3: if the job still exists, only
the trigger is scheduled for it (`scheduleJob(trigger)`), otherwise the job and
trigger as before. I did not use `addJob(job, true)`, since Quartz refuses a
non-durable job there. The new test
`testTriggerRemovedBeforeSchedulerStartsWithDurableJob` removes the trigger of
a durable job between the route start and `startScheduler`; it fails with
`ObjectAlreadyExistsException` without the change and passes with it.
_Claude Code on behalf of allthingssecurity_
##########
components/camel-quartz/src/main/java/org/apache/camel/pollconsumer/quartz/QuartzScheduledPollConsumerScheduler.java:
##########
@@ -102,11 +108,30 @@ public void unscheduleTask() {
@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
+ LOG.debug("Scheduling job: {} with trigger: {}", job,
triggerKey);
+ quartzScheduler.scheduleJob(job, trigger);
Review Comment:
Same as above, a930e25ef4c3.
_Claude Code on behalf of allthingssecurity_
##########
components/camel-quartz/src/main/java/org/apache/camel/pollconsumer/quartz/QuartzScheduledPollConsumerScheduler.java:
##########
@@ -317,8 +349,9 @@ private void unscheduleJob() throws SchedulerException {
rescheduleKey = null;
}
Review Comment:
Done in a930e25ef4c3, `triggerExists` is now cleared in `unscheduleJob` next
to `rescheduleKey`.
_Claude Code on behalf of allthingssecurity_
##########
components/camel-quartz/src/test/java/org/apache/camel/pollconsumer/quartz/FileConsumerQuartzSchedulerExistingTriggerTest.java:
##########
@@ -0,0 +1,135 @@
+/*
+ * 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.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.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();
+ }
+ assertTrue(cause instanceof IllegalArgumentException, "Unexpected
exception: " + e);
+ assertTrue(cause.getMessage().contains("is already used by route:
foo"), cause.getMessage());
Review Comment:
Done in a930e25ef4c3.
_Claude Code on behalf of allthingssecurity_
##########
components/camel-quartz/src/test/java/org/apache/camel/pollconsumer/quartz/FileConsumerQuartzSchedulerExistingTriggerTest.java:
##########
@@ -0,0 +1,135 @@
+/*
+ * 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.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.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();
+ }
+ assertTrue(cause instanceof IllegalArgumentException, "Unexpected
exception: " + e);
+ assertTrue(cause.getMessage().contains("is already used by route:
foo"), cause.getMessage());
Review Comment:
Same as above, a930e25ef4c3.
_Claude Code on behalf of allthingssecurity_
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]