简体   繁体   English

在驼峰路线中使用OSGi服务

[英]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. 我正在阅读“Camel in Action”这本书,我无法在驼峰路线中使用OSGi服务制定一个例子(第4.3.4节OsgiServiceRegistry)。 This is my bean (exposed as OSGi service 这是我的bean(暴露为OSGi服务

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 这是将上述bean公开为服务的spring XML文件

<?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: 当我执行maven目标'camel:run'时,我得到以下异常:

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. 请让我知道如何设置bundleContext。 I am using eclipse equinox as OSGi container. 我使用eclipse equinox作为OSGi容器。

camel:run just runs a thin non-OSGi runtime using the Spring Camel configs in your project. camel:run只使用项目中的Spring Camel配置运行一个瘦的非OSGi运行时。 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. 您获得的消息来自SpringDM(实例化<osgi:service id="helloService"...> )无法找到OSGi环境。 To get this to work you need to install the code inside a supporting container - such as Karaf of Servicemix. 要使其工作,您需要在支持容器中安装代码 - 例如Servicefix的Karaf。

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. 如果您希望OSGi使用Camel,请访问https://github.com/FuseByExample/smx-bootstraps查看Servicemix Bootstraps项目 - 有关安装和调整代码的完整文档。 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. 您将感兴趣的捆绑包是smx-pongersmx-ponger-service ,它们分别展示了OSGi服务的消费和提供。

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. 我曾经遇到过这样的情况,我在我的驼峰路线中有OSGi依赖组件,我想通过像Eclipse这样的IDE运行/调试。

If you are looking to debug while you develop, you can deploy to ServiceMix and remotely debug: 如果您希望在开发时进行调试,可以部署到ServiceMix并进行远程调试:

http://servicemix.apache.org/developers/remote-debugging-servicemix-in-eclipse.html 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: Camel 2.10可能会支持OSGi蓝图开箱即用的场景:

http://camel.apache.org/camel-run-maven-goal.html 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. Spring OSGI扩展很好,但正如您所看到的,当您从相同的spring上下文实现和声明bean时,测试服务接口有点乱。 You could of course have bean reference helloBean, but that defeats the purpose. 您当然可以使用bean引用helloBean,但这会破坏目的。

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. 我不确定spring-osgi扩展行为,但至少使用与pojosr非常相似的camel-blueprint,可以使用修改后的helloService元素进行相同的测试。

<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. 注意一个不寻常的事实,即bean id通常引用bean id,你现在正在使用完全限定的接口。

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. 在这种情况下,一种替代方法是实际使用CamelContext的bundleContext属性并使用编程API。 But of course we would like to avoid that in favor of declarative approaches. 但我们当然希望避免使用声明性方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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