2

We are using Blueprint + Camel + Karaf ,migrating from Spring . I am new to OSgi Blueprint . We are using Blueprint XML to define services from beans defined in blueprint xml.

After we added Service in Blueprint XML, atleast are getting from karaf as below: fyi: bundle is in Active state

  karaf>service:list | grep custom
  [org.apache.camel.Processor, com.rnd.model.impl.PaymentServiceProcessorBase,com.rnd.generic.CustomServiceP rocessor]osgi.service.blueprint.compname = customPaymentProcessor

I am sure bean is registering into OSGI Services. but somehow its not visible to other XML in other Bundle.

  **Blueprint XML**::
  <bean id="customPaymentProcessor" class="blah blah"/>
  <service ref="customPaymentProcessor" auto-export="all-classes"/>

Please help me how to get accesss this bean in Routes XML file in APPConfig(under karaf root Dir) Folder.

myRoutes.xml

   <!-- Add this route to CamelContext Using LoadRouteDefinitions  -->
   <routes id="xyz-Context" xmlns="http://camel.apache.org/schema/spring">

     <route id="xyz-one">
              <from uri="direct:xyz"/>
              <!-- this customPayProcesssor is exposed as above  -->
              <process ref="customPayProcesssor"/>
          </route>      

   </routes> 

I understand from this ref: https://access.redhat.com/documentation/en-US/Red_Hat_JBoss_Fuse/6.0/html/Deploying_into_the_Container/files/DeploySimple-Publish.html

All Osgi services are implicitly register as oSGI regsitry for camel to search. But I am getting ;::

  [Bean[ref:cust... because of No bean could be found in the registry for: customPaymentProcessor
Slok
  • 576
  • 1
  • 12
  • 27

3 Answers3

4

This is a simple example on how to do it. You can publish different implementations of an interface and let the bundles ask for the one that fits most.
First let's understand the requirements:

ExportBundle must:

  • export the package that contains PaymentProcessor interface
  • export an implementation of the interface as OSGi service

ImportBundle must:

  • import the package that contains PaymentProcessor interface
  • ask OSGi to inject required service, which is an implementation of the interface
  • keep a reference to such service in a bean, and use it inside Camel Context

All import and export of packages is usually configured by maven-bundle-plugin. Most of the times it get is right enough. Inside Karaf console, use headers <bundleid> to check which service and package import/export exist.

Export an OSGi service using Blueprint:

<blueprint>
    <bean id="customPaymentProcessor" class="my.app.impl.CustomPaymentProcessorImpl">

    <service id="customPaymentProcessorService" ref="customPaymentProcessor"
             interface="my.app.PaymentProcessor" />
</blueprint>

Import an OSGi service using Blueprint and use it in Camel:

<blueprint>
    <reference id="customPaymentProcessor" interface="my.app.PaymentProcessor" />

    <camelContext>
        <route>
            <from uri="direct:start" />
            <to uri="bean:customPaymentProcessor" />
        </route>
    </camelContext>
</blueprint>

Note: beans are not shared between Blueprint contexts. However, in a single bundle you can have as many .xml files in OSGI-INF/blueprint folder as you need. In such case, beans are shared because all files concur to build the same Blueprint context. To add to the fun, if you define CamelContexts in more than one file, you get different contexts.
In bigger bundles I usually define a beans.xml to configure beans and services and a routes.xml to define Camel routes which use beans from the "other" file.

Working Example

I was already working on a similar project, feel free to check an example out on my GitHub.

Alessandro Da Rugna
  • 4,571
  • 20
  • 40
  • 64
1

In order to import your OSGi-service into your bundle you can do something like this:

<reference id="myService" component-name="customPaymentProcessor" 
    interface="com.rnd.model.impl.PaymentServiceProcessorBase" />
Erik Karlstrand
  • 1,479
  • 9
  • 22
  • Before I tried your one I have read from redhat forums that i mentioned above . In My routes.xml how can i use which is not valid ? – Slok Mar 15 '17 at 07:30
  • If you are indeed using Blueprint (as opposed to Spring) the reference-element should be available. Perhaps you are missing a namespace? – Erik Karlstrand Mar 15 '17 at 17:25
0

Looks like you are mixing stuffs:

  • a blueprint file to export your service
  • a camel xml route definition

You should use blueprint also for your route definitions so that you'll get also the chance to use all the blueprint/osgi stuffs (config-admin, service references, etc).

Luca Burgazzoli
  • 1,216
  • 7
  • 9
  • If I use blueprint for route definition means route context right . If I use route context I cannot load dynamically into camel context. – Slok Mar 19 '17 at 05:53