-
Notifications
You must be signed in to change notification settings - Fork 5.2k
CAMEL-24811: camel-servlet - fix async servlet completing before the route finishes (backport 4.22.x) #26596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
davsclaus
merged 1 commit into
apache:camel-4.22.x
from
Croway:CAMEL-24811-servlet-async-race-4.22.x
Sep 18, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...amel-jetty/src/test/java/org/apache/camel/component/jetty/JettyAsyncDelayedRouteTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * 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.jetty; | ||
|
|
||
| import org.apache.camel.builder.RouteBuilder; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| /** | ||
| * async=true must wait for a route step that resumes on another thread before completing the request. | ||
| */ | ||
| public class JettyAsyncDelayedRouteTest extends BaseJettyTest { | ||
|
|
||
| @Test | ||
| public void testAsyncRouteCompletesBeforeResponse() { | ||
| String body = template.requestBody("http://localhost:{{port}}/racy", "hello", String.class); | ||
| assertEquals("delayed-response", body); | ||
| } | ||
|
|
||
| @Override | ||
| protected RouteBuilder createRouteBuilder() { | ||
| return new RouteBuilder() { | ||
| public void configure() { | ||
| // async and continuation is not compatible! | ||
| from("jetty:http://localhost:{{port}}/racy?async=true&useContinuation=false") | ||
| // resumes on another thread | ||
| .delay(300).asyncDelayed().end() | ||
| .transform().constant("delayed-response"); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| } |
98 changes: 98 additions & 0 deletions
98
...camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletAsyncErrorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| * 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.servlet; | ||
|
|
||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| import io.undertow.servlet.Servlets; | ||
| import io.undertow.servlet.api.DeploymentInfo; | ||
| import org.apache.camel.AsyncCallback; | ||
| import org.apache.camel.Exchange; | ||
| import org.apache.camel.builder.RouteBuilder; | ||
| import org.apache.camel.support.AsyncProcessorSupport; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| /** | ||
| * async=true must still write an error response when the route fails after resuming on another thread, or when the | ||
| * processor's stage completes exceptionally. | ||
| */ | ||
| public class ServletAsyncErrorTest extends ServletCamelRouterTestSupport { | ||
|
|
||
| @Test | ||
| public void testAsyncRouteThrows() throws Exception { | ||
| WebResponse response = query(new GetMethodWebRequest(contextUrl + "/services/async-error"), false); | ||
|
|
||
| assertEquals(500, response.getResponseCode()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAsyncStageCompletesExceptionally() throws Exception { | ||
| // a route processor never completes the stage exceptionally (the exception lands on the exchange), so | ||
| // plug a consumer with a custom AsyncProcessor directly | ||
| ServletEndpoint endpoint = context.getEndpoint("servlet:///async-failed", ServletEndpoint.class); | ||
| ServletConsumer consumer = (ServletConsumer) endpoint.createConsumer(new AsyncProcessorSupport() { | ||
| @Override | ||
| public boolean process(Exchange exchange, AsyncCallback callback) { | ||
| callback.done(true); | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<Exchange> processAsync(Exchange exchange) { | ||
| return CompletableFuture.failedFuture(new IllegalStateException("stage failed")); | ||
| } | ||
| }); | ||
| consumer.start(); | ||
| try { | ||
| WebResponse response = query(new GetMethodWebRequest(contextUrl + "/services/async-failed"), false); | ||
|
|
||
| assertEquals(500, response.getResponseCode()); | ||
| } finally { | ||
| consumer.stop(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected DeploymentInfo getDeploymentInfo() { | ||
| return Servlets.deployment() | ||
| .setClassLoader(getClass().getClassLoader()) | ||
| .setContextPath(CONTEXT) | ||
| .setDeploymentName(getClass().getName()) | ||
| .addServlet(Servlets.servlet("CamelServlet", CamelHttpTransportServlet.class) | ||
| .addInitParam("async", "true") | ||
| .setAsyncSupported(true) | ||
| .addMapping("/services/*")); | ||
| } | ||
|
|
||
| @Override | ||
| protected RouteBuilder createRouteBuilder() { | ||
| return new RouteBuilder() { | ||
| @Override | ||
| public void configure() { | ||
| from("servlet:///async-error") | ||
| // resumes on another thread, then fails | ||
| .delay(100).asyncDelayed().end() | ||
| .process(e -> { | ||
| throw new IllegalStateException("boom"); | ||
| }); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| } |
56 changes: 56 additions & 0 deletions
56
...t/src/test/java/org/apache/camel/component/servlet/ServletAsyncNoExecutorRefRaceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * 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.servlet; | ||
|
|
||
| import io.undertow.servlet.Servlets; | ||
| import io.undertow.servlet.api.DeploymentInfo; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.web.context.ContextLoaderListener; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| /** | ||
| * async=true without executorRef must wait for a route step that resumes on another thread before completing. | ||
| */ | ||
| public class ServletAsyncNoExecutorRefRaceTest extends ServletCamelRouterTestSupport { | ||
|
|
||
| @Test | ||
| public void testAsyncRouteCompletesBeforeResponse() throws Exception { | ||
| WebRequest req = new GetMethodWebRequest(contextUrl + "/services/racy"); | ||
| WebResponse response = query(req, false); | ||
|
|
||
| assertEquals(200, response.getResponseCode()); | ||
| assertEquals("delayed-response", response.getText()); | ||
| } | ||
|
|
||
| @Override | ||
| protected DeploymentInfo getDeploymentInfo() { | ||
| return Servlets.deployment() | ||
| .setClassLoader(getClass().getClassLoader()) | ||
| .setContextPath(CONTEXT) | ||
| .setDeploymentName(getClass().getName()) | ||
| .addInitParameter("contextConfigLocation", | ||
| "classpath:org/apache/camel/component/servlet/example-camelContext-race.xml") | ||
| .addListener(Servlets.listener(ContextLoaderListener.class)) | ||
| .addServlet(Servlets.servlet("CamelServlet", CamelHttpTransportServlet.class) | ||
| .addInitParam("async", "true") | ||
| .setLoadOnStartup(1) | ||
| .setAsyncSupported(true) | ||
| .addMapping("/services/*")); | ||
| } | ||
|
|
||
| } | ||
44 changes: 44 additions & 0 deletions
44
...rvlet/src/test/resources/org/apache/camel/component/servlet/example-camelContext-race.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
|
|
||
| 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. | ||
|
|
||
| --> | ||
| <beans xmlns="http://www.springframework.org/schema/beans" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xmlns:camel="http://camel.apache.org/schema/spring" | ||
| xsi:schemaLocation=" | ||
| http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd | ||
| http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd | ||
| "> | ||
|
|
||
| <camelContext id="camel" streamCache="true" xmlns="http://camel.apache.org/schema/spring" > | ||
| <route id="raceRoute"> | ||
| <!-- incoming requests from the servlet is routed --> | ||
| <from uri="servlet:racy"/> | ||
| <!-- genuinely asynchronous: resumes on the delayer's scheduled-executor thread, | ||
| simulating a downstream call (e.g. CXF SOAP client) that completes on a | ||
| background thread instead of the calling thread --> | ||
| <delay asyncDelayed="true"> | ||
| <constant>300</constant> | ||
| </delay> | ||
| <transform> | ||
| <simple>delayed-response</simple> | ||
| </transform> | ||
| </route> | ||
| </camelContext> | ||
|
|
||
| </beans> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.