/** use auto acknowledge which automatically has the client say OK */
  public void doReceiveAuto() {

    if (doSetup()) {
      try {
        queueConnection = queueConnectionFactory.createQueueConnection();
        QueueSession queueSession =
            queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        QueueReceiver queueReceiver = queueSession.createReceiver(queue);
        queueConnection.start();
        while (true) {
          Message m = queueReceiver.receive(1);
          if (m != null) {
            if (m instanceof TextMessage) {
              TextMessage message = (TextMessage) m;
              log.debug("Reading message:==> " + message.getText());
            } else {
              break;
            }
          }
        }
      } catch (JMSException e) {
        log.error("Listen Exception occurred: " + e.toString());
      } finally {
        doCleanup();
      }
    }
  }
Beispiel #2
0
 @Test
 public void noMatchingRoutes() throws JMSException {
   String expected = "'no matching route'";
   QueueReceiver qr = messageManager.createQueueReceiver("queue/DLQ");
   clear(qr);
   plainEvent.fire(expected);
   Message m = qr.receive(3000);
   qr.close();
   Assert.assertNull("Unexpectedly received a message", m);
 }
Beispiel #3
0
 public static Message queueReceiver(QueueConnection cnn, String queueName, long timeout)
     throws JMSException {
   QueueSession session = cnn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
   Queue queue = session.createQueue(queueName);
   QueueReceiver receiver = session.createReceiver(queue);
   Message msg = receiver.receive(timeout);
   if (msg != null) msg.acknowledge();
   receiver.close();
   session.close();
   return msg;
 }
Beispiel #4
0
 @Test
 public void forwardSimpleEvent_via_single_route_config() throws JMSException {
   String expected = "'configured via Route'";
   QueueReceiver qr = messageManager.createQueueReceiver("queue/DLQ");
   clear(qr);
   event_viaSingleRouteConfig.fire(expected);
   Message m = qr.receive(3000);
   qr.close();
   Assert.assertTrue(m != null);
   Assert.assertTrue(m instanceof TextMessage);
   Assert.assertEquals(expected, ((TextMessage) m).getText());
 }
Beispiel #5
0
 public static void drainQueue(QueueConnection cnn, String queue) throws Exception {
   QueueSession session = cnn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
   Queue q = session.createQueue(queue);
   QueueReceiver receiver = session.createReceiver(q);
   // Add a delay so that activeMQ can fetch messages from the broker
   // Thread.sleep(5000);
   Message msg = null;
   while ((msg = receiver.receive(1000)) != null) {
     System.out.println("Removing message: " + msg);
     msg.acknowledge();
   }
   receiver.close();
   session.close();
 }
  /**
   * Wait for 'count' messages on controlQueue before continuing. Called by a publisher to make sure
   * that subscribers have started before it begins publishing messages.
   *
   * <p>If controlQueue doesn't exist, the method throws an exception.
   *
   * @param prefix prefix (publisher or subscriber) to be displayed
   * @param controlQueueName name of control queue
   * @param count number of messages to receive
   */
  public static void receiveSynchronizeMessages(String prefix, String controlQueueName, int count)
      throws Exception {
    QueueConnectionFactory queueConnectionFactory = null;
    QueueConnection queueConnection = null;
    QueueSession queueSession = null;
    Queue controlQueue = null;
    QueueReceiver queueReceiver = null;

    try {
      queueConnectionFactory = SampleUtilities.getQueueConnectionFactory();
      queueConnection = queueConnectionFactory.createQueueConnection();
      queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      controlQueue = getQueue(controlQueueName, queueSession);
      queueConnection.start();
    } catch (Exception e) {
      System.out.println("Connection problem: " + e.toString());
      if (queueConnection != null) {
        try {
          queueConnection.close();
        } catch (JMSException ee) {
        }
      }
      throw e;
    }

    try {
      System.out.println(
          prefix
              + "Receiving synchronize messages from "
              + controlQueueName
              + "; count = "
              + count);
      queueReceiver = queueSession.createReceiver(controlQueue);
      while (count > 0) {
        queueReceiver.receive();
        count--;
        System.out.println(prefix + "Received synchronize message; expect " + count + " more");
      }
    } catch (JMSException e) {
      System.out.println("Exception occurred: " + e.toString());
      throw e;
    } finally {
      if (queueConnection != null) {
        try {
          queueConnection.close();
        } catch (JMSException e) {
        }
      }
    }
  }
  public static void main(String[] args) {
    try {
      QueueConnectionFactory queueConnectionFactory;
      QueueConnection queueConnection;
      QueueSession queueSession;
      QueueReceiver queueReceiver;
      Queue queue;
      TextMessage msg;

      // JNDI InitialContextを作成します
      InitialContext ctx = new InitialContext();
      // Connection FactoryとQueueをLook upします
      queueConnectionFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
      queue = (Queue) ctx.lookup(QUEUE);

      // コネクションを作成
      queueConnection = queueConnectionFactory.createQueueConnection();
      // セッションを作成
      queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      // キューレシーバーを作成
      queueReceiver = queueSession.createReceiver(queue);
      // メッセージの配送をスタート
      queueConnection.start();
      // メッセージの受信
      while (true) {
        Message m = queueReceiver.receive(1);
        if (m != null) {
          if (m instanceof TextMessage) {
            msg = (TextMessage) m;
            System.out.println(msg.getText());
          } else {
            break;
          }
        }
      }
      // 接続を切断
      queueReceiver.close();
      queueSession.close();
      queueConnection.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /** manually say OK I'm ready to take the message on the client */
  public void doReceiveAck() {

    if (doSetup()) {
      try {
        queueConnection = queueConnectionFactory.createQueueConnection();
        QueueSession queueSession =
            queueConnection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
        QueueReceiver queueReceiver = queueSession.createReceiver(queue);
        queueConnection.start();
        String info = null;
        while (true) {
          Message m = queueReceiver.receive(1);
          if (m != null) {
            if (m instanceof TextMessage) {
              TextMessage message = (TextMessage) m;
              info = message.getText();

              if (info.indexOf("12") > -1) {
                log.debug("Skipping message:==> " + message.getText());
              } else {
                log.debug("Reading message:==> " + message.getText());
                message.acknowledge();
              }

            } else {
              break;
            }
          }
        }
      } catch (JMSException e) {
        log.error("Listen Exception occurred: " + e.toString());
      } finally {
        doCleanup();
      }
    }
  }