Listing Camel Routes deployed on ServiceMix

Here’s a simple groovy script to show deployed Apache Camel routes. It uses JMX to query the description of routes available.

import javax.management.*
import javax.management.remote.*
import javax.lang.management.*

def prettyPrint = { s ->
  ps = ""
  level = 0
  for(c in s){
    if(c == '['){
      level++
      ps += "[\n".padRight(level*4," ")
    } else if(c == ']'){
      level--
      ps += "]"
    } else if(c == ','){
      ps += ",\n".padRight(level*4," ")
    } else {
      ps += c
    }
  }
  return ps
}

def serverUrl = 'service:jmx:rmi:///jndi/rmi://localhost:1099/karaf-root'
def jmxUsername = 'smx'
def jmxPassword = 'smx'

def env = [(JMXConnector.CREDENTIALS): [jmxUsername,jmxPassword] as String[]]
def jmxUrl = new JMXServiceURL(serverUrl)
def connect = JMXConnectorFactory.connect(jmxUrl,env)
def server = connect.MBeanServerConnection
def query = new ObjectName('org.apache.camel:type=routes,*')
def routes = server.queryNames(query,null)

routes.each { route ->
	def routeBean = new GroovyMBean(server,route)
	printf "%250s\n", prettyPrint(routeBean.Description)
}

Here’s an example of the output generated by this script:

EventDrivenConsumerRoute[
  Endpoint[
      activemq://topic:STOCKS.TOPIC?clientId=1&durableSubscriptionName=dsn1&selector=SE%3D%27NASDAQ%27] -> Instrumentation:route[
      UnitOfWork(Pipeline[
          [
              Channel[
                  sendTo(Endpoint[
                      log://stocks.out?showAll=true])],
               Channel[
                  sendTo(Endpoint[
                      activemq://queue:STOCKS.NASDAQ])]]])]]

EventDrivenConsumerRoute[
  Endpoint[
      activemq://queue:STOCKS.IN] -> Instrumentation:route[
      UnitOfWork(Pipeline[
          [
              Channel[
                  sendTo(Endpoint[
                      log://stocks.in?showAll=true])],
               Channel[
                  sendTo(Endpoint[
                      activemq://topic:STOCKS.TOPIC])]]])]]

Listing ActiveMQ queues with JMX and Groovy

The following groovy script displays a list of queues in ActiveMQ with some statistics per queue.

import javax.management.*
import javax.management.remote.*
import javax.lang.management.*

def serverUrl = 'service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi'

def env = [(JMXConnector.CREDENTIALS): ["smx","smx"] as String[]]
def jmxUrl = new JMXServiceURL(serverUrl)
def connect = JMXConnectorFactory.connect(jmxUrl,env)
def server = connect.MBeanServerConnection

def query = new ObjectName('org.apache.activemq:Type=Queue,*')
def queues = server.queryNames(query,null)
printf "%-50s, %7s, %7s, %7s, %7s, %7s, %7s, %7s, %7s, %7s\n", "Name", "Avg QT", "Max QT", "Min QT", "Enq Cnt", "Dsp Cnt", "Exp Cnt", "Inf Cnt", "Consumrs", "Producr"

printf "%-50s, %7s, %7s, %7s, %7s, %7s, %7s, %7s, %7s, %7s\n", "----", "------", "------", "------", "-------", "-------", "-------", "-------", "--------", "-------"

queues.each { queue ->
        def queueBean = new GroovyMBean(server,queue)
        printf "%-50s, %7.2g, %7.2g, %7.2g, %7s, %7s, %7s, %7s, %7s, %7s\n",
                queueBean.Name,
                queueBean.AverageEnqueueTime as Float, queueBean.MaxEnqueueTime as Float, queueBean.MinEnqueueTime as Float,

                queueBean.EnqueueCount, queueBean.DispatchCount, queueBean.ExpiredCount, queueBean.InFlightCount,
                queueBean.ConsumerCount, queueBean.ProducerCount
}

Listing Camel Routes using JMX and Groovy

The following groovy script can be used to list all Camel routes on a running servicemix. Per route it will display state of the route, and some statistics.


import javax.management.*
import javax.management.remote.*
import javax.lang.management.*

def serverUrl = 'service:jmx:rmi:///jndi/rmi://localhost:1099/karaf-root'

def env = [(JMXConnector.CREDENTIALS): ["smx","smx"] as String[]]
def jmxUrl = new JMXServiceURL(serverUrl)
def connect = JMXConnectorFactory.connect(jmxUrl,env)
def server = connect.MBeanServerConnection

def query = new ObjectName('org.apache.camel:type=routes,*')
def routes = server.queryNames(query,null)
printf "%-30s, %-10s, %7s, %7s, %7s, %7s, %7s, %7s, %7s\n", "RouteId", "State", "Ex comp", "Ex Fail", "Ex Tot", "Last T", "Max T", "Mean T", "Total T"
printf "%-30s, %-10s, %7s, %7s, %7s, %7s, %7s, %7s, %7s\n", "-------", "-----", "-------", "-------", "------", "------", "-----", "------", "-------"
routes.each { route ->
	def routeBean = new GroovyMBean(server,route)
	printf "%-30s, %-10s, %7s, %7s, %7s, %7s, %7s, %7s, %7s\n",
		routeBean.RouteId, routeBean.State,
		routeBean.ExchangesCompleted, routeBean.ExchangesFailed, routeBean.ExchangesTotal,
		routeBean.LastProcessingTime, routeBean.MaxProcessingTime, routeBean.MeanProcessingTime, routeBean.TotalProcessingTime
}

Monitoring Camel routes with JMX

ServiceMix and Camel expose a lot of information through jmx mbeans. You can use jconsole to access this information.

Start jconsole and connect to a running servicemix instance. Look for the entry with karaf in the name:

JConsole: new connection

In the mbeans tab you’ll find a node for Apache Camel. Here you’ll find all your routes, endpoints, services and processors:

Route MBean

You can name your routes using the id method, this makes it easier to find it in jconsole:

public class TestRouteBuilder extends RouteBuilder {

    public void configure() throws Exception {
        from("file:///tmp/testin").id("fileToLogRoute").to("log:filereceived?showAll=true");
    }

}

THe attributes section shows you a lot of information about the exchanges that have been handled by the route, for example number of exchanges completed, number of failed exchanges. It also shows you the status of a route.

Route Attributes

The methods sections enables you to start and stop a route:

Route operations