Enterprise Integration Zone is brought to you in partnership with:

Independent IT Consultant and Apache Camel Committer with a background in open source Java architecture, design and development. Currently helping companies utilize Apache ActiveMQ, Camel & Servicemix technologies. Ben is a DZone MVB and is not an employee of DZone and has posted 11 posts at DZone. You can read more from them at their website. View Full User Profile

Managing Camel Routes With JMX APIs

07.29.2012
| 4351 views |
  • submit to reddit

Here is a quick example of how to programmatically access Camel MBeans to monitor and manipulate routes...

first, get a connection to a JMX server (assumes localhost, port 1099, no auth)
note, always cache the connection for subsequent requests (can cause memory utilization issues otherwise)

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url);
MBeanServerConnection server = jmxc.getMBeanServerConnection();

use the following to iterate over all routes and retrieve statistics (state, exchanges, etc)...

ObjectName objName = new ObjectName("org.apache.camel:type=routes,*");
List<ObjectName> cacheList = new LinkedList(server.queryNames(objName, null));
for (Iterator<ObjectName> iter = cacheList.iterator(); iter.hasNext();)
{
    objName = iter.next();
    String keyProps = objName.getCanonicalKeyPropertyListString();
    ObjectName objectInfoName = new ObjectName("org.apache.camel:" + keyProps);
    String routeId = (String) server.getAttribute(objectInfoName, "RouteId");
    String description = (String) server.getAttribute(objectInfoName, "Description");
    String state = (String) server.getAttribute(objectInfoName, "State");
    ...
}

use the following to execute operations against a Camel route (stop,start, etc)

ObjectName objName = new ObjectName("org.apache.camel:type=routes,*");
List<ObjectName> cacheList = new LinkedList(server.queryNames(objName, null));
for (Iterator<ObjectName> iter = cacheList.iterator(); iter.hasNext();)
{
    objName = iter.next();
    String keyProps = objName.getCanonicalKeyPropertyListString();
    if(keyProps.contains(routeID))
    {
        ObjectName objectRouteName = new ObjectName("org.apache.camel:" + keyProps);
        Object[] params = {};
        String[] sig = {};
        server.invoke(objectRouteName, operationName, params, sig);
        return;
    }
}

 summary

These APIs can easily be used to build a web or command line based tool to support remote Camel management features. All of these features are available via the JMX console and Camel does provide a web console to support some management/monitoring tasks.

See these pages for more information...
http://camel.apache.org/camel-jmx.html
http://camel.apache.org/web-console.html

 

 

 

 

 

 

 

 

Published at DZone with permission of Ben O' Day, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)