コード例 #1
0
  /**
   * Receives a message from the given blocking queue, waiting up to the given timeout value.
   *
   * @param queue the queue from which to consume
   * @param timeout greater than 0 to wait the given number of milliseconds, 0 for unlimited
   *     blocking wait, less than zero for no wait
   * @return the message received or null if no message was received
   * @throws JMSException
   */
  protected Message receive(BlockingQueue<byte[]> queue, long timeout) throws JMSException {

    // Figure out the receive strategy.
    ReceiveStrategy receiver = null;
    if (timeout < 0) {
      receiver = new IndefiniteBlockingReceive();
    } else {
      receiver = new TimedBlockingReceive(timeout);
    }

    Message msg = null;
    do {
      synchronized (STOP_GUARD) {
        try {
          byte[] msgData = receiver.receive(queue);

          // Check that we got message data
          if (msgData != null) {

            // Convert the message data back into a JMS message
            msg = messageMarshaller.toMessage(msgData);

            // Switch Bytes messages to read mode
            if (msg instanceof BytesMessage) {
              ((BytesMessage) msg).reset();
            }

            // Check for message expiration
            long expirationTime = msg.getJMSExpiration();

            if (expirationTime != 0 && expirationTime <= System.currentTimeMillis()) {
              log.info(
                  format("Dropping message [%s] because it has expired.", msg.getJMSMessageID()));
              msg = null;
            }
          }
        } catch (InterruptedException ex) {
          throw new JMSException("Error receiving message: " + ex.getMessage());
        } catch (IOException ex) {
          throw new JMSException("Error receiving message: " + ex.getMessage());
        }
      }
    } while (msg == null && receiver.isRetryable());

    return msg;
  }
コード例 #2
0
  public void onMessage(Message message) {
    if (message instanceof TextMessage) {
      TextMessage msg = (TextMessage) message;

      try {
        String unitOfOrder = message.getStringProperty("JMS_BEA_UnitOfOrder");
        // Sleep for 2 seconds to demonstrate the messages are indeed
        // processes sequentially (in unit-of-order)
        Thread.sleep(2000);
        System.out.println(
            "UOOListener:: MDB=["
                + mdbId
                + "] UOO=["
                + unitOfOrder
                + "] Message=["
                + msg.getText()
                + "]");
      } catch (JMSException e) {
        e.printStackTrace();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }