11package org .javaee7 .servlet .async ;
22
3- import java .io .IOException ;
4- import java .io .PrintWriter ;
53import javax .annotation .Resource ;
64import javax .enterprise .concurrent .ManagedExecutorService ;
75import javax .servlet .AsyncContext ;
1210import javax .servlet .http .HttpServlet ;
1311import javax .servlet .http .HttpServletRequest ;
1412import javax .servlet .http .HttpServletResponse ;
13+ import java .io .IOException ;
1514
1615/**
1716 * @author Arun Gupta
@@ -34,43 +33,31 @@ public class MyAsyncServlet extends HttpServlet {
3433 */
3534 protected void processRequest (HttpServletRequest request , HttpServletResponse response )
3635 throws ServletException , IOException {
37- response .setContentType ("text/html;charset=UTF-8" );
38- try (final PrintWriter out = response .getWriter ()) {
39- out .println ("<!DOCTYPE html>" );
40- out .println ("<html>" );
41- out .println ("<head>" );
42- out .println ("<title>Async Servlet</title>" );
43- out .println ("</head>" );
44- out .println ("<body>" );
45- out .println ("<h1>Async Servlet</h1>" );
46- AsyncContext ac = request .startAsync ();
36+ AsyncContext ac = request .startAsync ();
4737
48- ac .addListener (new AsyncListener () {
49- @ Override
50- public void onComplete (AsyncEvent event ) throws IOException {
51- System . out .println ("onComplete" );
52- }
38+ ac .addListener (new AsyncListener () {
39+ @ Override
40+ public void onComplete (AsyncEvent event ) throws IOException {
41+ event . getSuppliedResponse (). getWriter () .println ("onComplete" );
42+ }
5343
54- @ Override
55- public void onTimeout (AsyncEvent event ) throws IOException {
56- System .out .println ("onTimeout" );
57- }
44+ @ Override
45+ public void onTimeout (AsyncEvent event ) throws IOException {
46+ event .getSuppliedResponse ().getWriter ().println ("onTimeout" );
47+ event .getAsyncContext ().complete ();
48+ }
5849
59- @ Override
60- public void onError (AsyncEvent event ) throws IOException {
61- System . out .println ("onError" );
62- }
50+ @ Override
51+ public void onError (AsyncEvent event ) throws IOException {
52+ event . getSuppliedResponse (). getWriter () .println ("onError" );
53+ }
6354
64- @ Override
65- public void onStartAsync (AsyncEvent event ) throws IOException {
66- System .out .println ("onStartAsync" );
67- }
68- });
69- executor .submit (new MyAsyncService (ac ));
70- out .println ("Check \" server.log\" for output from Async Servlet" );
71- out .println ("</body>" );
72- out .println ("</html>" );
73- }
55+ @ Override
56+ public void onStartAsync (AsyncEvent event ) throws IOException {
57+ event .getSuppliedResponse ().getWriter ().println ("onStartAsync" );
58+ }
59+ });
60+ executor .submit (new MyAsyncService (ac ));
7461 }
7562
7663 class MyAsyncService implements Runnable {
@@ -83,7 +70,11 @@ public MyAsyncService(AsyncContext ac) {
8370
8471 @ Override
8572 public void run () {
86- System .out .println ("Running inside MyAsyncService" );
73+ try {
74+ ac .getResponse ().getWriter ().println ("Running inside MyAsyncService" );
75+ } catch (IOException e ) {
76+ throw new IllegalStateException (e );
77+ }
8778 ac .complete ();
8879 }
8980 }
0 commit comments