How to monitor ActiveMQ networks

If you’re running ActiveMQ in a distributed setup using network of brokers, you’re probably interested in techniques available to monitor your network. This usually implies viewing the status of the network bridges and generating events when the status changes. There were some improvements in this area for the next 5.5 release and here I’ll try to explain this topic a bit more.

To demonstrate these techniques we need a simple network, so take a look at the one shown in the following diagram.

Network

We see that broker 1 have two network bridges:

  • One created by duplex connection, originating from broker 2
  • and one direct to the broker 3

Now let’s see for starters what JConsole will show us.

JConsole1

JConsole2.png

As you can see, both bridges are shown in JConsole under the Network Bridge tab. You can spot bridges created by remote duplex connectors, by having a connector name pattern like duplex#x and also CreatedByDuplex attribute set to true.

The similar information you can get now in Web Console. There’s a new tab, called Network, which will give you the following view.

Web Console

You can notice that again, you can easily separate bridges created by connectors on this broker and those created by remote duplex connector.

Tools are nice for most usages, but what if you want to monitor your network bridges programatically? Well the first thing you can do is to use JMX API to query bridges. The following application, connects to the broker and lists all of its network bridges.

public class Bridges {
    public static void main(String[] args) throws Exception {
        JMXServiceURL url = new JMXServiceURL(
                "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi");
        JMXConnector connector = JMXConnectorFactory.connect(url, null);
        connector.connect();
        MBeanServerConnection connection = connector.getMBeanServerConnection();
        ObjectName name = new ObjectName(
                "org.apache.activemq:BrokerName=static-broker1,Type=NetworkBridge,*");
        Set bridges = connection.queryNames(name, null);
        for (ObjectName bridgeName : bridges) {
            NetworkBridgeViewMBean view =
                    (NetworkBridgeViewMBean) MBeanServerInvocationHandler.newProxyInstance(
                            connection, bridgeName, NetworkBridgeViewMBean.class, true);
            System.out.println("Bridge to " + view.getRemoteBrokerName()
                    + " (" + view.getRemoteAddress() + ") "
                    + ((view.isCreatedByDuplex() ? "- created by duplex" : "")));
        }
    }
}

So if you run it against broker 1, you’ll get something like

Bridge to static-broker3 (localhost/127.0.0.1:61617) Bridge to static-broker2 (/127.0.0.1:52776) - created by duplex

You can see both bridges, and an indication that bridge to broker 2 is created by remote duplex.

JMX API will give you just the static snapshot of the available bridges. If you’re interested in notifications when bridges are being stopped or started, you can use advisory messages. The following example shows how to subscribe to the appropriate advisory topic and get events of the interest.

public class BridgeAdvisories implements MessageListener { 
 public static void main(String[] args) throws Exception { 
   ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616"); 
   Connection conn = factory.createConnection(); Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);     
   MessageConsumer consumer = sess.createConsumer(AdvisorySupport.getNetworkBridgeAdvisoryTopic());
   consumer.setMessageListener(new BridgeAdvisories()); conn.start(); 
 } 

  @Override 
  public void onMessage(Message message) { 
    try { 
      BrokerInfo brokerInfo = (BrokerInfo) ((ActiveMQMessage) message).getDataStructure();
      boolean started = message.getBooleanProperty("started");
      if (started) { 
        boolean createdByDuplex = message.getBooleanProperty("createdByDuplex");
        System.out.println("Network bridge to " + brokerInfo.getBrokerName() + " (" 
      + brokerInfo.getBrokerURL() + ") has been started " + (createdByDuplex ? "by duplex" : ""));
      } else { 
        System.out.println("Network bridge to " + brokerInfo.getBrokerName() + " (" 
      + brokerInfo.getBrokerURL() + ") has been stopped"); 
      }
    } catch (JMSException e) { e.printStackTrace(); } 
  } 
}

If you run this against our broker 1 and start and stop brokers 2 and 3, you can expect the output similar to the following.

Network bridge to static-broker3 (tcp://dejan-bosanacs-macbook-pro.local:61617) has been stopped 
Network bridge to static-broker3 (tcp://dejan-bosanacs-macbook-pro.local:61617) has been started 
Network bridge to static-broker2 (tcp://dejan-bosanacs-macbook-pro.local:61618) has been stopped 
Network bridge to static-broker2 (tcp://dejan-bosanacs-macbook-pro.local:61618) has been started by duplex

So with this we covered techniques that you can use to monitor networks of ActiveMQ broker, which I hope you’ll find useful. If you’re interested in learning more about ActiveMQ (and related technologies), you can attend one of the online trainings organized by FuseSource

2 comments

  1. Hey,

    thanks for this very nice post! I was searching for a long while for something like this. I tried this and it works very fine. I need similar features for ActiveMQ prior to version 5.5. In fact, i have to use 5.4.3 and i saw that there is no AdvisorySupport.getNetworkBridgeAdvisoryTopic(). So what would be the preffered way to listen for those events with AMQ 5.4.3??

    Thanks in advance

  2. Hi,

    unfortunately I’m not aware of any techniques to do something similar prior to 5.5. You can try polling JMX periodically.

Comments are closed.