예제 #1
0
파일: JmsHelper.java 프로젝트: araqne/logdb
  public static Map<String, Object> parse(Message message) throws JMSException {
    if (message instanceof TextMessage) {
      TextMessage msg = (TextMessage) message;
      String text = msg.getText();
      Date date = msg.getJMSTimestamp() == 0 ? new Date() : new Date(msg.getJMSTimestamp());

      Map<String, Object> m = new HashMap<String, Object>();
      m.put("_time", date);
      m.put("_msg_id", msg.getJMSMessageID());
      m.put("line", text);

      return m;
    } else if (message instanceof MapMessage) {
      MapMessage msg = (MapMessage) message;
      Date date = msg.getJMSTimestamp() == 0 ? new Date() : new Date(msg.getJMSTimestamp());

      Map<String, Object> m = new HashMap<String, Object>();
      m.put("_time", date);
      m.put("_msg_id", msg.getJMSMessageID());

      @SuppressWarnings("unchecked")
      Enumeration<String> e = msg.getPropertyNames();
      while (e.hasMoreElements()) {
        String key = e.nextElement();
        Object val = msg.getObjectProperty(key);
        m.put(key, val);
      }

      return m;
    }

    return null;
  }
  public void testDestinationStats() 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");
    MessageProducer producer = session.createProducer(null);
    Queue query =
        session.createQueue(StatisticsBroker.STATS_DESTINATION_PREFIX + testQueue.getQueueName());
    Message msg = session.createMessage();

    producer.send(testQueue, msg);

    msg.setJMSReplyTo(replyTo);
    producer.send(query, msg);
    MapMessage reply = (MapMessage) consumer.receive(10 * 1000);
    assertNotNull(reply);
    assertTrue(reply.getMapNames().hasMoreElements());
    assertTrue(reply.getJMSTimestamp() > 0);
    assertEquals(Message.DEFAULT_PRIORITY, reply.getJMSPriority());
    /*
    for (Enumeration e = reply.getMapNames();e.hasMoreElements();) {
        String name = e.nextElement().toString();
        System.err.println(name+"="+reply.getObject(name));
    }
    */
  }
  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());
  }