2017-03-14 22 views
2

我们正在使用Blueprint + Camel + Karaf,从Spring迁移。 我是新的OSgi蓝图。我们使用Blueprint XML来定义blueprint xml中定义的bean的服务。骆驼路线无法找到在其他蓝图xml文件(其他捆绑)中定义的bean

后,我们在蓝图XML增值业务,至少从karaf得到如下: FYI:束处于Active状态

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

我相信bean被注册到OSGi服务。但不知何故,其他Bundle中的其他XML不可见。

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

请帮我如何获得accesss(下karaf根目录)文件夹这个bean在路由XML文件中的AppConfig。

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> 

我从这个裁判明白: https://access.redhat.com/documentation/en-US/Red_Hat_JBoss_Fuse/6.0/html/Deploying_into_the_Container/files/DeploySimple-Publish.html

所有OSGi服务都隐含注册为OSGi regsitry骆驼进行搜索。但我得到; ::

[Bean[ref:cust... because of No bean could be found in the registry for: customPaymentProcessor 

回答

1

为了导入OSGi的服务到你的包,你可以做这样的事情:

<reference id="myService" component-name="customPaymentProcessor" 
    interface="com.rnd.model.impl.PaymentServiceProcessorBase" /> 
+0

在我尝试你的之前,我已经阅读过我上面提到的redhat论坛。在我的routes.xml中,我如何使用这是无效的? – Slok

+0

如果您确实使用蓝图(而不是Spring),则参考元素应该可用。也许你错过了一个命名空间? – noMad17

0

看起来你是混合的东西:

  • 蓝图文件导出您的服务
  • 骆驼xml路由定义

您应该使用蓝图也用于您的路由定义,以便您也有机会使用所有blueprint/osgi的东西(config-admin,服务引用等)。

+0

如果我使用蓝图进行路线定义,则意味着路线上下文正确。如果我使用路由上下文,我不能动态加载到骆驼上下文中。 – Slok

3

这是一个简单的例子,介绍如何做到这一点。你可以发布一个接口的不同实现,并让这些包要求最适合的接口。
首先,让我们了解需求:

ExportBundle必须:

  • 导出包含PaymentProcessor接口
  • 出口接口的实现作为OSGi服务

ImportBundle必须在程序包:

  • 导入包含PaymentProcessor接口封装
  • 问OSGi的注入所需的服务,这是该接口
  • 保持在一个bean这种服务的引用的实现,并使用它骆驼上下文中

所有包的导入和导出通常由配置maven-bundle-plugin。大部分时间都是正确的。在Karaf控制台内部,使用headers <bundleid>来检查哪些服务和软件包的导入/导出存在。

出口OSGi服务使用蓝图:利用蓝图,并用它

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

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

导入OSGi服务的骆驼:

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

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

注意:豆类不蓝图上下文之间共享 。但是,在一个包中,您可以根据需要在OSGI-INF/blueprint文件夹中包含尽可能多的.xml文件。在这种情况下,beans 共享为,因为所有文件都同意构建相同的蓝图上下文。为了增加趣味性,如果将CamelContexts定义在多个文件中,则会获得不同的上下文。
在更大的包中,我通常定义一个beans.xml来配置bean和服务,并定义一个routes.xml来定义使用来自“other”文件的bean的Camel路由。

工作实例

我已经在一个类似的项目工作,随时检查为例进行on my GitHub