1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.acme.test;
20
21 import java.io.IOException;
22 import java.io.PrintWriter;
23
24 import javax.annotation.PostConstruct;
25 import javax.annotation.Resource;
26 import javax.servlet.AsyncContext;
27 import javax.servlet.AsyncEvent;
28 import javax.servlet.AsyncListener;
29 import javax.servlet.ServletException;
30 import javax.servlet.annotation.WebServlet;
31 import javax.servlet.http.HttpServlet;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 @WebServlet(urlPatterns="/asy/*", asyncSupported=true)
36 public class AsyncListenerServlet extends HttpServlet
37 {
38 public static class MyAsyncListener implements AsyncListener
39 {
40 @Resource(mappedName="maxAmount")
41 private Double maxAmount;
42
43 boolean postConstructCalled = false;
44 boolean resourceInjected = false;
45
46 @PostConstruct
47 public void postConstruct()
48 {
49 postConstructCalled = true;
50 resourceInjected = (maxAmount != null);
51 }
52
53 public boolean isPostConstructCalled()
54 {
55 return postConstructCalled;
56 }
57
58 public boolean isResourceInjected()
59 {
60 return resourceInjected;
61 }
62
63 @Override
64 public void onComplete(AsyncEvent event) throws IOException
65 {
66
67
68 }
69
70 @Override
71 public void onTimeout(AsyncEvent event) throws IOException
72 {
73
74
75 }
76
77 @Override
78 public void onError(AsyncEvent event) throws IOException
79 {
80
81
82 }
83
84 @Override
85 public void onStartAsync(AsyncEvent event) throws IOException
86 {
87
88
89 }
90 }
91
92
93 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
94 throws ServletException, IOException
95 {
96 AsyncContext asyncContext = req.startAsync();
97 MyAsyncListener listener = asyncContext.createListener(MyAsyncListener.class);
98
99 PrintWriter writer = resp.getWriter();
100 writer.println( "<html>");
101 writer.println("<HEAD><link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\"/></HEAD>");
102 writer.println( "<body>");
103 writer.println("<h1>AsyncListener</h2>");
104 writer.println("<pre>");
105 writer.println("<h2>@PostConstruct Callback</h2>");
106 writer.println("<pre>");
107 writer.println("@PostConstruct");
108 writer.println("private void postConstruct ()");
109 writer.println("{}");
110 writer.println("</pre>");
111 writer.println("<br/><b>Result: "+(listener.isPostConstructCalled()?"<span class=\"pass\">PASS</span>":"<span class=\"fail\">FAIL</span>")+"</b>");
112
113 writer.println("<h2>@Resource Injection for env-entry </h2>");
114 writer.println("<pre>");
115 writer.println("@Resource(mappedName=\"maxAmount\")");
116 writer.println("private Double maxAmount;");
117 writer.println("</pre>");
118 writer.println("<br/><b>Result: "+(listener.isResourceInjected()?" <span class=\"pass\">PASS</span>":" <span class=\"FAIL\">FAIL</span>")+"</b>");
119
120 writer.println( "</body>");
121 writer.println( "</html>");
122 writer.flush();
123 writer.close();
124
125 asyncContext.complete();
126 }
127 }