简体   繁体   中英

Using OSGi service in the camel route

I am reading the book 'Camel in Action' and I am unable to work out an example (Section 4.3.4 OsgiServiceRegistry) using OSGi service in the camel route. This is my bean (exposed as OSGi service

public class HelloBean {
public String hello(String name){
    System.out.println(" Invoking Hello method ");
    return "Hello " + name;

 }
}

This is the spring XML file that exposes the above bean as service

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://camel.apache.org/schema/spring
   http://camel.apache.org/schema/spring/camel-spring.xsd
   http://www.springframework.org/schema/osgi
   http://www.springframework.org/schema/osgi/spring-osgi.xsd">

<bean id="helloBean" class="camelinaction.testbeans.HelloBean" />

<osgi:service id="helloService" interface="camelinaction.testbeans.HelloBean" ref="helloBean" />

<camelContext xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="direct:start" />
        <bean ref="helloService" method="hello" />
    </route>
</camelContext>

</beans>

When I execute the maven goal 'camel:run', I get the following exception:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloService': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: required property 'bundleContext' has not been set

Please let me know how to set the bundleContext. I am using eclipse equinox as OSGi container.

camel:run just runs a thin non-OSGi runtime using the Spring Camel configs in your project. The message that you are getting is from SpringDM (the thing that instantiates the <osgi:service id="helloService"...> ) not being able to locate an OSGi environment. To get this to work you need to install the code inside a supporting container - such as Karaf of Servicemix.

If you'd like to see OSGi working with Camel, check out the Servicemix Bootstraps project at https://github.com/FuseByExample/smx-bootstraps - full documentation is there around installing and tweaking the code. The bundles you'll be interested in there are smx-ponger and smx-ponger-service , which demonstrate the consumption and provision of OSGi services respectively.

I have run into situations like this in the past where I have OSGi dependent components in my camel route and I want to run/debug through an IDE like Eclipse.

If you are looking to debug while you develop, you can deploy to ServiceMix and remotely debug:

http://servicemix.apache.org/developers/remote-debugging-servicemix-in-eclipse.html

Camel 2.10 might support your scenario out of the box with OSGi blueprint:

http://camel.apache.org/camel-run-maven-goal.html

Spring OSGI extensions are fine, but as you can see it is a bit incestuous to test the service interface when you implement and declare the bean from the same spring context. You could of course have bean reference helloBean, but that defeats the purpose.

I am not sure of spring-osgi extension behavior, but at least with the very similar camel-blueprint with pojosr the same test can be with the modified helloService element.

<to uri="bean:camelinaction.testbeans.HelloBean" method="hello" />

Note the unusual fact that where bean id normally references a bean id you are now using the fully qualified interface.

Of course, this has some unfortunate limitations. It works fine if there is only one service instance implementing the desired interface, but there is no obvious way (to me) on how to apply a filter. One alternative in that case is to resort to actually using the bundleContext property of the CamelContext and using the programmatic API. But of course we would like to avoid that in favor of declarative approaches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM