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 4e4022ed9448 CAMEL-26538: camel-support - API components: the thread 
pool belongs to the CamelContext of the endpoint (#29999)
4e4022ed9448 is described below

commit 4e4022ed9448be94b059eb7bd75d672358ae59c7
Author: allthingssecurity <[email protected]>
AuthorDate: Sun Nov 15 14:14:34 2026 +0530

    CAMEL-26538: camel-support - API components: the thread pool belongs to the 
CamelContext of the endpoint (#29999)
    
    AbstractApiEndpoint kept the thread pool of the API component producers in 
a static map, one per endpoint class for the whole JVM, created by the first 
CamelContext that used it. With two CamelContexts in one JVM, stopping the 
first shut the pool down under the second, whose calls were then dropped and 
never completed; an endpoint kept across a restart of its own CamelContext 
broke the same way.
    
    There is now one pool per endpoint class per CamelContext, kept in a 
private context plugin and created through the ExecutorServiceManager of that 
CamelContext, and an endpoint looks the pool up again when the one it kept was 
shut down. New AbstractApiEndpointExecutorServiceTest in camel-core. The 4.24 
upgrade guide notes that an application with several CamelContexts can have 
more threads, each pool using the thread pool profile of its own CamelContext. 
A call that races the stop of  [...]
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../AbstractApiEndpointExecutorServiceTest.java    | 254 +++++++++++++++++++++
 .../support/component/AbstractApiEndpoint.java     |  83 ++++---
 .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc    |   7 +
 3 files changed, 310 insertions(+), 34 deletions(-)

diff --git 
a/core/camel-core/src/test/java/org/apache/camel/support/component/AbstractApiEndpointExecutorServiceTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/support/component/AbstractApiEndpointExecutorServiceTest.java
new file mode 100644
index 000000000000..2e0569967360
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/support/component/AbstractApiEndpointExecutorServiceTest.java
@@ -0,0 +1,254 @@
+/*
+ * 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.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Component;
+import org.apache.camel.Exchange;
+import org.apache.camel.Producer;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.DefaultCamelContext;
+import 
org.apache.camel.support.component.AbstractApiProducerTest.TestApiComponent;
+import 
org.apache.camel.support.component.AbstractApiProducerTest.TestApiEndpoint;
+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.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The thread pool of an API endpoint belongs to the CamelContext of the 
endpoint: stopping another CamelContext does
+ * not shut down the pool that the producers of this CamelContext use, and an 
endpoint that is used again after its
+ * CamelContext was started again gets a new pool.
+ */
+class AbstractApiEndpointExecutorServiceTest {
+
+    private CamelContext context1;
+    private CamelContext context2;
+
+    @AfterEach
+    void stopContexts() {
+        if (context1 != null) {
+            context1.stop();
+        }
+        if (context2 != null) {
+            context2.stop();
+        }
+    }
+
+    @Test
+    void testStoppingAnotherCamelContextDoesNotStopThePool() throws Exception {
+        context1 = new DefaultCamelContext();
+        context1.start();
+        context2 = new DefaultCamelContext();
+        context2.start();
+
+        TestApiEndpoint endpoint1 = createEndpoint(context1);
+        TestApiEndpoint endpoint2 = createEndpoint(context2);
+
+        // both CamelContexts call the API
+        assertGreeting(context1, endpoint1);
+        assertGreeting(context2, endpoint2);
+
+        context1.stop();
+
+        // the second CamelContext is still running, so its producer must 
still work
+        assertGreeting(context2, endpoint2);
+    }
+
+    @Test
+    void testStoppingAnotherCamelContextDoesNotInterruptItsCalls() throws 
Exception {
+        context1 = new DefaultCamelContext();
+        // do not wait long for the tasks still running in the pools when the 
CamelContext stops
+        context1.getExecutorServiceManager().setShutdownAwaitTermination(100);
+        context1.start();
+        context2 = new DefaultCamelContext();
+        context2.start();
+
+        BlockingApiEndpoint endpoint1 = new BlockingApiEndpoint(new 
TestApiComponent(context1));
+        BlockingApiEndpoint endpoint2 = new BlockingApiEndpoint(new 
TestApiComponent(context2));
+        assertGreeting(context1, endpoint1);
+
+        // a call of the second CamelContext is running when the first 
CamelContext stops
+        endpoint2.block = new CountDownLatch(1);
+        try (ProducerTemplate template = context2.createProducerTemplate()) {
+            CompletableFuture<Exchange> future = template.asyncSend(endpoint2, 
e -> e.getMessage().setBody("World"));
+            assertTrue(endpoint2.running.await(10, TimeUnit.SECONDS), "The 
call should be running");
+
+            try {
+                context1.stop();
+            } finally {
+                endpoint2.block.countDown();
+            }
+            assertCompleted(future);
+        }
+    }
+
+    @Test
+    void testEndpointKeptAcrossARestartCanStillCallTheApi() throws Exception {
+        context1 = new DefaultCamelContext();
+        // a lost call stays in flight, do not wait long for it when stopping
+        context1.getShutdownStrategy().setTimeout(1);
+        TestApiEndpoint endpoint = createEndpoint(context1);
+        // the route uses the endpoint object, which it keeps when the 
CamelContext is started again
+        context1.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start").to(endpoint);
+            }
+        });
+        context1.start();
+        assertRouteGreeting(context1);
+
+        // the pool of the endpoint is shut down when the CamelContext stops
+        context1.stop();
+        context1.start();
+
+        assertRouteGreeting(context1);
+    }
+
+    @Test
+    void testOnePoolPerCamelContextWhenFirstCallsRace() throws Exception {
+        context1 = new DefaultCamelContext();
+        context1.start();
+        context2 = new DefaultCamelContext();
+        context2.start();
+
+        List<TestApiEndpoint> endpoints = new ArrayList<>();
+        for (int i = 0; i < 4; i++) {
+            endpoints.add(createEndpoint(context1));
+            endpoints.add(createEndpoint(context2));
+        }
+
+        // the endpoints of both CamelContexts look up their pool at the same 
time
+        ExecutorService threads = 
Executors.newFixedThreadPool(endpoints.size());
+        try {
+            CountDownLatch go = new CountDownLatch(1);
+            List<Future<ExecutorService>> pools = new ArrayList<>();
+            for (TestApiEndpoint endpoint : endpoints) {
+                pools.add(threads.submit(() -> {
+                    go.await();
+                    return endpoint.getExecutorService();
+                }));
+            }
+            go.countDown();
+
+            Set<ExecutorService> pools1 = new HashSet<>();
+            Set<ExecutorService> pools2 = new HashSet<>();
+            for (int i = 0; i < endpoints.size(); i++) {
+                ExecutorService pool = pools.get(i).get(10, TimeUnit.SECONDS);
+                if (endpoints.get(i).getCamelContext() == context1) {
+                    pools1.add(pool);
+                } else {
+                    pools2.add(pool);
+                }
+            }
+            assertEquals(1, pools1.size(), "The endpoints of a CamelContext 
should share one pool");
+            assertEquals(1, pools2.size(), "The endpoints of a CamelContext 
should share one pool");
+            assertNotSame(pools1.iterator().next(), pools2.iterator().next(),
+                    "Each CamelContext should have its own pool");
+        } finally {
+            threads.shutdownNow();
+            threads.awaitTermination(10, TimeUnit.SECONDS);
+        }
+    }
+
+    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 void assertGreeting(CamelContext context, TestApiEndpoint 
endpoint) throws Exception {
+        try (ProducerTemplate template = context.createProducerTemplate()) {
+            // a call that is lost never completes, so do not wait for good
+            assertCompleted(template.asyncSend(endpoint, e -> 
e.getMessage().setBody("World")));
+        }
+    }
+
+    private static void assertRouteGreeting(CamelContext context) throws 
Exception {
+        try (ProducerTemplate template = context.createProducerTemplate()) {
+            assertCompleted(template.asyncSend("direct:start", e -> 
e.getMessage().setBody("World")));
+        }
+    }
+
+    private static void assertCompleted(CompletableFuture<Exchange> future) 
throws Exception {
+        Exchange out;
+        try {
+            out = future.get(10, TimeUnit.SECONDS);
+        } catch (TimeoutException e) {
+            throw new AssertionError("The call did not complete", e);
+        }
+        assertNull(out.getException(), "Should not fail: " + 
out.getException());
+        assertEquals("Greetings World", out.getMessage().getBody());
+    }
+
+    /**
+     * An API endpoint whose calls wait until they are released.
+     */
+    static class BlockingApiEndpoint extends TestApiEndpoint {
+
+        final CountDownLatch running = new CountDownLatch(1);
+        volatile CountDownLatch block;
+
+        BlockingApiEndpoint(Component component) throws Exception {
+            super("test-api:greetMe", component, "greetMe");
+            setInBody("name");
+            configureProperties(new HashMap<>());
+        }
+
+        @Override
+        public Producer createProducer() {
+            return new AbstractApiProducer<>(this, getPropertiesHelper()) {
+                @Override
+                protected Object doInvokeMethod(ApiMethod method, Map<String, 
Object> properties) {
+                    CountDownLatch latch = block;
+                    if (latch != null) {
+                        running.countDown();
+                        try {
+                            latch.await();
+                        } catch (InterruptedException e) {
+                            Thread.currentThread().interrupt();
+                            throw new RuntimeCamelException("The call was 
interrupted", e);
+                        }
+                    }
+                    return super.doInvokeMethod(method, properties);
+                }
+            };
+        }
+    }
+}
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 1444459b433b..d96b4d258563 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
@@ -23,13 +23,15 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ExecutorService;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
 
 import org.apache.camel.ApiEndpoint;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Component;
 import org.apache.camel.Consumer;
+import org.apache.camel.ExtendedCamelContext;
 import org.apache.camel.spi.ExecutorServiceManager;
 import org.apache.camel.spi.PropertyConfigurer;
 import org.apache.camel.spi.ThreadPoolProfile;
@@ -48,8 +50,8 @@ public abstract class AbstractApiEndpoint<E extends ApiName, 
T>
         extends ScheduledPollEndpoint
         implements ApiEndpoint, PropertyNamesInterceptor, 
PropertiesInterceptor {
 
-    // thread pool executor with Endpoint Class name as keys
-    private static final Map<String, ExecutorService> EXECUTOR_SERVICE_MAP = 
new ConcurrentHashMap<>();
+    // guards the lookup and creation of the thread pools of the API endpoints 
of a CamelContext
+    private static final Lock EXECUTOR_SERVICES_LOCK = new ReentrantLock();
 
     // logger
     protected final Logger log = LoggerFactory.getLogger(getClass());
@@ -309,45 +311,50 @@ public abstract class AbstractApiEndpoint<E extends 
ApiName, T>
     private static ExecutorService getExecutorService(
             Class<? extends AbstractApiEndpoint> endpointClass, CamelContext 
context, String threadProfileName) {
 
-        // lookup executorService for extending class name
-        final String endpointClassName = endpointClass.getName();
-        ExecutorService executorService = 
EXECUTOR_SERVICE_MAP.get(endpointClassName);
-
-        // CamelContext will shutdown thread pool when it shutdown so we can
-        // lazy create it on demand
-        // but in case of hot-deploy or the likes we need to be able to
-        // re-create it (its a shared static instance)
-        if (executorService == null || executorService.isTerminated() || 
executorService.isShutdown()) {
-            final ExecutorServiceManager manager = 
context.getExecutorServiceManager();
-
-            // try to lookup a pool first based on profile
-            ThreadPoolProfile poolProfile = manager.getThreadPoolProfile(
-                    threadProfileName);
-            if (poolProfile == null) {
-                poolProfile = manager.getDefaultThreadPoolProfile();
+        // one pool per endpoint class per CamelContext, created and shut down 
by its ExecutorServiceManager
+        EXECUTOR_SERVICES_LOCK.lock();
+        try {
+            final ExtendedCamelContext ecc = 
context.getCamelContextExtension();
+            ExecutorServices executorServices = 
ecc.getContextPlugin(ExecutorServices.class);
+            if (executorServices == null) {
+                executorServices = new ExecutorServices();
+                ecc.addContextPlugin(ExecutorServices.class, executorServices);
             }
 
-            // create a new pool using the custom or default profile
-            executorService = manager.newScheduledThreadPool(endpointClass, 
threadProfileName, poolProfile);
+            // lookup executorService for extending class name
+            final String endpointClassName = endpointClass.getName();
+            ExecutorService executorService = 
executorServices.pools.get(endpointClassName);
+            if (executorService == null || executorService.isShutdown()) {
+                final ExecutorServiceManager manager = 
context.getExecutorServiceManager();
+
+                // try to lookup a pool first based on profile
+                ThreadPoolProfile poolProfile = manager.getThreadPoolProfile(
+                        threadProfileName);
+                if (poolProfile == null) {
+                    poolProfile = manager.getDefaultThreadPoolProfile();
+                }
 
-            EXECUTOR_SERVICE_MAP.put(endpointClassName, executorService);
-        }
+                // create a new pool using the custom or default profile
+                executorService = 
manager.newScheduledThreadPool(endpointClass, threadProfileName, poolProfile);
+
+                executorServices.pools.put(endpointClassName, executorService);
+            }
 
-        return executorService;
+            return executorService;
+        } finally {
+            EXECUTOR_SERVICES_LOCK.unlock();
+        }
     }
 
     public final ExecutorService getExecutorService() {
-        if (executorService == null) {
-            lock.lock();
-            try {
-                if (executorService == null) {
-                    executorService = getExecutorService(getClass(), 
getCamelContext(), getThreadProfileName());
-                }
-            } finally {
-                lock.unlock();
-            }
+        ExecutorService answer = executorService;
+        // the pool is shut down when the CamelContext stops, and this 
endpoint may be used again after a restart
+        if (answer == null || answer.isShutdown()) {
+            // the lookup returns the same pool to every endpoint of this 
class in the CamelContext
+            answer = getExecutorService(getClass(), getCamelContext(), 
getThreadProfileName());
+            executorService = answer;
         }
-        return executorService;
+        return answer;
     }
 
     /**
@@ -356,4 +363,12 @@ public abstract class AbstractApiEndpoint<E extends 
ApiName, T>
      * @return thread profile name to use.
      */
     protected abstract String getThreadProfileName();
+
+    /**
+     * The thread pools of the API endpoints of one CamelContext, with the 
endpoint class name as key (a context
+     * plugin).
+     */
+    private static final class ExecutorServices {
+        private final Map<String, ExecutorService> pools = new HashMap<>();
+    }
 }
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 9d73496ef940..d0b06d00c7d5 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
@@ -1191,6 +1191,13 @@ as `camel run` already did. Before, the failure left 
`main()` and only the JVM p
 The JVM no longer prints that same failure a second time. When the application 
logs nothing at ERROR (such as without
 a logging implementation), the JVM prints it as before.
 
+=== camel-support - API components use one thread pool per CamelContext
+
+The producers of the API components (AS2, Box, Braintree, DHIS2, FHIR, Google 
Calendar, Google Drive, Google Mail,
+Google Sheets, Twilio and Zendesk) now use one thread pool per component in 
each CamelContext, instead of one pool
+shared by all the CamelContexts of the JVM. An application with several 
CamelContexts can therefore have more threads,
+and each pool uses the thread pool profile (such as `CamelGoogleDrive`) of its 
own CamelContext.
+
 === camel-report-maven-plugin:validate / configuration-validate
 
 The catalog now accepts all `camel.*` groups defined in 
`camel-main-configuration-metadata.json`

Reply via email to