01 package examples.webapp.servlet.async;
02
03 import weblogic.servlet.http.AbstractAsyncServlet;
04 import weblogic.servlet.http.RequestResponseKey;
05
06 import javax.servlet.ServletException;
07 import java.io.IOException;
08
09 /**
10 * @author Copyright (c) Apr 26, 2006 by BEA Systems, Inc. All Rights Reserved.
11 */
12
13 /**
14 * An implementation of AbstractAsyncServlet handling client request in such way:
15 * <pre>
16 * The request will handled right now when there are queuing message;
17 * if there are no messages, the request will be handled in within a timeout period;
18 * if no new message arriving within the timeout period, a retry message will send to the client.
19 * </pre>
20 */
21 public class ChatReceiveServlet extends AbstractAsyncServlet {
22 private ChatSession chat_;
23
24 public void init() {
25 this.chat_ = ChatSession.getOrCreateChatSession(getServletContext());
26 /*
27 * Sets the interval period in milliseconds at which requests will
28 * be checked for timeouts.
29 */
30 AbstractAsyncServlet.setScavangeInterval(5 * 1000);
31 setTimeout(15 * 1000);
32 }
33
34 /**
35 * Handle the request right now if there are queuing messages;
36 * otherwise handle it in the future.
37 *
38 * @param rrk the RequestResponseKey, containing the servlet request
39 * and response
40 * @return true if the response will be handled in the future.
41 * @throws IOException - if an input or output error occurs
42 * @throws ServletException - if the HTTP request cannot be handled
43 */
44
45 public boolean doRequest(RequestResponseKey rrk)
46 throws IOException, ServletException {
47 print("ChatReceiveServlet.doRequest " + rrk);
48 rrk.getResponse().setContentType("text/xml");
49 rrk.getResponse().setHeader("Cache-Control", "no-cache");
50 return chat_.registerForMessages(rrk);
51 }
52
53 /**
54 * Invoked when new message arrived within the timeout period.
55 *
56 * @param rrk - the RequestResponseKey, containing the servlet request
57 * and response
58 * @throws IOException - if an input or output error occurs
59 * @throws ServletException - if the HTTP request cannot be handled
60 */
61 public void doResponse(RequestResponseKey rrk, Object msg)
62 throws IOException, ServletException {
63 print("ChatReceiveServlet.doResponse " + rrk);
64 StringBuffer sb = new StringBuffer(ChatSession.XML_PROLOGUE);
65 sb.append("<messages>").append((String) msg).append("</messages>");
66 rrk.getResponse().getWriter().write(sb.toString());
67 print("doResponse called..." + sb.toString());
68 }
69
70 /**
71 * Sending a retry messsage to the client when the request is timed-out.
72 *
73 * @param rrk - the RequestResponseKey, containing the servlet request and response
74 * @throws IOException - if an input or output error occurs
75 * @throws ServletException - if the HTTP request cannot be handled
76 */
77 public void doTimeout(RequestResponseKey rrk)
78 throws IOException, ServletException {
79 print("ChatReceiveServlet.doTimeout " + rrk);
80 chat_.handleTimeout(rrk);
81 }
82
83 private void print(String message) {
84 System.out.println(message);
85
86 }
87 }
|