StatusServlet.java.

Architect's Corner
Designing asynchronous J2EE architectures with JMS
by Satagopan Rajagopalan
Listing 3. StatusServlet.java.


 //Initialize the Queue, Connection and attach to the Message Queue
    public void doGet (HttpServletRequest request,
                  HttpServletResponse response)
    throws ServletException, IOException {


     //Connection to the Message Queue and Registration of Message
     // Listener is done in the init method
     // of the Servlet
        result = getJMSMessage(session.getSessionId());
        if(result!=null){
        try {
            // to be used by the JSP page
            session.setAttribute(“Result”,result);
            response.setContentType(“text/html”);
            RequestDispatcher dispatcher = getServletContext().
                                 getRequestDispatcher(“Results.jsp”);
            dispatcher.include(request, response);
        }
        catch (Exception e) {
              throw e;
        }
       } else {
        out.println(“<HTML><HEAD><meta http-equiv=\”Refresh\”
                                        Content=\”10 ; URL = http://localhost:8080/servlets/StatusServlet><TITLE>”);
        out.println(title);
        out.println(“</TITLE></HEAD><BODY>”);
        out.println(“<H1>” + title + “</H1>”);
        out.println(“<P>Your Application is being processed pls wait
                                                       ...</P>” );
        out.println(“</BODY></HTML>”);
        }
    }

     public String getJMSMessage(String Key) {
        return(String)MessageTable.get(Key);
     }
    public void setJMSMessage(String key,String body) {
      MessageTable.put(key,body);
    }

    /** This method provides the implementation of the
                                     MessageListener interface. */
    public void onMessage(Message message)
    {
        try {
            ObjectMessage objMessage = (ObjectMessage)message;
            System.out.println(“In On message”);
            ResultData data = (ResultData)objMessage.getObject();
            if(data!=null){
                  setJMSMessage(data.getSessionId(),data);
            } else {
                    System.out.println(“error: message contained no
                                                               Result”);
              }
        } catch (Exception e) {
            System.out.println(“onMessage caught “+e);
        }