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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/blueprint" | |
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0" | |
xsi:schemaLocation=" | |
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd | |
http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd"> | |
<bean id="messageProducer" class="com.jboss.fuse.MessageProducer"> | |
<property name="camelTemplate" ref="camelTemplate" /> | |
</bean> | |
<camelContext id="blueprintContext" trace="false" | |
xmlns="http://camel.apache.org/schema/blueprint"> | |
<template id="camelTemplate" /> | |
<routeBuilder ref="messageProducer" /> | |
</camelContext> | |
</blueprint> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jboss.fuse; | |
import org.apache.camel.ProducerTemplate; | |
import org.apache.camel.builder.RouteBuilder; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class MessageProducer extends RouteBuilder { | |
private static final Logger logger = LoggerFactory | |
.getLogger(MessageProducer.class); | |
private ProducerTemplate camelTemplate; | |
@Override | |
public void configure() throws Exception { | |
if (camelTemplate != null) { | |
for (int i = 0; i <= 100; i++) { | |
logger.info("Sending message -: " + i); | |
camelTemplate.sendBody("jms:testQueue", "camel"); | |
} | |
} | |
} | |
public void setCamelTemplate(ProducerTemplate camelTemplate) { | |
this.camelTemplate = camelTemplate; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/blueprint" | |
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0" | |
xsi:schemaLocation=" | |
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd | |
http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd"> | |
<bean id="activemq-jms-connection-factory" class="org.apache.activemq.ActiveMQConnectionFactory"> | |
<property name="brokerURL" value="tcp://localhost:61616" /> | |
<property name="userName" value="admin" /> | |
<property name="password" value="admin" /> | |
</bean> | |
<bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent"> | |
<property name="configuration" ref="jmsConfig" /> | |
</bean> | |
<bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration"> | |
<property name="connectionFactory" ref="pooledConnectionFactory" /> | |
<property name="concurrentConsumers" value="10" /> | |
</bean> | |
<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" | |
init-method="start" destroy-method="stop"> | |
<property name="maxConnections" value="8" /> | |
<property name="connectionFactory" ref="activemq-jms-connection-factory" /> | |
</bean> | |
</blueprint> |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<plugin> | |
<groupId>org.apache.felix</groupId> | |
<artifactId>maven-bundle-plugin</artifactId> | |
<version>2.2.0</version> | |
<extensions>true</extensions> | |
<configuration> | |
<instructions> | |
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName> | |
<Fragment-Host>camel-broker-client;bundle-version=1.0.0.SNAPSHOT</Fragment-Host> | |
</instructions> | |
</configuration> | |
</plugin> |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main bundle: | |
Bundle-SymbolicName: camel-broker-client | |
Bundle-Version: 1.0.0.SNAPSHOT | |
fragment bundle: | |
Fragment-Host: camel-broker-client;bundle-version=1.0.0.SNAPSHOT | |
Bundle-Version: 1.0.0 |