繁体   English   中英

在驼峰路线中使用OSGi服务

[英]Using OSGi service in the camel route

我正在阅读“Camel in Action”这本书,我无法在驼峰路线中使用OSGi服务制定一个例子(第4.3.4节OsgiServiceRegistry)。 这是我的bean(暴露为OSGi服务

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

 }
}

这是将上述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>

当我执行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

请让我知道如何设置bundleContext。 我使用eclipse equinox作为OSGi容器。

camel:run只使用项目中的Spring Camel配置运行一个瘦的非OSGi运行时。 您获得的消息来自SpringDM(实例化<osgi:service id="helloService"...> )无法找到OSGi环境。 要使其工作,您需要在支持容器中安装代码 - 例如Servicefix的Karaf。

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

我曾经遇到过这样的情况,我在我的驼峰路线中有OSGi依赖组件,我想通过像Eclipse这样的IDE运行/调试。

如果您希望在开发时进行调试,可以部署到ServiceMix并进行远程调试:

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

Camel 2.10可能会支持OSGi蓝图开箱即用的场景:

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

Spring OSGI扩展很好,但正如您所看到的,当您从相同的spring上下文实现和声明bean时,测试服务接口有点乱。 您当然可以使用bean引用helloBean,但这会破坏目的。

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

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

注意一个不寻常的事实,即bean id通常引用bean id,你现在正在使用完全限定的接口。

当然,这有一些不幸的局限。 如果只有一个服务实例实现了所需的接口,它可以正常工作,但对于如何应用过滤器没有明显的方法(对我而言)。 在这种情况下,一种替代方法是实际使用CamelContext的bundleContext属性并使用编程API。 但我们当然希望避免使用声明性方法。

暂无
暂无

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

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