Tuesday, March 17, 2009

Can Apache Camel be independent of the queuing engine ?

Designing and deploying an Apache Camel project in an environment where you have (as an administrator) the control is something very simple to do. Most of the time, the code that you will receive from the developers will be ready to be deployed on the server (ServiceMix, J2EE, ...).

But this is not the case, when you work in big companies where the infrastructure is separated from the teams in charge to design/build/deploy and release the application. As a developer you don't have access to the acceptance or production environment. This means that you must provide different configuration files to the builder team and/or release management in order to deploy your application in development, acceptance and production environment.

Obviously, the name of the servers are not the same and the infrastructure is in charge to manage the queues servers. In this environment, infra will never accept that the queues definition or creation of the queuing engine are under the responsibility of the developer(s). Thus, it will not be possible in your spring DSL file (or Camel Java DSL classes) to declare your queuing engine like this :

<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent" >
<property name="brokerURL" value="vm://localhost:61616" / >
</bean >

remark: vm://localhost:61616 must be changed to serverName.intranet.domainName:port
depending on queuing engine deployed in the different environment.

To make Camel independent of its queuing engine :
  • ActiveMq,
  • WebSphereMQ,
  • SonicMq,
  • TIBCO,
  • ...
and parameters like :
  • Size of the queue,
  • Size of the messages accepted on the queue,
  • Persistence of the messages
  • ...
you must made different modifications. Let's assume that you plan to use ActiveMq as your queuing engine. Here is the list of the modifications to do :

1) Add the required bundles to your ServiceMix 4 osgi server to allow it to work with ActiveMq. Normally, the features transaction and activemq are activated by default in the file etc/org.apache.servicemix.features.cfg. So, you don't have to do anything else except if you use ServiceMix Kernel. In this case, you have to add the bundles manually or add a link (in the config file) to the features.xml file where the bundles have been declared

2) Create a spring xml file where the parameters of the queuing engine are defined. Luckily, such a file already exists and is available from Apache ServiceMix 4 project. So, copy the file into the deploy folder of ServiceMix 4 or Servicemix 4 Kernel

3) Create a osgi-queueservice.xml file containing the following lines and copy it in the deploy folder of ServiceMix

<?xml version="1.0" encoding="UTF-8"? >
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">

<bean id="active-mq" class="org.apache.activemq.camel.component.ActiveMQComponent" />

<osgi:service id="osgiqueuingservice" ref="active-mq" interface="org.apache.camel.Component"/>

</beans>

When the spring file will be at the server startup loaded by Spring (though its Application context), it will create the bean "active-mq" and instantiate the class : org.apache.activemq.camel.component.ActiveMQComponent.

Remarks :
- No parameters are provided to the ActiveMqComponent like brokerUrl, ...
- If you need to work with another JMS queuing engine, you can replace the class ActiveMQComponent by : org.apache.camel.component.jms.JmsComponent

Next it will create an OSGI service called "osgiqueuingservice" and expose it through the interfaces : org.apache.camel.Component

Remark : As JmsComponent, ActiveMqComponent implements this interface, the OSGI service is generic. So you could parameterized it using a property file (cfg file in the ServiceMix world)

4) The last step is very simple. In your camel config file, you add a reference to your osgi service in order to retrieve the queuing engine like this :

<osgi:reference id="queuingservice" interface="org.apache.camel.Component" />

Please remark that you import the Component interface. So, Camel becomes independent of the queuing engine used (WebSphere, TIBCO, ActiveMQ, ...)

and in your Camel route :

<camel:from uri="queuingservice:queue:in" />

3 comments:

James Strachan said...

Just use the ${broker.url} spring property notation in your Spring XML.

Then whether you use stand alone applications, WARs or OSGi bundles in ServiceMix you can then bind the actual connection details to your deployment units.

e.g. using the OSGi config service, putting a properties file on the classpath, using system properties etc

Elena said...

I can't use ${broker.url}. returns error: Illegal character in path at index 1: ${brokerURL}

in my xml:








in my properties.properties:
brokerURL = tcp://localhost:61616

Using works
Can you public an example how to use the properties in xml?

cmoulliard said...

Hi Elena.

Can you show me on Github the project that you are using ? Do you use Spring with OSGI, without ? How do you declare the property place holder ?

Regards,

Charles