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 9326b38f5ae5 CAMEL-27152: camel-ai-prompt, camel-ai-resource - a call
in flight completes when its route stops (#30336)
9326b38f5ae5 is described below
commit 9326b38f5ae533f6bf12aa07219ad2ed93cf9bb6
Author: Serdar Gökay <[email protected]>
AuthorDate: Wed Nov 18 22:58:40 2026 +0300
CAMEL-27152: camel-ai-prompt, camel-ai-resource - a call in flight
completes when its route stops (#30336)
A consumer that is neither Suspendable nor ShutdownAware is stopped at once
by DefaultShutdownStrategy, and
DefaultConsumer.doStop stops the route processor. A prompt request or
resource read still inside the route then failed
at its next step with RouteStoppingException, while the strategy waited for
it. AiPromptConsumer and AiResourceConsumer
had the same shape as AiToolConsumer before its fix.
Both consumers are now Suspendable: a route stop or graceful shutdown
suspends them first, which deregisters the
prompt, resource or resource template, and stops them once the calls in
flight have completed. Their doSuspend and
doResume become reachable, so suspendRoute suspends the route instead of
stopping it. AiPromptExecutor and
AiResourceExecutor refuse a new call to a suspended prompt or resource, as
AiToolExecutor does, and name the prompt in
the suspended cause; resource template reads go through AiResourceExecutor
and are refused the same way.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
---
components/camel-ai/camel-ai-prompt/pom.xml | 6 ++
.../component/ai/prompt/AiPromptConsumer.java | 8 +-
.../component/ai/prompt/AiPromptExecutor.java | 7 ++
.../component/ai/prompt/AiPromptComponentTest.java | 35 ++++++++
.../ai/prompt/AiPromptRouteStopInFlightTest.java | 97 ++++++++++++++++++++++
components/camel-ai/camel-ai-resource/pom.xml | 6 ++
.../component/ai/resource/AiResourceConsumer.java | 8 +-
.../component/ai/resource/AiResourceExecutor.java | 8 ++
.../ai/resource/AiResourceExecutorTest.java | 71 +++++++++++++++-
.../resource/AiResourceRouteStopInFlightTest.java | 97 ++++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 11 +--
11 files changed, 344 insertions(+), 10 deletions(-)
diff --git a/components/camel-ai/camel-ai-prompt/pom.xml
b/components/camel-ai/camel-ai-prompt/pom.xml
index 1b728819f46f..bf19ce0ade81 100644
--- a/components/camel-ai/camel-ai-prompt/pom.xml
+++ b/components/camel-ai/camel-ai-prompt/pom.xml
@@ -60,5 +60,11 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.awaitility</groupId>
+ <artifactId>awaitility</artifactId>
+ <version>${awaitility-version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git
a/components/camel-ai/camel-ai-prompt/src/main/java/org/apache/camel/component/ai/prompt/AiPromptConsumer.java
b/components/camel-ai/camel-ai-prompt/src/main/java/org/apache/camel/component/ai/prompt/AiPromptConsumer.java
index 3fd7ba3074c2..15b022752489 100644
---
a/components/camel-ai/camel-ai-prompt/src/main/java/org/apache/camel/component/ai/prompt/AiPromptConsumer.java
+++
b/components/camel-ai/camel-ai-prompt/src/main/java/org/apache/camel/component/ai/prompt/AiPromptConsumer.java
@@ -24,6 +24,7 @@ import java.util.Locale;
import java.util.Map;
import org.apache.camel.Processor;
+import org.apache.camel.Suspendable;
import org.apache.camel.support.CamelContextHelper;
import org.apache.camel.support.DefaultConsumer;
import org.slf4j.Logger;
@@ -31,10 +32,15 @@ import org.slf4j.LoggerFactory;
/**
* Consumer registering a prompt route while its route is started and removing
it when stopped.
+ * <p>
+ * It is {@link Suspendable}, so a route stop or graceful shutdown suspends it
first, which deregisters the prompt, and
+ * stops it once the prompt requests in flight have completed or the shutdown
timeout has elapsed. Suspending the route
+ * deregisters the prompt without stopping the route, and resuming it
registers the same prompt spec again. A suspended
+ * prompt refuses new requests.
*
* @since 4.24
*/
-public class AiPromptConsumer extends DefaultConsumer {
+public class AiPromptConsumer extends DefaultConsumer implements Suspendable {
private static final Logger LOG =
LoggerFactory.getLogger(AiPromptConsumer.class);
diff --git
a/components/camel-ai/camel-ai-prompt/src/main/java/org/apache/camel/component/ai/prompt/AiPromptExecutor.java
b/components/camel-ai/camel-ai-prompt/src/main/java/org/apache/camel/component/ai/prompt/AiPromptExecutor.java
index 518548f80e9e..f3ad0d1ef455 100644
---
a/components/camel-ai/camel-ai-prompt/src/main/java/org/apache/camel/component/ai/prompt/AiPromptExecutor.java
+++
b/components/camel-ai/camel-ai-prompt/src/main/java/org/apache/camel/component/ai/prompt/AiPromptExecutor.java
@@ -48,6 +48,13 @@ public final class AiPromptExecutor {
"No consumer available for prompt '" + promptName + "'",
new IllegalStateException("No consumer available"));
}
+ // A suspended prompt (suspended route, graceful shutdown) refuses new
requests, as request/reply consumers do
+ // while suspended; a caller may hold its spec from before the
suspend. Requests in flight are past this point.
+ if (consumer.isSuspendingOrSuspended()) {
+ IllegalStateException cause = new IllegalStateException(
+ String.format("Prompt '%s' is suspended", promptName));
+ return new AiPromptResult.ExecutionError(cause.getMessage(),
cause);
+ }
Processor processor = consumer.getProcessor();
if (processor == null) {
return new AiPromptResult.ExecutionError(
diff --git
a/components/camel-ai/camel-ai-prompt/src/test/java/org/apache/camel/component/ai/prompt/AiPromptComponentTest.java
b/components/camel-ai/camel-ai-prompt/src/test/java/org/apache/camel/component/ai/prompt/AiPromptComponentTest.java
index 8241169637ac..84e59622d99a 100644
---
a/components/camel-ai/camel-ai-prompt/src/test/java/org/apache/camel/component/ai/prompt/AiPromptComponentTest.java
+++
b/components/camel-ai/camel-ai-prompt/src/test/java/org/apache/camel/component/ai/prompt/AiPromptComponentTest.java
@@ -48,6 +48,10 @@ class AiPromptComponentTest extends CamelTestSupport {
from("ai-prompt:ordered?tags=tests&argument.second.description=Second"
+ "&argument.first.description=First")
.setBody(constant("ordered"));
+
+ from("ai-prompt:suspendable?tags=tests&description=A prompt
whose route is suspended and resumed")
+ .routeId("suspendable-route")
+ .setBody(constant("suspendable result"));
}
};
}
@@ -109,4 +113,35 @@ class AiPromptComponentTest extends CamelTestSupport {
assertThat(((AiPromptResult.ArgumentError) result).message())
.contains("Missing required argument 'name'");
}
+
+ @Test
+ void suspendedPromptRefusesRequestsUntilItsRouteResumes() throws Exception
{
+ // a caller can hold a spec it obtained before the route was suspended
+ AiPromptSpec spec = findSpec("suspendable");
+ context.getRouteController().suspendRoute("suspendable-route");
+
+ AiPromptResult refused = AiPromptExecutor.execute(
+ spec, Map.of(),
spec.getConsumer().getEndpoint().createExchange());
+
+ assertThat(refused).isInstanceOf(AiPromptResult.ExecutionError.class);
+ AiPromptResult.ExecutionError error = (AiPromptResult.ExecutionError)
refused;
+ assertThat(error.message()).isEqualTo("Prompt 'suspendable' is
suspended");
+ assertThat(error.cause()).isInstanceOf(IllegalStateException.class)
+ .hasMessage("Prompt 'suspendable' is suspended");
+
+ context.getRouteController().resumeRoute("suspendable-route");
+
+ assertThat(findSpec("suspendable")).isSameAs(spec);
+ AiPromptResult result = AiPromptExecutor.execute(
+ spec, Map.of(),
spec.getConsumer().getEndpoint().createExchange());
+ assertThat(result).isInstanceOf(AiPromptResult.Success.class);
+ assertThat(((AiPromptResult.Success)
result).value()).isEqualTo("suspendable result");
+ }
+
+ private AiPromptSpec findSpec(String promptName) {
+ return
AiPromptRegistry.getOrCreate(context).getPromptsByTag("tests").stream()
+ .filter(prompt -> promptName.equals(prompt.getName()))
+ .findFirst()
+ .orElseThrow(() -> new AssertionError("Prompt not found: " +
promptName));
+ }
}
diff --git
a/components/camel-ai/camel-ai-prompt/src/test/java/org/apache/camel/component/ai/prompt/AiPromptRouteStopInFlightTest.java
b/components/camel-ai/camel-ai-prompt/src/test/java/org/apache/camel/component/ai/prompt/AiPromptRouteStopInFlightTest.java
new file mode 100644
index 000000000000..7405c9990a0a
--- /dev/null
+++
b/components/camel-ai/camel-ai-prompt/src/test/java/org/apache/camel/component/ai/prompt/AiPromptRouteStopInFlightTest.java
@@ -0,0 +1,97 @@
+/*
+ * 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.ai.prompt;
+
+import java.util.Map;
+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 org.apache.camel.ServiceStatus;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.awaitility.Awaitility.await;
+
+/**
+ * A prompt request that is in flight inside an {@code ai-prompt} route when
the route is stopped completes: the
+ * shutdown strategy suspends the consumer, which deregisters the prompt,
waits for the request, and then stops the
+ * route.
+ */
+class AiPromptRouteStopInFlightTest extends CamelTestSupport {
+
+ private final CountDownLatch entered = new CountDownLatch(1);
+ private final CountDownLatch release = new CountDownLatch(1);
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("ai-prompt:held?tags=test&description=Held inside the
route").routeId("held")
+ .process(exchange -> {
+ entered.countDown();
+ if (!release.await(10, TimeUnit.SECONDS)) {
+ throw new IllegalStateException("The request
was not released");
+ }
+ })
+ .setBody(constant("ok"));
+ }
+ };
+ }
+
+ @Test
+ void aRequestHeldInsideTheRouteCompletesWhenTheRouteStops() throws
Exception {
+ AiPromptSpec spec = findSpec("held");
+ ExecutorService executor = Executors.newFixedThreadPool(2);
+ try {
+ Future<AiPromptResult> request = executor.submit(
+ () -> AiPromptExecutor.execute(spec, Map.of(),
spec.getConsumer().getEndpoint().createExchange()));
+ assertThat(entered.await(10, TimeUnit.SECONDS)).isTrue();
+
+ Future<?> stop = executor.submit(() -> {
+ context.getRouteController().stopRoute("held");
+ return null;
+ });
+ // the shutdown strategy has suspended the consumer and now waits
for the request
+ await().atMost(10, TimeUnit.SECONDS).until(() ->
spec.getConsumer().isSuspended());
+
assertThat(AiPromptRegistry.getOrCreate(context).getPromptsByTag("test"))
+ .extracting(AiPromptSpec::getName)
+ .doesNotContain("held");
+ release.countDown();
+
+ AiPromptResult result = request.get(10, TimeUnit.SECONDS);
+ assertThat(result).isInstanceOf(AiPromptResult.Success.class);
+ assertThat(((AiPromptResult.Success)
result).value()).isEqualTo("ok");
+ stop.get(10, TimeUnit.SECONDS);
+
assertThat(context.getRouteController().getRouteStatus("held")).isEqualTo(ServiceStatus.Stopped);
+ } finally {
+ executor.shutdownNow();
+ }
+ }
+
+ private AiPromptSpec findSpec(String promptName) {
+ return
AiPromptRegistry.getOrCreate(context).getPromptsByTag("test").stream()
+ .filter(s -> promptName.equals(s.getName()))
+ .findFirst()
+ .orElseThrow(() -> new AssertionError("Prompt not found: " +
promptName));
+ }
+}
diff --git a/components/camel-ai/camel-ai-resource/pom.xml
b/components/camel-ai/camel-ai-resource/pom.xml
index fbb0492f12b5..9d1fc1d4d305 100644
--- a/components/camel-ai/camel-ai-resource/pom.xml
+++ b/components/camel-ai/camel-ai-resource/pom.xml
@@ -61,6 +61,12 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.awaitility</groupId>
+ <artifactId>awaitility</artifactId>
+ <version>${awaitility-version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git
a/components/camel-ai/camel-ai-resource/src/main/java/org/apache/camel/component/ai/resource/AiResourceConsumer.java
b/components/camel-ai/camel-ai-resource/src/main/java/org/apache/camel/component/ai/resource/AiResourceConsumer.java
index 4db3fda3207c..529405b828fc 100644
---
a/components/camel-ai/camel-ai-resource/src/main/java/org/apache/camel/component/ai/resource/AiResourceConsumer.java
+++
b/components/camel-ai/camel-ai-resource/src/main/java/org/apache/camel/component/ai/resource/AiResourceConsumer.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.ai.resource;
import java.util.Arrays;
import org.apache.camel.Processor;
+import org.apache.camel.Suspendable;
import org.apache.camel.support.CamelContextHelper;
import org.apache.camel.support.DefaultConsumer;
import org.slf4j.Logger;
@@ -27,10 +28,15 @@ import org.slf4j.LoggerFactory;
/**
* Consumer that registers a Camel route as an AI resource in the {@link
AiResourceRegistry} on start and deregisters on
* stop.
+ * <p>
+ * It is {@link Suspendable}, so a route stop or graceful shutdown suspends it
first, which deregisters the resource or
+ * resource template, and stops it once the resource reads in flight have
completed or the shutdown timeout has elapsed.
+ * Suspending the route deregisters the resource or resource template without
stopping the route, and resuming it
+ * registers the same spec again. A suspended resource refuses new reads.
*
* @since 4.23
*/
-public class AiResourceConsumer extends DefaultConsumer {
+public class AiResourceConsumer extends DefaultConsumer implements Suspendable
{
private static final Logger LOG =
LoggerFactory.getLogger(AiResourceConsumer.class);
diff --git
a/components/camel-ai/camel-ai-resource/src/main/java/org/apache/camel/component/ai/resource/AiResourceExecutor.java
b/components/camel-ai/camel-ai-resource/src/main/java/org/apache/camel/component/ai/resource/AiResourceExecutor.java
index 672727f8ea8b..ae996e5b0bd5 100644
---
a/components/camel-ai/camel-ai-resource/src/main/java/org/apache/camel/component/ai/resource/AiResourceExecutor.java
+++
b/components/camel-ai/camel-ai-resource/src/main/java/org/apache/camel/component/ai/resource/AiResourceExecutor.java
@@ -61,6 +61,14 @@ public final class AiResourceExecutor {
return new AiResourceResult.ExecutionError(cause.getMessage(),
cause);
}
+ // A suspended resource (suspended route, graceful shutdown) refuses
new reads, as request/reply consumers do
+ // while suspended; a caller may still hold its spec from before the
suspend. Reads in flight are past this point.
+ if (consumer.isSuspendingOrSuspended()) {
+ IllegalStateException cause = new IllegalStateException(
+ String.format("Resource '%s' is suspended", resourceName));
+ return new AiResourceResult.ExecutionError(cause.getMessage(),
cause);
+ }
+
Processor routeProcessor = consumer.getProcessor();
if (routeProcessor == null) {
IllegalStateException cause = new IllegalStateException(
diff --git
a/components/camel-ai/camel-ai-resource/src/test/java/org/apache/camel/component/ai/resource/AiResourceExecutorTest.java
b/components/camel-ai/camel-ai-resource/src/test/java/org/apache/camel/component/ai/resource/AiResourceExecutorTest.java
index 9461fe8fd6a7..e3d586c3ce72 100644
---
a/components/camel-ai/camel-ai-resource/src/test/java/org/apache/camel/component/ai/resource/AiResourceExecutorTest.java
+++
b/components/camel-ai/camel-ai-resource/src/test/java/org/apache/camel/component/ai/resource/AiResourceExecutorTest.java
@@ -80,6 +80,18 @@ public class AiResourceExecutorTest extends CamelTestSupport
{
+ "?resourceUri=camel:///exchange-failure"
+ "&tags=test")
.process(exchange -> exchange.setException(new
IllegalStateException("exchange failure")));
+
+ from("ai-resource:suspendable"
+ + "?resourceUri=camel:///suspendable"
+ + "&tags=test")
+ .routeId("suspendable-route")
+ .setBody(constant("suspendable content"));
+
+ from("ai-resource:suspendable_template"
+ + "?uriTemplate=camel:///suspendable-items/{id}"
+ + "&tags=test")
+ .routeId("suspendable-template-route")
+ .setBody(constant("suspendable item"));
}
};
}
@@ -172,12 +184,65 @@ public class AiResourceExecutorTest extends
CamelTestSupport {
.doesNotContain("camel:///orphan");
}
+ @Test
+ public void testSuspendedResourceRefusesReadsUntilItsRouteResumes() throws
Exception {
+ // a caller can hold a spec it obtained before the route was suspended
+ AiResourceSpec spec = findSpec("camel:///suspendable");
+ context.getRouteController().suspendRoute("suspendable-route");
+
+ AiResourceResult refused = AiResourceExecutor.execute(spec, new
DefaultExchange(context));
+
+
assertThat(refused).isInstanceOf(AiResourceResult.ExecutionError.class);
+ AiResourceResult.ExecutionError error =
(AiResourceResult.ExecutionError) refused;
+ assertThat(error.message()).isEqualTo("Resource 'suspendable' is
suspended");
+ assertThat(error.cause()).isInstanceOf(IllegalStateException.class);
+
+ context.getRouteController().resumeRoute("suspendable-route");
+
+ assertThat(findSpec("camel:///suspendable")).isSameAs(spec);
+ AiResourceResult result = AiResourceExecutor.execute(spec, new
DefaultExchange(context));
+ assertThat(result).isInstanceOf(AiResourceResult.Text.class);
+ assertThat(((AiResourceResult.Text)
result).value()).isEqualTo("suspendable content");
+ }
+
+ @Test
+ public void
testSuspendedResourceTemplateRefusesReadsUntilItsRouteResumes() throws
Exception {
+ AiResourceTemplateSpec template =
findTemplate("camel:///suspendable-items/{id}");
+
context.getRouteController().suspendRoute("suspendable-template-route");
+
+
assertThat(AiResourceRegistry.getOrCreate(context).getResourceTemplatesByTag("test")).doesNotContain(template);
+ AiResourceResult refused = AiResourceExecutor.execute(
+ template.asResourceSpec("camel:///suspendable-items/42"), new
DefaultExchange(context));
+
+
assertThat(refused).isInstanceOf(AiResourceResult.ExecutionError.class);
+ assertThat(((AiResourceResult.ExecutionError) refused).message())
+ .isEqualTo("Resource 'suspendable_template' is suspended");
+
+ context.getRouteController().resumeRoute("suspendable-template-route");
+
+
assertThat(findTemplate("camel:///suspendable-items/{id}")).isSameAs(template);
+ AiResourceResult result = AiResourceExecutor.execute(
+ template.asResourceSpec("camel:///suspendable-items/42"), new
DefaultExchange(context));
+ assertThat(result).isInstanceOf(AiResourceResult.Text.class);
+ assertThat(((AiResourceResult.Text)
result).value()).isEqualTo("suspendable item");
+ }
+
private AiResourceResult read(String resourceUri) {
- AiResourceSpec spec =
AiResourceRegistry.getOrCreate(context).getResourcesByTag("test").stream()
+ Exchange exchange = new DefaultExchange(context);
+ return AiResourceExecutor.execute(findSpec(resourceUri), exchange);
+ }
+
+ private AiResourceSpec findSpec(String resourceUri) {
+ return
AiResourceRegistry.getOrCreate(context).getResourcesByTag("test").stream()
.filter(s -> resourceUri.equals(s.getUri()))
.findFirst()
.orElseThrow(() -> new AssertionError("Resource not found: " +
resourceUri));
- Exchange exchange = new DefaultExchange(context);
- return AiResourceExecutor.execute(spec, exchange);
+ }
+
+ private AiResourceTemplateSpec findTemplate(String uriTemplate) {
+ return
AiResourceRegistry.getOrCreate(context).getResourceTemplatesByTag("test").stream()
+ .filter(s -> uriTemplate.equals(s.getUriTemplate()))
+ .findFirst()
+ .orElseThrow(() -> new AssertionError("Resource template not
found: " + uriTemplate));
}
}
diff --git
a/components/camel-ai/camel-ai-resource/src/test/java/org/apache/camel/component/ai/resource/AiResourceRouteStopInFlightTest.java
b/components/camel-ai/camel-ai-resource/src/test/java/org/apache/camel/component/ai/resource/AiResourceRouteStopInFlightTest.java
new file mode 100644
index 000000000000..80cbda278a25
--- /dev/null
+++
b/components/camel-ai/camel-ai-resource/src/test/java/org/apache/camel/component/ai/resource/AiResourceRouteStopInFlightTest.java
@@ -0,0 +1,97 @@
+/*
+ * 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.ai.resource;
+
+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 org.apache.camel.ServiceStatus;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.support.DefaultExchange;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.awaitility.Awaitility.await;
+
+/**
+ * A resource read that is in flight inside an {@code ai-resource} route when
the route is stopped completes: the
+ * shutdown strategy suspends the consumer, which deregisters the resource,
waits for the read, and then stops the
+ * route.
+ */
+class AiResourceRouteStopInFlightTest extends CamelTestSupport {
+
+ private final CountDownLatch entered = new CountDownLatch(1);
+ private final CountDownLatch release = new CountDownLatch(1);
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ public void configure() {
+
from("ai-resource:held?resourceUri=camel:///held&tags=test&description=Held
inside the route")
+ .routeId("held")
+ .process(exchange -> {
+ entered.countDown();
+ if (!release.await(10, TimeUnit.SECONDS)) {
+ throw new IllegalStateException("The read was
not released");
+ }
+ })
+ .setBody(constant("ok"));
+ }
+ };
+ }
+
+ @Test
+ void aReadHeldInsideTheRouteCompletesWhenTheRouteStops() throws Exception {
+ AiResourceSpec spec = findSpec("camel:///held");
+ ExecutorService executor = Executors.newFixedThreadPool(2);
+ try {
+ Future<AiResourceResult> read
+ = executor.submit(() -> AiResourceExecutor.execute(spec,
new DefaultExchange(context)));
+ assertThat(entered.await(10, TimeUnit.SECONDS)).isTrue();
+
+ Future<?> stop = executor.submit(() -> {
+ context.getRouteController().stopRoute("held");
+ return null;
+ });
+ // the shutdown strategy has suspended the consumer and now waits
for the read
+ await().atMost(10, TimeUnit.SECONDS).until(() ->
spec.getConsumer().isSuspended());
+
assertThat(AiResourceRegistry.getOrCreate(context).getResourcesByTag("test"))
+ .extracting(AiResourceSpec::getUri)
+ .doesNotContain("camel:///held");
+ release.countDown();
+
+ AiResourceResult result = read.get(10, TimeUnit.SECONDS);
+ assertThat(result).isInstanceOf(AiResourceResult.Text.class);
+ assertThat(((AiResourceResult.Text)
result).value()).isEqualTo("ok");
+ stop.get(10, TimeUnit.SECONDS);
+
assertThat(context.getRouteController().getRouteStatus("held")).isEqualTo(ServiceStatus.Stopped);
+ } finally {
+ executor.shutdownNow();
+ }
+ }
+
+ private AiResourceSpec findSpec(String resourceUri) {
+ return
AiResourceRegistry.getOrCreate(context).getResourcesByTag("test").stream()
+ .filter(s -> resourceUri.equals(s.getUri()))
+ .findFirst()
+ .orElseThrow(() -> new AssertionError("Resource not found: " +
resourceUri));
+ }
+}
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 c4407447dd6a..e345deaa03b0 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
@@ -393,12 +393,13 @@ now reject the invocation before the tool route runs.
Previously, these argument
required-argument check and then be discarded by the reserved-name filter,
leaving the route without
a required value. Rename these parameters and update their `parameter.*` or
`argSchema` declarations.
-=== camel-ai-tool - suspending a route no longer stops it, and tool calls in
flight complete when the route stops
+=== camel-ai-tool, camel-ai-resource - suspending a route no longer stops it,
and calls in flight complete when the route stops
-Suspending an `ai-tool` route, with `suspendRoute` or `context.suspend()`, now
suspends the route instead of stopping
-it: the tool is deregistered, tool calls in flight complete, a new call to the
suspended tool is refused, and resuming
-the route registers the tool again. When an `ai-tool` route is stopped, or
Camel shuts down gracefully, tool calls in
-flight now complete instead of failing.
+Suspending an `ai-tool` or `ai-resource` route, with `suspendRoute` or
`context.suspend()`, now suspends the route
+instead of stopping it: the tool or resource is deregistered, tool calls and
resource reads in flight complete, a new
+call to the suspended tool or read of the suspended resource is refused, and
resuming the route registers the tool or
+resource again. When an `ai-tool` or `ai-resource` route is stopped, or Camel
shuts down gracefully, tool calls and
+resource reads in flight now complete instead of failing.
=== camel-wordpress - each endpoint uses its own site URL and credentials