One of the many good practices OSGi1 frameworks impose is properly modularizing your code. Having your app modularized, you have quite a few choices when it comes to communication between those modules. You can build your applications as “MonolithFirst”2, but you can decide up-front to choose a communication style which will not hinder breaking them down into separate containers so e.g. they can be deployed in a cluster.

With OSGi, of course, a direct communication style such as declarative services3 could be your first choice. But what if some of the operations are long running and therefore an asynchronous communication style would be preferable? Additionally, what if you foresee that in the not so distant future you would like to have the option to separate some of the modules responsible for those long running operations into separate containers and deploy one or more instances of each in Kubernetes4 cluster?

For those reasons, I chose to use a message broker to facilitate this asynchronous communication and have the flexibility to migrate the application into cluster whenever I choose to do so. Because of my previous experience with and its versatility, I chose RabbitMQ5. In addition, to have the option of using some of the more popular Enterprise Integration Patterns6, without having to implement them from scratch, I chose Camel7, which, luckily has a RabbitMQ component8.

Since I found very little to no documentation regarding integration of all these pieces together in an OSGi R7 framework, it’s one of the several things I’d like to share having gathered this material when putting together the Automated Linguistic Analysis application It is a complete application showcasing usage of technologies such as:

  • OSGi R7 Promises for asynchronous generation of transcriptions and linguistic analyses
  • OSGi R7 Push Stream and JAX RS Server Sent Events for push notifications of processing status
  • Apache Camel 2.23.1 and RabbitMQ 3.7 for asynchronous communication between services
  • JPA 2.1 and Hibernate 5.2.12, along with OSGi R7 JPA and Transaction Control services, for persistence layer
  • OSGi R7 HTTP and JAX RS Whiteboard for registering servlets, resources and REST controllers
  • OSGi R7 Configurator, Configuration Admin and Metatype services for automatic configuration of components
  • OSGi R7 Declarative Services for dependency injection
  • Maven automated build of Docker images
  • Maven automated deployment into Kubernetes cluster
  • RabbitMQ message broker as a StatefulSet
  • CockroachDB relational database as a StatefulSet

You can clone, configure and deploy the application yourself–all steps are documented

Here, I will not cover basics of neither OSGi, Bnd9/Bndtools10, Camel nor RabbitMQ–you can explore sites like OSGi enRoute11, where you can find plenty of introductory material, as well as DZone12 or blogs like Vogella13–and only focus on what’s most important to have these work together for this particular example, including using custom message types.

1. Since this is an OSGi application which uses Maven14 with Bnd plugins, we start by adding required dependencies; hence, in service-messaging-impl module you will find camel-scr and camel-rabbitmq dependencies, i.e.:


2. In the same service-messaging-impl module we add class which will bootstrap OSGi implementation of Camel Context (OsgiDefaultCamelContext), i.e. software.into.ala.service.messaging.impl.MessagingBootstrapService extending org.apache.camel.scr.AbstractCamelRunner provided by the camel-scr dependency.

3. We switch now to the mono-app module which pulls together all the components required for our Monolith application, and in the mono-app.bndrun run descriptor file we reference the service-messaging-impl module via runrequires instruction, so that all of the dependencies it brings can be resolved properly, i.e.

-runrequires: \ 

4. In the same mono-app module, in both mono-app.bndrun and debug.bndrun run descriptor files via runproperties instruction we add classes which Camel uses, i.e.:

-runproperties: \

Without these, as soon as you’d launch the app, you’d hit this error:

(…) Exception in thread "Camel Thread #0 - LRUCacheFactory" java.lang.NoClassDefFoundError: Could not initialize class
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(
at java.lang.reflect.Constructor.newInstance(
at Source) (…) 

5. Now, for the last part, since we would like to use custom message types for our asynchronous communication via RabbitMQ message broker, in the service-messaging-dto module, which contains classes serving as custom message types, in POM of this module we attach it module as fragment to the org.apache.camel.camel-rabbitmq bundle brought in by the camel-rabbitmq dependency, i.e.:

    Fragment-Host: org.apache.camel.camel-rabbitmq
    Bundle-SymbolicName: ${project.groupId}.${project.artifactId}
     -sources: true
     -contract: *

This is required so these custom message types can be found by the camel-rabbitmq dependency – without this, as soon as modules started communicating with each other, you’d hit the following error:

(…) RabbitMQConsumer Error processing exchange. Exchange[]. Caused by: [java.lang.ClassNotFoundException - software.into.ala.service.messaging.dto.FileMessageDTO cannot be found by org.apache.camel.camel-rabbitmq_2.23.1]
java.lang.ClassNotFoundException: software.into.ala.service.messaging.dto.FileMessageDTO cannot be found by org.apache.camel.camel-rabbitmq_2.23.1
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(
    at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(
    at org.apache.camel.component.rabbitmq.RabbitMQMessageConverter.deserializeBody(
    at org.apache.camel.component.rabbitmq.RabbitMQMessageConverter.populateMessageBody(
    at org.apache.camel.component.rabbitmq.RabbitMQMessageConverter.populateRabbitExchange(
    at org.apache.camel.component.rabbitmq.RabbitMQEndpoint.createRabbitExchange(
    at org.apache.camel.component.rabbitmq.RabbitConsumer.doHandleDelivery(
    at org.apache.camel.component.rabbitmq.RabbitConsumer.handleDelivery(
    at com.rabbitmq.client.impl.ConsumerDispatcher$
    at com.rabbitmq.client.impl.ConsumerWorkService$
    at java.util.concurrent.ThreadPoolExecutor.runWorker(
    at java.util.concurrent.ThreadPoolExecutor$
    at (…)

  1. “OSGi“ 

  2. “MonolithFirst” 

  3. “OSGi Compendium R7 Declarative Services Specification” 

  4. “Kubernetes“ 

  5. “RabbitMQ“ 

  6. “Enterprise Integration Patterns“ 

  7. “Apache Camel“ 

  8. “RabbitMQ Component“ 

  9. “bnd“ 

  10. “Bndtools“ 

  11. “OSGi enRoute“ 

  12. “DZone“ 

  13. “Vogella Blog“ 

  14. “Maven“