public void testBrokerStatsReset() throws Exception {
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue replyTo = session.createTemporaryQueue();
    MessageConsumer consumer = session.createConsumer(replyTo);
    Queue testQueue = session.createQueue("Test.Queue");
    Queue query = session.createQueue(StatisticsBroker.STATS_BROKER_PREFIX);
    MessageProducer producer = session.createProducer(null);

    producer.send(testQueue, session.createMessage());

    Message msg = session.createMessage();
    msg.setJMSReplyTo(replyTo);
    producer.send(query, msg);
    MapMessage reply = (MapMessage) consumer.receive(10 * 1000);
    assertNotNull(reply);
    assertTrue(reply.getMapNames().hasMoreElements());
    assertTrue(reply.getLong("enqueueCount") >= 1);

    msg = session.createMessage();
    msg.setBooleanProperty(StatisticsBroker.STATS_BROKER_RESET_HEADER, true);
    msg.setJMSReplyTo(replyTo);
    producer.send(query, msg);
    reply = (MapMessage) consumer.receive(10 * 1000);
    assertNotNull(reply);
    assertTrue(reply.getMapNames().hasMoreElements());
    assertEquals(0, reply.getLong("enqueueCount"));
    assertTrue(reply.getJMSTimestamp() > 0);
    assertEquals(Message.DEFAULT_PRIORITY, reply.getJMSPriority());
  }
  @SuppressWarnings("static-access")
  @Override
  public void onMessage(Message message, Session session) throws JMSException {
    logger.debug("receive Syn Product Status msg!");
    MapMessage mapMessage = (MapMessage) message;
    Long merchantId = mapMessage.getLong("merchantId");
    String merchantProdCode = mapMessage.getString("merchantProdCode");
    String status = mapMessage.getString("status");

    HopsRequestEvent hre = new HopsRequestEvent(SynProductStatusMessageListener.class);
    hre =
        new SynProductStatusEvent(
            SynProductStatusMessageListener.class, merchantId, merchantProdCode, status);
    publisher.publishRequestEvent(hre);
  }
예제 #3
0
  /**
   * Try to use ActiveMQ StatisticsPlugin to get size and if that fails fallback to {@link
   * JMSMailQueue#getSize()}
   */
  @Override
  public long getSize() throws MailQueueException {

    Connection connection = null;
    Session session = null;
    MessageConsumer consumer = null;
    MessageProducer producer = null;
    TemporaryQueue replyTo = null;
    long size;

    try {
      connection = connectionFactory.createConnection();
      connection.start();

      session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      replyTo = session.createTemporaryQueue();
      consumer = session.createConsumer(replyTo);

      Queue myQueue = session.createQueue(queueName);
      producer = session.createProducer(null);

      String queueName = "ActiveMQ.Statistics.Destination." + myQueue.getQueueName();
      Queue query = session.createQueue(queueName);

      Message msg = session.createMessage();
      msg.setJMSReplyTo(replyTo);
      producer.send(query, msg);
      MapMessage reply = (MapMessage) consumer.receive(2000);
      if (reply != null && reply.itemExists("size")) {
        try {
          size = reply.getLong("size");
          return size;
        } catch (NumberFormatException e) {
          // if we hit this we can't calculate the size so just catch
          // it
        }
      }

    } catch (Exception e) {
      throw new MailQueueException("Unable to remove mails", e);

    } finally {

      if (consumer != null) {

        try {
          consumer.close();
        } catch (JMSException e1) {
          e1.printStackTrace();
          // ignore on rollback
        }
      }

      if (producer != null) {

        try {
          producer.close();
        } catch (JMSException e1) {
          // ignore on rollback
        }
      }

      if (replyTo != null) {
        try {

          // we need to delete the temporary queue to be sure we will
          // free up memory if thats not done and a pool is used
          // its possible that we will register a new mbean in jmx for
          // every TemporaryQueue which will never get unregistered
          replyTo.delete();
        } catch (JMSException e) {
        }
      }
      try {
        if (session != null) session.close();
      } catch (JMSException e1) {
        // ignore here
      }

      try {
        if (connection != null) connection.close();
      } catch (JMSException e1) {
        // ignore here
      }
    }

    // if we came to this point we should just fallback to super method
    return super.getSize();
  }