Пример #1
0
  /**
   * Receive a message from destination with timeout.
   *
   * @param destinationName destinationName
   * @param timeout timeout
   * @return message
   */
  public String receiveTextMessageFromDestinationWithTimeout(
      final String destinationName, final int timeout) {

    if (!this.isConnected()) {
      throw new JmsNotConnectedException("Not connected");
    }
    MessageConsumer consumer = getConsumer(destinationName);
    TextMessage message;
    try {
      if (timeout == 0) {
        message = (TextMessage) consumer.receiveNoWait();
      } else {
        message = (TextMessage) consumer.receive(timeout);
      }
      if (message != null) {

        if (acknowledgeMode == Session.CLIENT_ACKNOWLEDGE) {
          message.acknowledge();
        }
        return message.getText();
      } else {
        return null;
      }
    } catch (JMSException e) {
      throw new IllegalStateException("Unable to receive message from " + destinationName, e);
    }
  }
Пример #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();
    }
  }