【问题标题】:Camel route not able to find bean defined in other blueprint xml file(other bundle)骆驼路线无法找到在其他蓝图 xml 文件(其他捆绑包)中定义的 bean
【发布时间】:2017-08-05 02:30:16
【问题描述】:

我们正在使用 Blueprint + Camel + Karaf ,从 Spring 迁移。 我是 OSgi Blueprint 的新手。我们正在使用 Blueprint XML 从 blueprint xml 中定义的 bean 定义服务。

在 Blueprint XML 中添加 Service 后,至少从 karaf 获取如下: 仅供参考:捆绑处于活动状态

  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"/>

请帮助我如何在 APPConfig(在 karaf 根目录下)文件夹中的 Routes XML 文件中访问此 bean。

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

【问题讨论】:

    标签: java apache-camel osgi-bundle blueprint-osgi


    【解决方案1】:

    为了将您的 OSGi 服务导入到您的包中,您可以执行以下操作:

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

    【讨论】:

    • 在我尝试你的之前,我已经阅读了我上面提到的 redhat 论坛。在我的 routes.xml 中,我如何使用无效的
    • 如果您确实在使用 Blueprint(而不是 Spring),则应该可以使用参考元素。也许您缺少命名空间?
    【解决方案2】:

    看起来你在混合东西:

    • 用于导出服务的蓝图文件
    • camel xml 路由定义

    您还应该为您的路由定义使用蓝图,这样您就有机会使用所有蓝图/osgi 东西(配置管理、服务引用等)。

    【讨论】:

    • 如果我使用蓝图进行路由定义意味着路由上下文正确。如果我使用路由上下文,我无法动态加载到骆驼上下文中。
    【解决方案3】:

    这是一个关于如何做的简单示例。您可以发布接口的不同实现,并让捆绑包要求最适合的实现。
    首先让我们了解需求:

    ExportBundle 必须:

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

    ImportBundle 必须:

    • 导入包含PaymentProcessor接口的包
    • 要求 OSGi 注入所需的服务,这是接口的实现
    • 在 bean 中保留对此类服务的引用,并在 Camel 上下文中使用它

    所有包的导入导出通常由ma​​ven-bundle-plugin配置。大多数时候它得到的就足够了。在 Karaf 控制台中,使用headers &lt;bundleid&gt; 检查存在哪些服务和包导入/导出。

    使用蓝图导出 OSGi 服务:

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

    使用 Blueprint 导入 OSGi 服务并在 Camel 中使用:

    <blueprint>
        <reference id="customPaymentProcessor" interface="my.app.PaymentProcessor" />
    
        <camelContext>
            <route>
                <from uri="direct:start" />
                <to uri="bean:customPaymentProcessor" />
            </route>
        </camelContext>
    </blueprint>
    

    注意:bean 在蓝图上下文之间不共享。但是,在单个捆绑包中,您可以根据需要在 OSGI-INF/blueprint 文件夹中拥有任意数量的 .xml 文件。在这种情况下,bean 共享的,因为所有文件都同意构建相同的蓝图上下文。更有趣的是,如果您在多个文件中定义 CamelContexts,您将获得不同的上下文。
    在更大的包中,我通常定义一个 beans.xml 来配置 bean 和服务,并定义一个 routes.xml 来定义使用“其他”文件中的 bean 的 Camel 路由。

    工作示例

    我已经在做一个类似的项目,请随时查看on my GitHub 的示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 2012-05-04
      • 2023-04-02
      • 2021-11-04
      • 2014-11-02
      相关资源
      最近更新 更多