From 87e39e79c4c8562bc09dd0c0acc4ddd5a27df416 Mon Sep 17 00:00:00 2001 From: fhanik Date: Fri, 17 Jul 2009 21:14:13 +0000 Subject: [PATCH] very simple async example 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 | 59 ++++++++++++++++++++++ webapps/examples/WEB-INF/web.xml | 9 ++++ webapps/examples/jsp/async/async1.jsp | 8 +++ webapps/examples/jsp/async/index.jsp | 2 + 4 files changed, 78 insertions(+) create mode 100644 webapps/examples/WEB-INF/classes/async/Async1.java create mode 100644 webapps/examples/jsp/async/async1.jsp create mode 100644 webapps/examples/jsp/async/index.jsp diff --git a/webapps/examples/WEB-INF/classes/async/Async1.java b/webapps/examples/WEB-INF/classes/async/Async1.java new file mode 100644 index 000000000..d6722712b --- /dev/null +++ b/webapps/examples/WEB-INF/classes/async/Async1.java @@ -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(); + } + + +} diff --git a/webapps/examples/WEB-INF/web.xml b/webapps/examples/WEB-INF/web.xml index fb40623df..805c38bdc 100644 --- a/webapps/examples/WEB-INF/web.xml +++ b/webapps/examples/WEB-INF/web.xml @@ -295,4 +295,13 @@ 10 + + + async1 + async.Async1 + + + async1 + /async/async1 + diff --git a/webapps/examples/jsp/async/async1.jsp b/webapps/examples/jsp/async/async1.jsp new file mode 100644 index 000000000..2555765c9 --- /dev/null +++ b/webapps/examples/jsp/async/async1.jsp @@ -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 index 000000000..b880a71e6 --- /dev/null +++ b/webapps/examples/jsp/async/index.jsp @@ -0,0 +1,2 @@ +<%@page session="false"%> +"> Async 1 -- 2.11.0