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 3d3b5185f1b8 CAMEL-27083: camel-scheduler - a consumer that is stopped
and started again polls again, and releases only the pool reference it took
(#30308)
3d3b5185f1b8 is described below
commit 3d3b5185f1b8ef92d152a6099cbc0bf3da639321
Author: allthingssecurity <[email protected]>
AuthorDate: Wed Nov 18 12:35:01 2026 +0530
CAMEL-27083: camel-scheduler - a consumer that is stopped and started again
polls again, and releases only the pool reference it took (#30308)
The consumers of a scheduler endpoint share a thread pool that the component
keeps per scheduler name with a reference count. SchedulerEndpoint took a
reference only when the consumer had no scheduler and no thread pool yet,
and
released one on every stop.
A consumer that was stopped and started again in place (JMX, or a route
policy
that stops and starts the consumer, such as the leader election policies)
kept
the pool and the scheduler of its first start: its stop had released the
last
reference and the component had shut the pool down, so the second start took
no reference and scheduled the poll on the shut down pool, where the default
CallerRuns policy drops it. The route was Started and never polled again. A
further stop released a reference it did not hold, which could shut down the
pool of another route of the same scheduler name, and so did the stop of a
consumer that has its own thread pool or scheduler.
The consumer now records that it took a reference to the pool of the
component. Only then its stop releases one, and its next start takes the
pool
again and gives it to the scheduler it created at its first start.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../component/scheduler/SchedulerConsumer.java | 23 ++++
.../component/scheduler/SchedulerEndpoint.java | 21 +++-
.../scheduler/SchedulerConsumerRestartTest.java | 131 +++++++++++++++++++++
3 files changed, 172 insertions(+), 3 deletions(-)
diff --git
a/components/camel-scheduler/src/main/java/org/apache/camel/component/scheduler/SchedulerConsumer.java
b/components/camel-scheduler/src/main/java/org/apache/camel/component/scheduler/SchedulerConsumer.java
index e88ac7915676..e71e3b5e3876 100644
---
a/components/camel-scheduler/src/main/java/org/apache/camel/component/scheduler/SchedulerConsumer.java
+++
b/components/camel-scheduler/src/main/java/org/apache/camel/component/scheduler/SchedulerConsumer.java
@@ -17,6 +17,7 @@
package org.apache.camel.component.scheduler;
import java.util.Date;
+import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.camel.Exchange;
@@ -30,6 +31,12 @@ public class SchedulerConsumer extends ScheduledPollConsumer
{
private static final Logger LOG =
LoggerFactory.getLogger(SchedulerConsumer.class);
+ // the thread pool of the component this consumer runs on (null when it
has its own scheduler or thread pool),
+ // and whether it holds a reference to it: the pool is released when the
consumer stops, and taken again when it
+ // starts again
+ private ScheduledExecutorService componentPool;
+ private boolean componentPoolTaken;
+
public SchedulerConsumer(SchedulerEndpoint endpoint, Processor processor) {
super(endpoint, processor);
}
@@ -106,6 +113,22 @@ public class SchedulerConsumer extends
ScheduledPollConsumer {
return polled ? 1 : 0;
}
+ ScheduledExecutorService getComponentPool() {
+ return componentPool;
+ }
+
+ void setComponentPool(ScheduledExecutorService componentPool) {
+ this.componentPool = componentPool;
+ }
+
+ boolean isComponentPoolTaken() {
+ return componentPoolTaken;
+ }
+
+ void setComponentPoolTaken(boolean componentPoolTaken) {
+ this.componentPoolTaken = componentPoolTaken;
+ }
+
@Override
protected void doStart() throws Exception {
getEndpoint().onConsumerStart(this);
diff --git
a/components/camel-scheduler/src/main/java/org/apache/camel/component/scheduler/SchedulerEndpoint.java
b/components/camel-scheduler/src/main/java/org/apache/camel/component/scheduler/SchedulerEndpoint.java
index ac6aa6a500c9..7d826a711131 100644
---
a/components/camel-scheduler/src/main/java/org/apache/camel/component/scheduler/SchedulerEndpoint.java
+++
b/components/camel-scheduler/src/main/java/org/apache/camel/component/scheduler/SchedulerEndpoint.java
@@ -29,6 +29,7 @@ import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.UriEndpoint;
import org.apache.camel.spi.UriParam;
import org.apache.camel.spi.UriPath;
+import org.apache.camel.support.DefaultScheduledPollConsumerScheduler;
import org.apache.camel.support.ScheduledPollEndpoint;
/**
@@ -125,14 +126,28 @@ public class SchedulerEndpoint extends
ScheduledPollEndpoint {
}
public void onConsumerStart(SchedulerConsumer consumer) {
- // if using default scheduler then obtain thread pool from component
which manages their lifecycle
- if (consumer.getScheduler() == null &&
consumer.getScheduledExecutorService() == null) {
+ // if using default scheduler then obtain thread pool from component
which manages their lifecycle. A consumer
+ // that is started again (stopped and started via JMX or a route
policy) takes the pool again, as its stop
+ // released it, and the component shut it down if it was the last user
+ boolean usesComponentPool = consumer.getComponentPool() != null;
+ if (usesComponentPool || consumer.getScheduler() == null &&
consumer.getScheduledExecutorService() == null) {
ScheduledExecutorService scheduler =
getComponent().addConsumer(consumer);
+ consumer.setComponentPool(scheduler);
+ consumer.setComponentPoolTaken(true);
consumer.setScheduledExecutorService(scheduler);
+ if (consumer.getScheduler() instanceof
DefaultScheduledPollConsumerScheduler defaultScheduler) {
+ // the scheduler the consumer created at its first start still
has the pool of that start
+ defaultScheduler.setScheduledExecutorService(scheduler);
+ }
}
}
public void onConsumerStop(SchedulerConsumer consumer) {
- getComponent().removeConsumer(consumer);
+ // only a consumer that took a reference to the pool of the component
releases one: otherwise the pool could
+ // be shut down while other consumers still use it
+ if (consumer.isComponentPoolTaken()) {
+ consumer.setComponentPoolTaken(false);
+ getComponent().removeConsumer(consumer);
+ }
}
}
diff --git
a/core/camel-core/src/test/java/org/apache/camel/component/scheduler/SchedulerConsumerRestartTest.java
b/core/camel-core/src/test/java/org/apache/camel/component/scheduler/SchedulerConsumerRestartTest.java
new file mode 100644
index 000000000000..550f6f9d7541
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/component/scheduler/SchedulerConsumerRestartTest.java
@@ -0,0 +1,131 @@
+/*
+ * 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.scheduler;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.Registry;
+import org.apache.camel.support.ScheduledPollConsumer;
+import org.apache.camel.support.service.ServiceHelper;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A scheduler consumer that is stopped and started again (as JMX or a route
policy does) must take the thread pool of
+ * the component again, and a consumer must only release the pool it took.
+ */
+public class SchedulerConsumerRestartTest extends ContextTestSupport {
+
+ private final Map<String, CountDownLatch> polls = new
ConcurrentHashMap<>();
+ private final ScheduledExecutorService ownPool =
Executors.newSingleThreadScheduledExecutor();
+
+ @Override
+ protected Registry createCamelRegistry() throws Exception {
+ Registry registry = super.createCamelRegistry();
+ registry.bind("ownPool", ownPool);
+ return registry;
+ }
+
+ @Override
+ @AfterEach
+ public void tearDown() throws Exception {
+ super.tearDown();
+ ownPool.shutdownNow();
+ }
+
+ @Test
+ public void testStoppedAndStartedConsumerPollsAgain() throws Exception {
+ awaitPoll("solo");
+
+ Consumer consumer = context.getRoute("solo").getConsumer();
+ ServiceHelper.stopService(consumer);
+ ServiceHelper.startService(consumer);
+
+ assertFalse(executorOf("solo").isShutdown(),
+ "the restarted consumer must not use the thread pool that was
shut down when it stopped");
+ awaitPoll("solo");
+ }
+
+ @Test
+ public void testRestartedConsumerDoesNotShutDownThePoolOfAnotherRoute()
throws Exception {
+ awaitPoll("a");
+ awaitPoll("b");
+
+ Consumer consumer = context.getRoute("a").getConsumer();
+ ServiceHelper.stopService(consumer);
+ ServiceHelper.startService(consumer);
+ ServiceHelper.stopService(consumer);
+
+ assertFalse(executorOf("b").isShutdown(), "route b still uses the
thread pool of scheduler foo");
+ awaitPoll("b");
+ }
+
+ @Test
+ public void
testConsumerWithItsOwnPoolDoesNotReleaseThePoolOfTheComponent() throws
Exception {
+ awaitPoll("d");
+
+ // route c runs on its own thread pool, so it never took a reference
to the pool of scheduler bar
+ context.getRouteController().stopRoute("c");
+
+ assertFalse(executorOf("d").isShutdown(), "route d still uses the
thread pool of scheduler bar");
+ awaitPoll("d");
+ }
+
+ private void awaitPoll(String routeId) throws InterruptedException {
+ CountDownLatch latch = new CountDownLatch(1);
+ polls.put(routeId, latch);
+ assertTrue(latch.await(10, TimeUnit.SECONDS), "route " + routeId + "
must poll");
+ }
+
+ private ScheduledExecutorService executorOf(String routeId) {
+ return ((ScheduledPollConsumer)
context.getRoute(routeId).getConsumer()).getScheduledExecutorService();
+ }
+
+ private void poll(String routeId) {
+ CountDownLatch latch = polls.get(routeId);
+ if (latch != null) {
+ latch.countDown();
+ }
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ public void configure() {
+
from("scheduler://solo?initialDelay=0&delay=10").routeId("solo").process(e ->
poll("solo"));
+
+
from("scheduler://foo?initialDelay=0&delay=10").routeId("a").process(e ->
poll("a"));
+
from("scheduler://foo?initialDelay=0&delay=11").routeId("b").process(e ->
poll("b"));
+
+
from("scheduler://bar?initialDelay=0&delay=10").routeId("d").process(e ->
poll("d"));
+
from("scheduler://bar?initialDelay=0&delay=12&scheduledExecutorService=#ownPool").routeId("c")
+ .process(e -> poll("c"));
+ }
+ };
+ }
+}