Thursday 2 July 2015

Dynamic JMS configuration using Fragment Approach

I recently came across a situation where there was a need to configure a generic broker configuration for a different jms providers depending on the choice e.g. ActiveMQ, Websphere without the need to repackage and modify the beans and camel-context configs. The requirement was to deploy a camel osgi bundle/features which can be deployed into JBoss Fuse. This requirement can easily be achieved by the power of fragment bundles. According to OSGI specification fragments are incomplete bundles where the existence of it hugely depends on the host bundle. The host however can't itself be a fragment and must be a full-fledged bundle, even if it relies on its fragment to add classes and resources to it.

Solution

In order to address the above problem we are going to create the host bundle that contains all the business logic and the fragment bundle that's going to have only the connection configuration for different jms providers, this creates a flexible approach without having to repackage the main bundle. However the drawback to this solution is that we'll need to write different fragment bundles for different jms providers and attach it to the host bundle, depending on the need. Nonetheless this should cover the scenario and appears to be flexible enough for deployment, and follow the separation of concerns principle.

Bundles Concept

The main bundle consist of a camel route that uses producerTemplate to simply send bunch of messages into the ActiveMQ jms provider in this case. So the route configuration looks like the following:

and the route definition looks like the following:

Now, for the fragment bundle we place all the broker configuration so we can provide them as a main bundle resource at runtime, below is camel's ActiveMQ component configuration for the underlying jms provider:

Implementing the fragment bundle
As I explained earlier in the post a fragment bundle is like any other bundle, a Jar file with specific headers in its manifest. Even though our fragment bundle is part of our main bundle however we must correlate and identify the fragment to the host/main bundle, here is how we do it from the fragment pom.xml file:


The main key in the above configuration is Fragment-Host element where we specify the main bundle's symbolic name and it comes directly from the artifactId on our main pom.xml. Once we package the bundles e.g. main and the fragment, the manifest file of each could look like the following:

Deploying into Fuse

I have added this project in my github that has the instruction on how to install it into fuse.

0 comments:

Post a Comment