Managing Camel Routes With JMX APIs
Join the DZone community and get the full member experience.
Join For FreeHere 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, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments