Retrieving Broker and Destination Metrics

About Retrieving Metrics for the Message Queue Broker and Destination Applications

As a Cloud Foundry (CF) user, you can use the Statistics Plugin integrated with ActiveMQ to query and retrieve statistical data for the message queue broker, message queue, and destination applications. The statistical data contains various metrics, such as size of the messages, average time of the messages in the queue, the usage of application memory, number of producers and consumers, and so on. For more information on the Statistics Plugin, refer to the Apache ActiveMQ documentation.

Retrieve Metrics for Broker and Destination Applications

Before You Begin

Example: Retrieving Metrics for the Message Broker and Destination Applications

The following code example retrieves metrics for the message broker and destination applications:
import java.util.Enumeration;
import javax.jms.Connection;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public class StatisticsPluginData {
 private static String AMQ_ENDPOINT = "ssl://hostname:61617";
 private static String ACTIVE_MQ_USERNAME = "user";
    private static String ACTIVE_MQ_PASSWORD = "pwd";
    private String DESTINATION_NAME = "testqueue";
    private String DESTINATION_TYPE = "queue";
    private int msgCount = 2;
 
 public StatisticsPluginData(String destinationName, String destinationType, String amqEndpoint, String username, String password, int msgCount) {
  super();
  this.DESTINATION_NAME = destinationName;
  this.DESTINATION_TYPE = destinationType;
  StatisticsPluginData.AMQ_ENDPOINT = amqEndpoint;
  StatisticsPluginData.ACTIVE_MQ_PASSWORD = password;
  StatisticsPluginData.ACTIVE_MQ_USERNAME = username;
  this.msgCount = msgCount;
 }
 
 private static ActiveMQConnectionFactory createActiveMQConnectionFactory() {
        // Create a connection factory.
        final ActiveMQConnectionFactory connectionFactory =
                new ActiveMQConnectionFactory(AMQ_ENDPOINT);
        // Pass the username and password.
        connectionFactory.setUserName(ACTIVE_MQ_USERNAME);
        connectionFactory.setPassword(ACTIVE_MQ_PASSWORD);
        return connectionFactory;
    }
 
 public void getQueueStatistics()
 {
  Session session = null;
  Connection connection = null;
  ActiveMQConnectionFactory connectionFactory = null;
  Queue statisticsDestination = null;
  Queue replyTo;
  MessageConsumer consumer = null;
  MessageProducer producer = null;
  Message msg;
  String statisticsQueue;
  MapMessage statisticsMsg;
  Enumeration qEnum;
  String statistic;
  
  try
  {
   connectionFactory = createActiveMQConnectionFactory();
   connection = connectionFactory.createConnection();
   connection.start();
   
   session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   replyTo = session.createTemporaryQueue();
   
   consumer = session.createConsumer(replyTo);
   producer = session.createProducer(null);
   
   statisticsQueue = "ActiveMQ.Statistics.Destination." + this.DESTINATION_NAME;
   
   statisticsDestination = session.createQueue(statisticsQueue);
   msg = session.createMessage();
   msg.setJMSReplyTo(replyTo);
   producer.send(statisticsDestination,msg);
   
   statisticsMsg = (MapMessage)consumer.receive();
   System.out.println("Printing Statistics............................");
   
   for(qEnum = statisticsMsg.getMapNames(); qEnum.hasMoreElements();)
   {
    statistic = qEnum.nextElement().toString();
    System.out.println(statistic + " -- " + statisticsMsg.getObject(statistic));
   }
   
  }
  catch(Exception e)
  {
   System.out.println("Error msg - QueueStatistics Failure: ..........."+e.getMessage());
   e.printStackTrace();
  }
 }
}