Over a million developers have joined DZone.

Exporting Camel Endpoints as OSGi Services

DZone 's Guide to

Exporting Camel Endpoints as OSGi Services

· Integration Zone ·
Free Resource

Access to OSGi services through a bus

It is often useful to proxy calls to OSGi services through a service bus.

The bus could be used to log incoming calls, adapt/filter the call arguments and the result, transparently forward requests to a remote OSGi container, broadcast to several remote containers or forward to the more powerful Fabric8 endpoints.

Exporting a regular interface to a Camel endpoint is key to this functionality.

Figure 1: Direct service binding

Figure 2: Binding to a Camel proxy

Camel Bean Invocation

Camel allows to wrap calls to interfaces in BeanInvocation objects within Exchanges and then send them through endpoints such as JMS, HTTP, etc.

Camel supports 3 approaches to bean invocation:

  • The bean component which invokes a bean in the bean registry (e.g. Blueprint)
  • POJO producing where proxies that automate the bean invocation are injected through annotations.
  • Spring remoting where the proxies used in POJO producing are created explicitly.

Spring Remoting

We’ll employ Spring remoting to explicitly create service proxies linked to Camel endpoints and export these proxies as OSGi services in the Blueprint container.

Given the following interface:

interface TaskManager {
    Task save(Task task);
    void delete(String id);
    Task find(String id);

The Blueprint container implementing the TaskManager service makes it available through Camel endpoint activemq:queue:io.modio.blog.osgi.camel.proxy:

<blueprint default-activation="eager"
    <bean id="taskManager"
    <camelContext id="camelContext"
            <from uri="activemq:queue:io.modio.blog.osgi.camel.proxy"/>
            <to uri="bean:taskManager"/>

The proxy to the TaskManager service can be in the same or a different Blueprint container:

<blueprint default-activation="eager"
    <camelContext id="camelContext"
        <proxy id="taskManagerProxy"
    <service interface="io.modio.blog.osgi.camel.proxy.TaskManager"

Alternatively if the proxy is defined outside the camel context then the blueprint container and camel context ID need to be set explicitly and afterPropertiesSet() needs to be called:

<blueprint default-activation="eager"
    <camelContext id="camelContext"
    <bean id="taskManagerProxyFactory" init-method="afterPropertiesSet"
        <property name="serviceUrl"
        <property name="serviceInterface"
        <property name="camelContextId" value="camelContext"/>
        <property name="blueprintContainer" ref="blueprintContainer"/>
    <service interface="io.modio.blog.osgi.camel.proxy.TaskManager">
        <bean factory-ref="taskManagerProxyFactory" factory-method="getObject"/>

The Blueprint container on the consuming side is:

<blueprint default-activation="eager"
    <reference id="taskManager"
    <bean id="driver" class="io.modio.blog.osgi.camel.proxy.Driver">
        <property name="taskManager" ref="taskManager"/>

Blueprint service reference semantics

As powerful as it is, this technique also has a notable side effect. Placing the proxy in a different Blueprint container than the service implementation breaks the semantics of Blueprint service references: the proxy service may be available while the real service may be not.

This will affect both the bootstrapping of the service consumer Blueprint container and the semantics of the calls to the service when the underlying service is not available:

   Direct Service Binding Binding to a Camel Proxy 
 Service is unavailable during initialization phase.

The blueprint container initialization will block until the required service becomes available.
The blueprint container will complete initialization and bind to the proxy.
 Service becomes unavailable after initialization phase. Call to the service reference will block until the service becomes available again or a timeout occurs in which case a ServiceUnavailableException exception is thrown.
The blueprint container will call the proxy. The behavior of the call will depend on the Camel routing setup. E.g.
  • An asynchronous call that posts to a queue will complete (however the message will be left in the queue until the backend service is available to consume it).
  • A synchronous call that posts to a queue expecting a reply will block.
  • A call that delegates to a REST API that is not available, will result in an exception.

Blueprint's approach to service references has received a lot of criticism recently in favor of declarative services (DS). E.g. check the discussions regarding the migration of Fabric8 from Blueprint to DS or the (expected?) migration of Apache Karaf core 4.x from Blueprint to DS.  However, in this particular case, DS is also affected because communication through the bus bypasses the OSGi service lookup.

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}