Subscribe to RSS Feed

Kit Plummer
Software Engineer :: Researcher :: Techitect :: Evangelist :: Advisor
kitplummer@gmail.com

To the top »

Please share:

Pax Exam and ActiveMQ…Testing OSGi Bundles and JMS

I’ve been working with OSGi, specifically Apache Felix/Karaf, a lot lately. I’m also building a few services that interact via JMS. Being test-minded I was looking for a good way to automate testing of my bundles, possibly even deployed against multiple OSGi frameworks. I found PaxExam and became enamored with its great features for integration testing OSGi bundles, or multiples thereof. Pax Exam provides a great API-based scheme for declaring dependent bundles, and means for fetching them from Maven.

There’s already some great posts on using PaxExam, like this one - so I won’t get too much into that. But here’s how I was able to test a couple of Blueprint -based bundles that require a javax.jms.ConnectionFactory to be injected. I also am using ActiveMQ to run an embedded (within the test harness) JMS broker, and expose a ConnectionFactory to the OSGi service framwork.

First, I create an embedded broker in the “setup” method, and create and register the ConnectionFactory:

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
...
@Inject
private BundleContext bundleContext;
private BrokerService broker;
private ActiveMQConnectionFactory connectionFactory;
@Before
public void setup() throws Exception {
...
BrokerService broker = new BrokerService();
broker.setUseJmx(false);
broker.addConnector("tcp://localhost:61616");
broker.start();
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
bundleContext.registerService(javax.jms.ConnectionFactory.class.getName(),
connectionFactory, new Hashtable());
}

Note the disabling of the JMX in the broker - otherwise ActiveMQ will fail to launch because some OSGi framework initiate the same 1099 JMX port.

Use the @Configuration annotation to setup the broker’s required dependencies:

@Configuration
public static Option[] configuration() {
return options(felix(), profile("spring.dm"), provision(
....
mavenBundle().groupId("org.apache.geronimo.specs").artifactId("geronimo-jms_1.1_spec"),
mavenBundle().groupId("org.apache.geronimo.specs").artifactId("geronimo-annotation_1.0_spec"),
mavenBundle().groupId("org.apache.geronimo.specs").artifactId("geronimo-j2ee-management_1.1_spec"),
mavenBundle().groupId("org.apache.activemq").artifactId("kahadb"),
mavenBundle().groupId("org.apache.activemq").artifactId("activemq-core").version("5.4.2"),
mavenBundle().groupId("com.bundleundertest").artifactId("mybundle")
)
);
}

Now, any @Test cases can send messages to bundles, and listen on topics or queues which bundles produce to.

Lastly, I use the @After annotation and to shutdown the broker:

@After
public void shutdown() throws Exception {
if(null != broker) {
broker.stop();
}
}

Please comment:
blog comments powered by Disqus