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 7cb927ec8756 CAMEL-26951: camel-support - API components: a call
refused by a shut down thread pool fails the exchange instead of being dropped
(#30000)
7cb927ec8756 is described below
commit 7cb927ec87562e2e8730e503c1e250c556370ac3
Author: allthingssecurity <[email protected]>
AuthorDate: Tue Nov 17 20:38:36 2026 +0530
CAMEL-26951: camel-support - API components: a call refused by a shut down
thread pool fails the exchange instead of being dropped (#30000)
AbstractApiProducer submitted each call to the pool of its CamelContext
and only the task called the callback. When the pool was shut down, the
CallerRuns policy of the default profile discarded the task without an
exception, so the exchange never completed: a call that raced the stop
of its CamelContext, or a producer used after the stop.
Fail the call with a RejectedExecutionException when the pool is shut
down, also when it was shut down during the submit (the task and that
check claim the call once, so the callback is called exactly once), and
when the submit is rejected. Do not create a pool for a CamelContext
that is stopping or stopped.
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../AbstractApiProducerRejectedCallTest.java | 286 +++++++++++++++++++++
.../support/component/AbstractApiEndpoint.java | 49 +++-
.../support/component/AbstractApiProducer.java | 50 +++-
3 files changed, 382 insertions(+), 3 deletions(-)
diff --git
a/core/camel-core/src/test/java/org/apache/camel/support/component/AbstractApiProducerRejectedCallTest.java
b/core/camel-core/src/test/java/org/apache/camel/support/component/AbstractApiProducerRejectedCallTest.java
new file mode 100644
index 000000000000..5f9be6c0a71d
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/support/component/AbstractApiProducerRejectedCallTest.java
@@ -0,0 +1,286 @@
+/*
+ * 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.support.component;
+
+import java.util.HashMap;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.RejectedExecutionHandler;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.AsyncProducer;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.ServiceStatus;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.spi.ThreadPoolFactory;
+import org.apache.camel.spi.ThreadPoolProfile;
+import
org.apache.camel.support.component.AbstractApiProducerTest.TestApiComponent;
+import
org.apache.camel.support.component.AbstractApiProducerTest.TestApiEndpoint;
+import org.awaitility.Awaitility;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A call of an API producer that its thread pool can no longer run, because
the CamelContext of the endpoint stops or
+ * has stopped, fails the exchange instead of leaving it without completion. A
call made while the CamelContext waits
+ * for its inflight exchanges, or before it is started, gets a pool.
+ */
+class AbstractApiProducerRejectedCallTest {
+
+ private CamelContext context;
+
+ @AfterEach
+ void stopContext() {
+ if (context != null) {
+ context.stop();
+ }
+ }
+
+ @Test
+ void testCallRacingTheStopOfItsCamelContextFails() throws Exception {
+ context = new DefaultCamelContext();
+ HoldingThreadPoolFactory factory
+ = new
HoldingThreadPoolFactory(context.getExecutorServiceManager().getThreadPoolFactory());
+ context.getExecutorServiceManager().setThreadPoolFactory(factory);
+ context.start();
+
+ AsyncProducer producer = createProducer(context);
+ Exchange exchange = newExchange(producer);
+ AtomicInteger calls = new AtomicInteger();
+ CountDownLatch done = new CountDownLatch(1);
+
+ // the call gets the pool, and waits before it submits its task to it
+ Thread caller = new Thread(() -> producer.process(exchange, doneSync
-> {
+ calls.incrementAndGet();
+ done.countDown();
+ }), "api-caller");
+ caller.start();
+ try {
+ assertTrue(factory.held.await(10, TimeUnit.SECONDS), "The call
should wait before it submits");
+
+ // the CamelContext stops meanwhile, and shuts down the pool
+ context.stop();
+ } finally {
+ factory.release.countDown();
+ }
+
+ assertTrue(done.await(10, TimeUnit.SECONDS), "The call should
complete, not hang");
+ caller.join(10000);
+ assertEquals(1, calls.get(), "The callback must be called once");
+ assertInstanceOf(RejectedExecutionException.class,
exchange.getException());
+ }
+
+ @Test
+ void testCallAfterTheCamelContextStoppedFailsWithoutCreatingAPool() throws
Exception {
+ context = new DefaultCamelContext();
+ HoldingThreadPoolFactory factory
+ = new
HoldingThreadPoolFactory(context.getExecutorServiceManager().getThreadPoolFactory());
+ factory.release.countDown();
+ context.getExecutorServiceManager().setThreadPoolFactory(factory);
+ context.start();
+
+ AsyncProducer producer = createProducer(context);
+ Exchange first = newExchange(producer);
+ CountDownLatch firstDone = new CountDownLatch(1);
+ producer.process(first, doneSync -> firstDone.countDown());
+ assertTrue(firstDone.await(10, TimeUnit.SECONDS), "The call should
complete");
+ assertNull(first.getException());
+ assertEquals("Greetings World", first.getMessage().getBody());
+
+ context.stop();
+
+ Exchange exchange = newExchange(producer);
+ AtomicInteger calls = new AtomicInteger();
+ boolean sync = producer.process(exchange, doneSync ->
calls.incrementAndGet());
+
+ assertTrue(sync, "The call should fail at once");
+ assertEquals(1, calls.get(), "The callback must be called once");
+ assertInstanceOf(RejectedExecutionException.class,
exchange.getException());
+ assertEquals(1, factory.created.get(), "No pool should be created for
a stopped CamelContext");
+ }
+
+ @Test
+ void testFirstCallOfAnInflightExchangeWhileTheCamelContextStopsCompletes()
throws Exception {
+ context = new DefaultCamelContext();
+ context.getShutdownStrategy().setTimeout(20);
+ TestApiEndpoint endpoint = createEndpoint(context);
+ CountDownLatch entered = new CountDownLatch(1);
+ CountDownLatch release = new CountDownLatch(1);
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:start")
+ .process(e -> {
+ entered.countDown();
+ release.await(10, TimeUnit.SECONDS);
+ })
+ .to(endpoint);
+ }
+ });
+ context.start();
+
+ // an exchange is in flight, before its first call of the API (so the
pool of the endpoint is not there yet)
+ ProducerTemplate template = context.createProducerTemplate();
+ CompletableFuture<Exchange> reply = template.asyncSend("direct:start",
e -> e.getMessage().setBody("World"));
+ assertTrue(entered.await(10, TimeUnit.SECONDS), "The exchange should
be in flight");
+
+ // the CamelContext stops, and waits for the exchange
+ Thread stopper = new Thread(context::stop, "stopper");
+ stopper.start();
+ try {
+ Awaitility.await().atMost(10, TimeUnit.SECONDS)
+ .until(() -> context.getStatus() ==
ServiceStatus.Stopping);
+ } finally {
+ release.countDown();
+ }
+
+ // the first call of the API is made while the CamelContext stops: it
gets a pool and completes
+ Exchange out = reply.get(10, TimeUnit.SECONDS);
+ stopper.join(20000);
+ assertNull(out.getException());
+ assertEquals("Greetings World", out.getMessage().getBody());
+ assertTrue(context.isStopped());
+ }
+
+ @Test
+ void testCallBeforeTheCamelContextStartedCompletes() throws Exception {
+ context = new DefaultCamelContext();
+
+ // a producer used before its CamelContext is started
+ AsyncProducer producer = createProducer(context);
+ Exchange exchange = newExchange(producer);
+ CountDownLatch done = new CountDownLatch(1);
+ producer.process(exchange, doneSync -> done.countDown());
+
+ assertTrue(done.await(10, TimeUnit.SECONDS), "The call should
complete");
+ assertNull(exchange.getException());
+ assertEquals("Greetings World", exchange.getMessage().getBody());
+
+ // the pool is shut down with the CamelContext
+ ExecutorService pool = ((TestApiEndpoint)
producer.getEndpoint()).getExecutorService();
+ context.start();
+ context.stop();
+ assertTrue(pool.isShutdown());
+ }
+
+ @Test
+ void testCallAfterTheCamelContextStartedAgainCompletes() throws Exception {
+ context = new DefaultCamelContext();
+ context.start();
+ AsyncProducer producer = createProducer(context);
+ Exchange first = newExchange(producer);
+ CountDownLatch firstDone = new CountDownLatch(1);
+ producer.process(first, doneSync -> firstDone.countDown());
+ assertTrue(firstDone.await(10, TimeUnit.SECONDS), "The call should
complete");
+ assertNull(first.getException());
+
+ context.stop();
+ context.start();
+
+ // the endpoint gets a new pool once the CamelContext is started again
+ Exchange exchange = newExchange(producer);
+ CountDownLatch done = new CountDownLatch(1);
+ producer.process(exchange, doneSync -> done.countDown());
+ assertTrue(done.await(10, TimeUnit.SECONDS), "The call should
complete");
+ assertNull(exchange.getException());
+ assertEquals("Greetings World", exchange.getMessage().getBody());
+ }
+
+ private static AsyncProducer createProducer(CamelContext context) throws
Exception {
+ AsyncProducer producer = (AsyncProducer)
createEndpoint(context).createProducer();
+ producer.start();
+ return producer;
+ }
+
+ private static TestApiEndpoint createEndpoint(CamelContext context) throws
Exception {
+ TestApiComponent component = new TestApiComponent(context);
+ TestApiEndpoint endpoint = new TestApiEndpoint("test-api:greetMe",
component, "greetMe");
+ endpoint.setInBody("name");
+ endpoint.configureProperties(new HashMap<>());
+ return endpoint;
+ }
+
+ private static Exchange newExchange(AsyncProducer producer) {
+ Exchange exchange = producer.getEndpoint().createExchange();
+ exchange.getMessage().setBody("World");
+ return exchange;
+ }
+
+ /**
+ * Creates the scheduled pools of the CamelContext as its default factory
does, with the rejection policy of the
+ * profile, but the first submit to a pool waits until it is released.
+ */
+ static final class HoldingThreadPoolFactory implements ThreadPoolFactory {
+
+ final CountDownLatch held = new CountDownLatch(1);
+ final CountDownLatch release = new CountDownLatch(1);
+ final AtomicInteger created = new AtomicInteger();
+ private final ThreadPoolFactory delegate;
+
+ HoldingThreadPoolFactory(ThreadPoolFactory delegate) {
+ this.delegate = delegate;
+ }
+
+ @Override
+ public ScheduledExecutorService
newScheduledThreadPool(ThreadPoolProfile profile, ThreadFactory threadFactory) {
+ created.incrementAndGet();
+ RejectedExecutionHandler handler =
profile.getRejectedExecutionHandler();
+ if (handler == null) {
+ handler = new ThreadPoolExecutor.CallerRunsPolicy();
+ }
+ int size = profile.getPoolSize() != null ? profile.getPoolSize() :
1;
+ return new ScheduledThreadPoolExecutor(size, threadFactory,
handler) {
+ @Override
+ public Future<?> submit(Runnable task) {
+ held.countDown();
+ try {
+ release.await(10, TimeUnit.SECONDS);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ return super.submit(task);
+ }
+ };
+ }
+
+ @Override
+ public ExecutorService newCachedThreadPool(ThreadFactory
threadFactory) {
+ return delegate.newCachedThreadPool(threadFactory);
+ }
+
+ @Override
+ public ExecutorService newThreadPool(ThreadPoolProfile profile,
ThreadFactory threadFactory) {
+ return delegate.newThreadPool(profile, threadFactory);
+ }
+ }
+}
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiEndpoint.java
b/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiEndpoint.java
index d96b4d258563..de1fee37fb9a 100644
---
a/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiEndpoint.java
+++
b/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiEndpoint.java
@@ -24,6 +24,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
+import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@@ -36,6 +37,7 @@ import org.apache.camel.spi.ExecutorServiceManager;
import org.apache.camel.spi.PropertyConfigurer;
import org.apache.camel.spi.ThreadPoolProfile;
import org.apache.camel.spi.UriParam;
+import org.apache.camel.support.LifecycleStrategySupport;
import org.apache.camel.support.PropertyBindingSupport;
import org.apache.camel.support.PropertyConfigurerHelper;
import org.apache.camel.support.ScheduledPollEndpoint;
@@ -319,12 +321,23 @@ public abstract class AbstractApiEndpoint<E extends
ApiName, T>
if (executorServices == null) {
executorServices = new ExecutorServices();
ecc.addContextPlugin(ExecutorServices.class, executorServices);
+ // told when the CamelContext has stopped (and when it starts
again)
+ context.addLifecycleStrategy(executorServices);
}
// lookup executorService for extending class name
final String endpointClassName = endpointClass.getName();
ExecutorService executorService =
executorServices.pools.get(endpointClassName);
if (executorService == null || executorService.isShutdown()) {
+ if (executorServices.closed) {
+ // the CamelContext has stopped (its routes are drained):
a pool created now could be created after
+ // the ExecutorServiceManager shut down the pools of this
CamelContext, and then nothing would shut
+ // it down until the CamelContext is started and stopped
again. While the CamelContext is stopping
+ // and waits for its inflight exchanges, or before it is
started, a pool is created as usual
+ throw new RejectedExecutionException(
+ "CamelContext " + context.getName() + " is
stopped, no thread pool is created for "
+ + endpointClassName);
+ }
final ExecutorServiceManager manager =
context.getExecutorServiceManager();
// try to lookup a pool first based on profile
@@ -346,6 +359,14 @@ public abstract class AbstractApiEndpoint<E extends
ApiName, T>
}
}
+ /**
+ * The thread pool that the producers of this endpoint call the API in:
one pool per endpoint class per
+ * CamelContext, shut down when the CamelContext stops.
+ *
+ * @return the thread pool
+ * @throws RejectedExecutionException when the CamelContext has stopped
and the pool is not there, or shut down: no
+ * pool is created then
+ */
public final ExecutorService getExecutorService() {
ExecutorService answer = executorService;
// the pool is shut down when the CamelContext stops, and this
endpoint may be used again after a restart
@@ -366,9 +387,33 @@ public abstract class AbstractApiEndpoint<E extends
ApiName, T>
/**
* The thread pools of the API endpoints of one CamelContext, with the
endpoint class name as key (a context
- * plugin).
+ * plugin). As a lifecycle strategy of the CamelContext it knows when no
pool may be created: from
+ * {@link #onContextStopped}, which runs after the routes are drained and
before the ExecutorServiceManager shuts
+ * its pools down, until the CamelContext starts again.
*/
- private static final class ExecutorServices {
+ private static final class ExecutorServices extends
LifecycleStrategySupport {
private final Map<String, ExecutorService> pools = new HashMap<>();
+ // guarded by EXECUTOR_SERVICES_LOCK for writes, so a pool is created
either before it is set (and then shut
+ // down by the ExecutorServiceManager) or not at all
+ private volatile boolean closed;
+
+ @Override
+ public void onContextStarting(CamelContext context) {
+ setClosed(false);
+ }
+
+ @Override
+ public void onContextStopped(CamelContext context) {
+ setClosed(true);
+ }
+
+ private void setClosed(boolean closed) {
+ EXECUTOR_SERVICES_LOCK.lock();
+ try {
+ this.closed = closed;
+ } finally {
+ EXECUTOR_SERVICES_LOCK.unlock();
+ }
+ }
}
}
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiProducer.java
b/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiProducer.java
index 7060c3dc6431..07e111bbfd10 100644
---
a/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiProducer.java
+++
b/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiProducer.java
@@ -21,6 +21,9 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.camel.AsyncCallback;
import org.apache.camel.Exchange;
@@ -77,11 +80,18 @@ public abstract class AbstractApiProducer<E extends Enum<E>
& ApiName, T>
return true;
}
+ // the call runs once: set when the task starts, or when the call is
rejected after the task was submitted
+ final AtomicBoolean claimed = new AtomicBoolean();
+
// create a runnable invocation task to be submitted on a background
thread pool
// this way we avoid blocking the current thread for long-running
methods
Runnable invocation = new Runnable() {
@Override
public void run() {
+ if (!claimed.compareAndSet(false, true)) {
+ // the call was rejected because the pool was shut down
while the task was submitted
+ return;
+ }
try {
if (log.isDebugEnabled()) {
log.debug("Invoking operation {} with {}",
method.getName(), properties.keySet());
@@ -114,10 +124,48 @@ public abstract class AbstractApiProducer<E extends
Enum<E> & ApiName, T>
}
};
- endpoint.getExecutorService().submit(invocation);
+ final ExecutorService executorService;
+ try {
+ executorService = endpoint.getExecutorService();
+ } catch (RejectedExecutionException e) {
+ // the CamelContext has stopped, and has no pool for this endpoint
+ return rejected(exchange, callback, e);
+ }
+ if (executorService.isShutdown()) {
+ // the CallerRuns policy of a shut down pool would drop the task
without running it, and the exchange would
+ // never complete
+ return rejected(exchange, callback, newRejectedException(method));
+ }
+ try {
+ executorService.submit(invocation);
+ } catch (RejectedExecutionException e) {
+ if (claimed.compareAndSet(false, true)) {
+ return rejected(exchange, callback, e);
+ }
+ // a custom rejection policy ran the task synchronously before it
threw: the task completes the exchange
+ // (the JDK CallerRuns policy drops the task of a shut down pool,
and Abort throws without running it)
+ return false;
+ }
+ if (executorService.isShutdown() && claimed.compareAndSet(false,
true)) {
+ // the pool was shut down while the task was submitted, so the
task may have been dropped: the call is
+ // rejected (when the task was kept after all, it does not run the
call)
+ return rejected(exchange, callback, newRejectedException(method));
+ }
return false;
}
+ private RejectedExecutionException newRejectedException(ApiMethod method) {
+ return new RejectedExecutionException(
+ "The thread pool of " + endpoint + " is shut down, the call of
operation " + method.getName()
+ + " is rejected");
+ }
+
+ private static boolean rejected(Exchange exchange, AsyncCallback callback,
RejectedExecutionException cause) {
+ exchange.setException(cause);
+ callback.done(true);
+ return true;
+ }
+
@Override
public void interceptProperties(Map<String, Object> properties) {
// do nothing by default