Author: jfrederic.clere(a)jboss.com
Date: 2010-01-26 17:30:01 -0500 (Tue, 26 Jan 2010)
New Revision: 1373
Added:
sandbox/webapps/src/TestAsyncServlet.java
Modified:
sandbox/webapps/build.xml
Log:
Add AsyncServlet example.
Modified: sandbox/webapps/build.xml
===================================================================
--- sandbox/webapps/build.xml 2010-01-26 14:38:17 UTC (rev 1372)
+++ sandbox/webapps/build.xml 2010-01-26 22:30:01 UTC (rev 1373)
@@ -5,11 +5,11 @@
<!--
<javac srcdir="src"
classpath="/home/jfclere/tc6.0.x/output/build/lib/servlet-api.jar"
destdir="classes" />
-->
- <javac srcdir="src" debug="on"
debuglevel="lines,vars,source"
classpath="/home/jfclere/apache-tomcat-5.5.17/common/lib/servlet-api.jar"
destdir="classes" excludes="Comet*,MyServletContextListener*" />
+ <javac srcdir="src" debug="on"
debuglevel="lines,vars,source"
classpath="/home/jfclere/apache-tomcat-5.5.17/common/lib/servlet-api.jar"
destdir="classes"
excludes="Comet*,MyServletContextListener*,TestAsync*" />
- <javac srcdir="src" debug="on"
debuglevel="lines,vars,source"
classpath="/home/jfclere/jbossweb_trunk/output/jars/jbossweb.jar:/home/jfclere/jbossweb_trunk/output/jars/servlet-api.jar"
destdir="classes" includes="Comet*,MyServletContextListener*" />
+ <javac srcdir="src" debug="on"
debuglevel="lines,vars,source"
classpath="/home/jfclere/jbossweb_trunk/output/jars/jbossweb.jar:/home/jfclere/jbossweb_trunk/output/jars/servlet-api.jar"
destdir="classes"
includes="Comet*,MyServletContextListener*,TestAsync*" />
- <jar destfile="./lib/myservlets.jar" basedir="./classes"
excludes="Comet*,MyServletContextListener*" />
+ <jar destfile="./lib/myservlets.jar" basedir="./classes"
excludes="Comet*,MyServletContextListener*,TestAsync*" />
<war destfile="myapp.war" webxml="myapp.xml">
<fileset dir="html" />
Added: sandbox/webapps/src/TestAsyncServlet.java
===================================================================
--- sandbox/webapps/src/TestAsyncServlet.java (rev 0)
+++ sandbox/webapps/src/TestAsyncServlet.java 2010-01-26 22:30:01 UTC (rev 1373)
@@ -0,0 +1,79 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+
+// package org.jboss.web.comet;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.servlet.annotation.WebServlet;
+
+import javax.servlet.ServletException;
+import javax.servlet.ServletInputStream;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpSession;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import javax.servlet.AsyncListener;
+import javax.servlet.AsyncContext;
+import javax.servlet.AsyncEvent;
+
+/* Asynchronous example */
+// @WebServlet("/TestAsyncServlet")
+@WebServlet(urlPatterns = {"/TestAsyncServlet"}, asyncSupported = true)
+public class TestAsyncServlet extends HttpServlet {
+ public void doPost(HttpServletRequest req, HttpServletResponse res) {
+ // Servlet Code
+ // ...........
+ // Call startAsync
+ AsyncContext context = req.startAsync();
+ // Give AsyncContext to the Listener MonListener
+ context.addListener(new MonListener());
+ // ...........
+ }
+ public class MonListener implements AsyncListener {
+
+ public void onComplete(AsyncEvent event) {
+ System.out.println("onComplete");
+ }
+ public void onError(AsyncEvent event) {
+ ServletResponse res = event.getSuppliedResponse();
+ System.out.println("onError: " + res);
+ }
+ public void onTimeout(AsyncEvent event) {
+ ServletResponse res = event.getSuppliedResponse();
+ try {
+ ServletOutputStream os = res.getOutputStream();
+ System.out.println("onTimeout: " + res);
+ } catch (Exception e) {
+ }
+ }
+ public void onStartAsync(AsyncEvent event) {
+ System.out.println("onStartAsync");
+ }
+ }
+
+}