Ejemplo n.º 1
0
  private void setupMessageListener()
      throws javax.jms.JMSException, javax.naming.NamingException, Exception {
    InitialContext ctx = new InitialContext();

    QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("jms/taskQueueFactory");
    connection = factory.createQueueConnection();
    session = connection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
    Queue queue = (Queue) ctx.lookup("jms/taskQueue");

    receiver = session.createConsumer(queue);
    connection.start();
    ctx.close();
  }
Ejemplo n.º 2
0
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println("<html><head></head><body>");

    try {
      // Gather necessary JMS resources
      Context ctx = new InitialContext();
      ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/myConnectionFactory");
      Connection con = cf.createConnection();
      con.start(); // don't forget to start the connection
      QueueSession session = (QueueSession) con.createSession(false, Session.AUTO_ACKNOWLEDGE);

      // The PITsnapshot Queue is used for responses from the Players to this serverlet
      Queue q = (Queue) ctx.lookup("jms/PITsnapshot");
      MessageConsumer reader = session.createConsumer(q);

      /*
       * Throw out old PITsnapshot messages that may have been left from past
       * snapshots that did not complete (because of some error).
       */
      ObjectMessage m = null;
      while ((m = (ObjectMessage) reader.receiveNoWait()) != null) {
        System.out.println("Found an orphaned PITsnapshot message");
      }

      // Initialize the snapshot my sending a marker to a Player
      sendInitSnapshot();

      /*
       * Receive the snapshot messages from all Players.
       * Each snapshot is a HahsMap.  Put them into an array of HashMaps             *
       */
      HashMap state[] = new HashMap[numPlayers + 1];
      int stateResponses = 0;
      int failures = 0;
      while (stateResponses < numPlayers) {
        if ((m = (ObjectMessage) reader.receive(1000)) == null) {
          if (++failures > 10) {
            System.out.println("Not all players reported, giving up after " + stateResponses);
            break;
          }
          continue;
        }
        state[stateResponses++] = (HashMap) m.getObject();
      }

      /*
       * For each commodity, sum the number of them reported from
       * each Player.  Store these into a two dimensional table
       * that will then be used to generate the report back to the user.
       */
      String commodity[] = {"rice", "gold", "oil"};
      int total[][] = new int[numPlayers][commodity.length];
      for (int c = 0; c < commodity.length; c++) {
        for (int p = 0; p < stateResponses; p++) {
          try {
            Integer ccount = (Integer) state[p].get(commodity[c]);
            if (ccount == null) {
              total[p][c] = 0;
            } else {
              total[p][c] = (Integer) ccount.intValue();
            }
          } catch (Exception e) {
            System.out.println("Servlet threw exception " + e);
          }
        }
      }

      /*
       * Now turn the table of commodities, and the state from each Player
       * into a response to the user.
       */
      for (int c = 0; c < commodity.length; c++) {
        int ctotal = 0;
        out.print("<h2>Commodity: " + commodity[c] + "</h2>");
        out.print("<table border='1'><tr><th>Player</th><th>Quantity</th></tr>");
        for (int p = 0; p < stateResponses; p++) {
          out.print("<tr><td>" + p + "</td><td>" + total[p][c] + "</td></tr>");
          ctotal += total[p][c];
        }
        out.print("<tr><td><b>Total</b></td><td>" + ctotal + "</td></tr>");
        out.print("</table></br></br>");
      }

      // Close the connection
      con.close();

      out.println("</BODY></HTML>");

    } catch (Exception e) {
      System.out.println("Servlet threw exception " + e);
    } finally {
      out.println("</body></html>");
      out.close();
    }
  }