01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.test.timer.servlet;
23:
24: import java.io.IOException;
25: import java.io.PrintWriter;
26: import java.util.Date;
27: import javax.servlet.http.HttpServlet;
28: import javax.servlet.http.HttpServletRequest;
29: import javax.servlet.http.HttpServletResponse;
30: import javax.servlet.ServletConfig;
31: import javax.servlet.ServletException;
32: import javax.naming.InitialContext;
33:
34: import org.jboss.test.timer.interfaces.TimerSLSBHome;
35: import org.jboss.test.timer.interfaces.TimerSLSB;
36: import org.jboss.logging.Logger;
37:
38: /** A servlet that creates an ejb timer in its init method to test that timer
39: * restoration on receipt of the server startup event does not try to create
40: * a duplicate timer from this one.
41: *
42: * @author Scott.Stark@jboss.org
43: * @version $Revision: 57211 $
44: */
45: public class InitTimerServlet extends HttpServlet {
46: private static Logger log = Logger
47: .getLogger(InitTimerServlet.class);
48: private static byte[] handle;
49:
50: /**
51: * Start an ejb timer from within the init method.
52: *
53: * @param servletConfig
54: * @throws ServletException
55: */
56: public void init(ServletConfig servletConfig)
57: throws ServletException {
58: super .init(servletConfig);
59: log.info("init, creating ejb timer");
60: //
61: try {
62: InitialContext ctx = new InitialContext();
63: TimerSLSBHome home = (TimerSLSBHome) ctx
64: .lookup("java:/comp/env/ejb/TimerSLSBHome");
65: TimerSLSB bean = home.create();
66: handle = bean.startTimer(60000);
67: } catch (Exception e) {
68: throw new ServletException(e);
69: }
70: }
71:
72: protected void doGet(HttpServletRequest request,
73: HttpServletResponse response) throws ServletException,
74: IOException {
75: try {
76: InitialContext ctx = new InitialContext();
77: TimerSLSBHome home = (TimerSLSBHome) ctx
78: .lookup("java:/comp/env/ejb/TimerSLSBHome");
79: TimerSLSB bean = home.create();
80: int timeoutCount = bean.getTimeoutCount(handle);
81: Date nextTimeout = bean.getNextTimeout(handle);
82: long timeRemaining = bean.getTimeRemaining(handle);
83: PrintWriter pw = response.getWriter();
84: pw
85: .println("<html><head><title>InitTimerServlet</title></head><body>");
86: pw.println("<h1>Timer Info</h1>");
87: pw.println("TimeoutCount:" + timeoutCount);
88: pw.println("<br>NextTimeout:" + nextTimeout);
89: pw.println("<br>TimeRemaining:" + timeRemaining);
90: pw.println("</body></html>");
91: } catch (Exception e) {
92: throw new ServletException(e);
93: }
94: }
95: }
|