Thursday, July 01, 2010

Extending Apache ActiveMQ with Apache Camel

Apache Camel - the excellent integration engine - can be embedded in an ActiveMQ broker to turn it into a full integration hub. Camel has probably more integration connectors with 3rd party apps than any other ESB - couple that with its ease of use and you get an understanding of why its so popular!

Camel actually started off as a sub-project of ActiveMQ - so the integration between the two is excellent (although it will be even better in ActiveMQ 6).

I blogged a while ago about the new Statistics Plugin - that you can use to request a message from the ActiveMQ broker about its running statistics (just in case you don't want to use JMX). Now it would kinda nice to automatically send these messages out from the ActiveMQ message broker periodically. Well its really easy to do that ...

Firstly enable the statics plugin in the ActiveMQ broker configuration and also import a camel config - e.g.

   <broker brokerName="testBroker"  xmlns="">
      <transportConnector uri="tcp://localhost:61616"/>
  <import resource="camel.xml"/>

 Then in the camel XML configuration define an embedded ActiveMQ Connection -

<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent" >
        <property name="connectionFactory">
          <bean class="org.apache.activemq.ActiveMQConnectionFactory">
            <property name="brokerURL" value="vm://testBroker?create=false&amp;waitForStart=1000" />
            <property name="userName" value="DEFAULT_VALUE"/>
            <property name="password" value="DEFAULT_VALUE"/>

Note: the vm:// transport (uses a broker's name to locate it in the same JVM)

Then just add a Camel route in the Camel configuration which uses a Timer  to request statistics from the broker and publish them on a Topic  every second - e.g:

   <from uri="timer://foo?fixedRate=true&amp;period=1000"/>
   <inOut uri="activemq:queue:ActiveMQ.Statistics.DestinationTest.Queue"/>
   <to uri="activemq:topic:Statistics.Topic"/>


We are going to be on the road doing the popular  Fuse days again from September - but in the meantime - you can find out more about Apache Camel, ActiveMQ and ServiceMix from our Fuse webinars.

1 comment:

Paddy said...

Just a quick comment. If your using spring 3 it looks like you need to place the plugins tag alphabetically in activemq.xml.

I placed it after persistenceAdapter