Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check the status of an ActiveMQ broker

I want to create a java class , which has the sole purpose of checking the status of an ActiveMQ broker (or connectivity to an ActiveMQ broker as an outage may be defined as the client losing network connectivity as well).

so basically there would be a thread running after every few seconds to check the status of the broker and if there broker is down, I want to do some specific task of mailing the support group and stuff like that.

The examples online are not detailed enough to explain how the above can be achieved.

Has someone already done this, or can suggest a nice way to make this happen??

Thanks, Neeraj

like image 415
Neeraj Avatar asked Oct 18 '11 09:10

Neeraj


2 Answers

The following will also work to check if ActiveMQ is up and running:

try {
   ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
   // set transport listener so that active MQ start is notified.
   factory.setTransportListener(transportListenerObject); 
   Connection connection = factory.createConnection();
   // This will throw error if activeMQ is not running.
   connection.setClientID("my_client_id"); 
} catch (JMSException ex) {
    if (ex.getLinkedException() instanceof IOException) {
        // ActiveMQ is not running. Do some logic here.
        // use the TransportListener to restart the activeMQ connection
        // when activeMQ comes back up.
    } else {
        // Something seriously went wrong with the factory or connection
        // creation. Abort the process here, as nothing can be done.
        // Log the error and troubleshoot.
    }
}
like image 139
Anand Avatar answered Oct 20 '22 23:10

Anand


Send a Testmessage to the broker:

try {
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
    Connection conn = factory.createConnection(user, password);
    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer("test");
    MessageConsumer consumer = session.createConsumer("test");
    consumer.setMessageListener(this); // class that implements MessageListener
    conn.start();
    TextMessage message = new ActiveMQTextMessage();
    message.setText("TestMessage");
    producer.send(message);
} catch (JMSException e) {
    // somethings very wrong
}

connect, send message, if you recieve message: all is fine. if not....

thats what i do. In addition i do some other stuff:

  • listen to several Advisory Topics to receive important events (like Advisory.FULL) that are important indicators that somethings wrong.
  • regularly get the broker statistics from the statistics plugin to monitor message memory size and message store.
  • configure a dead letter queue so i know when a message was rejected by the consumer.
like image 29
Laures Avatar answered Oct 21 '22 01:10

Laures