簡體   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