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

thiagohp pushed a commit to branch async
in repository https://gitbox.apache.org/repos/asf/tapestry-5.git


The following commit(s) were added to refs/heads/async by this push:
     new d7cc17d  TAP5-2698: Adding support for async timeout
d7cc17d is described below

commit d7cc17d1689e68d3c14c5f64f2f0df40a299b2a2
Author: Thiago H. de Paula Figueiredo <[email protected]>
AuthorDate: Sat Dec 11 23:15:40 2021 -0300

    TAP5-2698: Adding support for async timeout
---
 tapestry-core/src/test/app1/WEB-INF/web.xml        | 12 +++++-----
 .../http/AsyncRequestHandlerResponse.java          | 26 ++++++++++++++++++++++
 .../org/apache/tapestry5/http/TapestryFilter.java  |  9 +++++++-
 3 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/tapestry-core/src/test/app1/WEB-INF/web.xml 
b/tapestry-core/src/test/app1/WEB-INF/web.xml
index bd71436..c4c2489 100644
--- a/tapestry-core/src/test/app1/WEB-INF/web.xml
+++ b/tapestry-core/src/test/app1/WEB-INF/web.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!-- 
-   Copyright 2006, 2010 The Apache Software Foundation
+   Copyright 2006, 2010, 2021 The Apache Software Foundation
 
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
@@ -14,11 +14,10 @@
    See the License for the specific language governing permissions and
    limitations under the License.
 -->
-
-<!DOCTYPE web-app
-        PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
-        "http://java.sun.com/dtd/web-app_2_3.dtd";>
-<web-app>
+<web-app xmlns="http://java.sun.com/xml/ns/javaee";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd";
+       version="3.0">
     <display-name>Integration Test App 1</display-name>
     <context-param>
         <param-name>tapestry.app-package</param-name>
@@ -35,6 +34,7 @@
     <filter>
         <filter-name>app</filter-name>
         <filter-class>org.apache.tapestry5.TapestryFilter</filter-class>
+        <async-supported>true</async-supported>
     </filter>
     <filter-mapping>
         <filter-name>app</filter-name>
diff --git 
a/tapestry-http/src/main/java/org/apache/tapestry5/http/AsyncRequestHandlerResponse.java
 
b/tapestry-http/src/main/java/org/apache/tapestry5/http/AsyncRequestHandlerResponse.java
index 9bd3040..aec82c1 100644
--- 
a/tapestry-http/src/main/java/org/apache/tapestry5/http/AsyncRequestHandlerResponse.java
+++ 
b/tapestry-http/src/main/java/org/apache/tapestry5/http/AsyncRequestHandlerResponse.java
@@ -39,6 +39,8 @@ public class AsyncRequestHandlerResponse
     
     private AsyncListener listener;
     
+    private long timeout;
+    
     /**
      * Creates an instance with a given {@link Executor}. It cannot be null.
      * If you want an instance with a non-async response, use {@link 
#notHandled()} instead.
@@ -86,6 +88,15 @@ public class AsyncRequestHandlerResponse
     }
     
     /**
+     * Sets the timeout for this asynchronous request in milliseconds.
+     */
+    public AsyncRequestHandlerResponse withTimeout(long timeout)
+    {
+        this.timeout = timeout;
+        return this;
+    }
+    
+    /**
      * Returns a response saying this {@linkplain AsyncRequestHandler} doesn't 
handle this request.
      * @return an {@link AsyncRequestHandlerResponse}.
      */
@@ -141,5 +152,20 @@ public class AsyncRequestHandlerResponse
     {
         return request != null && response != null;
     }
+
+    /**
+     * Returns the timeout, in milliseconds, for the asynchronous request. Any 
value
+     * less than or equal zero is considered not having set a timeout.
+     */
+    public long getTimeout() {
+        return timeout;
+    }
+
+    @Override
+    public String toString() 
+    {
+        return "AsyncRequestHandlerResponse [async=" + async + ", executor=" + 
executor + ", request=" + request + ", response=" + response + ", listener="
+                + listener + "]";
+    }
     
 }
diff --git 
a/tapestry-http/src/main/java/org/apache/tapestry5/http/TapestryFilter.java 
b/tapestry-http/src/main/java/org/apache/tapestry5/http/TapestryFilter.java
index 5be696a..3fdb98d 100644
--- a/tapestry-http/src/main/java/org/apache/tapestry5/http/TapestryFilter.java
+++ b/tapestry-http/src/main/java/org/apache/tapestry5/http/TapestryFilter.java
@@ -210,8 +210,15 @@ public class TapestryFilter implements Filter
             {
                 asyncContext.addListener(handlerResponse.getListener());
             }
+            if (handlerResponse.getTimeout() > 0)
+            {
+                asyncContext.setTimeout(handlerResponse.getTimeout());
+            }
             handlerResponse.getExecutor().execute(
-                    new ExceptionCatchingRunnable(() -> runFilter(request, 
response, chain)));
+                    new ExceptionCatchingRunnable(() -> {
+                        runFilter(request, response, chain);
+                        asyncContext.complete();
+                    }));
         }
         else
         {

Reply via email to