You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by cs...@apache.org on 2024/04/21 16:44:26 UTC

(activemq) 01/01: AMQ-9481 - Correctly complete async servlet request on timeout

This is an automated email from the ASF dual-hosted git repository.

cshannon pushed a commit to branch AMQ-9841
in repository https://gitbox.apache.org/repos/asf/activemq.git

commit 72befc14fbb69c24bdec0c7d4a1002da8874380d
Author: Christopher L. Shannon <cs...@apache.org>
AuthorDate: Sun Apr 21 12:42:20 2024 -0400

    AMQ-9481 - Correctly complete async servlet request on timeout
    
    This fixes AsyncServletRequest to correctly call context.dispatch() when
    the async request times out so that the consumer can be re-used.
---
 .../src/test/java/org/apache/activemq/web/RestTest.java | 17 +++++++++++++----
 .../apache/activemq/web/async/AsyncServletRequest.java  |  8 ++++----
 2 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/activemq-web-demo/src/test/java/org/apache/activemq/web/RestTest.java b/activemq-web-demo/src/test/java/org/apache/activemq/web/RestTest.java
index 24f95d0f5..23c096292 100644
--- a/activemq-web-demo/src/test/java/org/apache/activemq/web/RestTest.java
+++ b/activemq-web-demo/src/test/java/org/apache/activemq/web/RestTest.java
@@ -86,11 +86,20 @@ public class RestTest extends JettyTestSupport {
         HttpClient httpClient = new HttpClient();
         httpClient.start();
 
-        final StringBuffer buf = new StringBuffer();
-        final Future<Result> result =
-            asyncRequest(httpClient, "http://localhost:" + port + "/message/test?readTimeout=1000&type=queue", buf);
+        // AMQ-9330 - test no 500 error on timeout and instead 204 error
+        Future<Result> result =
+            asyncRequest(httpClient, "http://localhost:" + port + "/message/test?readTimeout=2000&type=queue&clientId=test", new StringBuffer());
+        // try a second request while the first is running, this should get a 500 error since the first is still running and
+        // concurrent access to the same consumer is not allowed
+        Future<Result> errorResult = asyncRequest(httpClient, "http://localhost:" + port + "/message/test?readTimeout=1&type=queue&clientId=test", new StringBuffer());
+        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, errorResult.get().getResponse().getStatus());
+        //After the original request finishes, verify 204 and not 500 error
+        assertEquals(HttpStatus.NO_CONTENT_204, result.get().getResponse().getStatus());
 
-        //Test timeout, no message was sent
+        // AMQ-9481 - test to make sure we can re-use the consumer after timeout by trying again and ensuring
+        // no 500 error. Before the fix in AMQ-9418 this would fail even after the previous request timed out
+        result =
+            asyncRequest(httpClient, "http://localhost:" + port + "/message/test?readTimeout=1000&type=queue&clientId=test", new StringBuffer());
         assertEquals(HttpStatus.NO_CONTENT_204, result.get().getResponse().getStatus());
     }
 
diff --git a/activemq-web/src/main/java/org/apache/activemq/web/async/AsyncServletRequest.java b/activemq-web/src/main/java/org/apache/activemq/web/async/AsyncServletRequest.java
index 9ff5d3c04..4ba158b45 100644
--- a/activemq-web/src/main/java/org/apache/activemq/web/async/AsyncServletRequest.java
+++ b/activemq-web/src/main/java/org/apache/activemq/web/async/AsyncServletRequest.java
@@ -115,10 +115,10 @@ public class AsyncServletRequest implements AsyncListener  {
         }
 
         final AsyncContext context = event.getAsyncContext();
-        if (context != null) {
-            // We must call complete and then set the status code to prevent a 500
-            // error. The spec requires a 500 error on timeout unless complete() is called.
-            context.complete();
+        if (context != null && event.getSuppliedRequest().isAsyncStarted()) {
+            // We must call dispatch to finish the request on timeout.
+            // then set the status code to prevent a 500 error.
+            context.dispatch();
             final ServletResponse response = context.getResponse();
             if (response instanceof HttpServletResponse) {
                 ((HttpServletResponse) response).setStatus(HttpServletResponse.SC_NO_CONTENT);