Exemple #1
0
  /**
   * this is the main method of the servlet that will service all get requests.
   *
   * @param request HttpServletRequest
   * @param responce HttpServletResponce
   */
  public void doGet(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
    try {
      res.setContentType("text/html");

      // The following 2 lines are the difference between PingServlet and PingServletWriter
      //   the latter uses a PrintWriter for output versus a binary output stream.
      ServletOutputStream out = res.getOutputStream();
      // java.io.PrintWriter out = res.getWriter();
      hitCount++;
      out.println(
          "<html><head><title>Ping Servlet</title></head>"
              + "<body><HR><BR><FONT size=\"+2\" color=\"#000066\">Ping Servlet<BR></FONT><FONT size=\"+1\" color=\"#000066\">Init time : "
              + initTime
              + "<BR><BR></FONT>  <B>Hit Count: "
              + hitCount
              + "</B></body></html>");
    } catch (Exception e) {
      Log.error(e, "PingServlet.doGet(...): general exception caught");
      res.sendError(500, e.toString());
    }
  }
  public void doGet(HttpServletRequest req, HttpServletResponse res)
      throws IOException, ServletException {

    res.setContentType("text/html");
    java.io.PrintWriter out = res.getWriter();
    // use a stringbuffer to avoid concatenation of Strings
    StringBuffer output = new StringBuffer(100);
    output.append(
        "<html><head><title>PingServlet2MDBQueue</title></head>"
            + "<body><HR><FONT size=\"+2\" color=\"#000066\">PingServlet2MDBQueue<BR></FONT>"
            + "<FONT size=\"-1\" color=\"#000066\">"
            + "Tests the basic operation of a servlet posting a message to an EJB MDB through a JMS Queue.<BR>"
            + "<FONT color=\"red\"><B>Note:</B> Not intended for performance testing.</FONT>");

    try {
      if (queueConnectionFactory == null) {
        queueConnectionFactory =
            (ConnectionFactory) new InitialContext().lookup("ConnectionFactory");
      }
      Connection conn = queueConnectionFactory.createConnection();

      if (tradeBrokerQueue == null) {
        tradeBrokerQueue =
            (Queue) new InitialContext().lookup("java:comp/env/jms/TradeBrokerQueue");
      }

      try {
        TextMessage message = null;
        int iter = TradeConfig.getPrimIterations();
        for (int ii = 0; ii < iter; ii++) {
          Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
          try {
            MessageProducer producer = sess.createProducer(tradeBrokerQueue);

            message = sess.createTextMessage();

            String command = "ping";
            message.setStringProperty("command", command);
            message.setLongProperty("publishTime", System.currentTimeMillis());
            message.setText(
                "Ping message for queue java:comp/env/jms/TradeBrokerQueue sent from PingServlet2MDBQueue at "
                    + new java.util.Date());
            producer.send(message);
          } finally {
            sess.close();
          }
        }

        // write out the output
        output.append("<HR>initTime: ").append(initTime);
        output.append("<BR>Hit Count: ").append(hitCount++);
        output.append("<HR>Posted Text message to java:comp/env/jms/TradeBrokerQueue destination");
        output.append("<BR>Message: ").append(message);
        output.append("<BR><BR>Message text: ").append(message.getText());
        output.append("<BR><HR></FONT></BODY></HTML>");
        out.println(output.toString());

      } catch (Exception e) {
        Log.error(
            "PingServlet2MDBQueue.doGet(...):exception posting message to TradeBrokerQueue destination ");
        throw e;
      } finally {
        conn.close();
      }
    } // this is where I actually handle the exceptions
    catch (Exception e) {
      Log.error(e, "PingServlet2MDBQueue.doGet(...): error");
      res.sendError(500, "PingServlet2MDBQueue.doGet(...): error, " + e.toString());
    }
  }