very simple async example
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 17 Jul 2009 21:14:13 +0000 (21:14 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 17 Jul 2009 21:14:13 +0000 (21:14 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@795229 13f79535-47bb-0310-9956-ffa450edef68

webapps/examples/WEB-INF/classes/async/Async1.java [new file with mode: 0644]
webapps/examples/WEB-INF/web.xml
webapps/examples/jsp/async/async1.jsp [new file with mode: 0644]
webapps/examples/jsp/async/index.jsp [new file with mode: 0644]

diff --git a/webapps/examples/WEB-INF/classes/async/Async1.java b/webapps/examples/WEB-INF/classes/async/Async1.java
new file mode 100644 (file)
index 0000000..d672271
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+* 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 async;
+
+import java.io.IOException;
+
+import javax.servlet.AsyncContext;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.juli.logging.Log;
+import org.apache.juli.logging.LogFactory;
+
+public class Async1 extends HttpServlet {
+    protected static Log log = LogFactory.getLog(Async1.class);
+    public Async1() {
+    }
+
+    @Override
+    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+        final AsyncContext actx = req.startAsync();
+        Runnable run = new Runnable() {
+            public void run() {
+                try {
+                    String path = "/jsp/async/async1.jsp";
+                    Thread.currentThread().setName("Async1-Thread");
+                    log.info("Putting AsyncThread to sleep");
+                    Thread.sleep(10*1000);
+                    log.info("Dispatching to "+path);
+                    actx.dispatch(path);
+                }catch (InterruptedException x) {
+                    log.error("Async1",x);
+                }catch (IllegalStateException x) {
+                    log.error("Async1",x);
+                }
+            }
+        };
+        Thread t = new Thread(run);
+        t.start();
+    }
+    
+
+}
index fb40623..805c38b 100644 (file)
       <env-entry-value>10</env-entry-value>
     </env-entry>
 
+    <!-- Async examples -->
+    <servlet>
+      <servlet-name>async1</servlet-name>
+      <servlet-class>async.Async1</servlet-class>
+    </servlet>
+    <servlet-mapping>
+      <servlet-name>async1</servlet-name>
+      <url-pattern>/async/async1</url-pattern>
+    </servlet-mapping>
 </web-app>
diff --git a/webapps/examples/jsp/async/async1.jsp b/webapps/examples/jsp/async/async1.jsp
new file mode 100644 (file)
index 0000000..2555765
--- /dev/null
@@ -0,0 +1,8 @@
+<%@page session="false"%>
+Output from async1.jsp
+<%
+System.out.println("Inside Async 1");
+  if (request.isAsyncStarted()) {
+    request.getAsyncContext().complete();
+  }
+%>
diff --git a/webapps/examples/jsp/async/index.jsp b/webapps/examples/jsp/async/index.jsp
new file mode 100644 (file)
index 0000000..b880a71
--- /dev/null
@@ -0,0 +1,2 @@
+<%@page session="false"%>
+<a href="<%=response.encodeURL("/examples/async/async1")%>"> Async 1 </a>