簡體   English   中英

一個捆綁包中的Spring Bean無法在另一個捆綁包中的駱駝路線中訪問

[英]Spring bean from one bundle is not accessible in camel route in another bundle

我有一個jar文件(connection.jar),這是一個OSGI捆綁軟件,文件夾結構為src / main / resources / META-INF / spring / mq-connect.xml,以下連接代碼位於mq-connect.xml中

<beans ...... >
    ....
    ....
    ....
    ....

    <!-- AMQ jms component -->
    <bean id="amqtx" class="org.apache.activemq.camel.component.ActiveMQComponent">
        <property name="connectionFactory" ref="amqPooledConnectFactory" />
        ...
        ...
        ...
        <property name="maxConcurrentConsumers" value="${concurrent.consumers}" />
    </bean>
</beans>

我還有另一個包含駱駝路線的osgi捆綁包(connection_impl.jar)。 我想使用“ amqtx” bean作為駱駝端點中的組件。 以下是我放入camel-context.xml中以訪問“ amqtx”的代碼

<beans ...... >
    <import resource="classpath*:mq-connect.xml"/>
        ...
        ...
        ...
        ...
    <camelContext trace="true" streamCache="true" autoStartup="true" xmlns="http://camel.apache.org/schema/spring">
             <route customId="true" id="TEST_AMQ">
                    <description>Test connectivity from another file</description>
                    <from uri="amqtx:INBOUND.QUEUE" />
                    <transacted ref="AMQ_PROPAGATION_REQUIRED" />
                    <to uri="OUTBOUND.QUEUE" />
                    <log message="Route completed successfully" />
                </route>
    </camelContext> 
</beans>

我正在使用jboss-a-mq-6.2.1並將其部署到Karaf容器。 OSGi捆綁包connection.jar部署成功,但是connection_impl部署失敗。 它在A-MQ日志中給出以下錯誤

Stack Trace:  
org.apache.camel.RuntimeCamelException: org.apache.camel.FailedToCreateRouteException: Failed to create route TEST_AMQ: 
Route(TEST_AMQ)[[From[amqtx:INBOUND.QUEUE]] -> ... because of Failed to resolve endpoint: amqtx://INBOUND.QUEUE due to: No component found with scheme: amqtx

我以各種方式嘗試了該部分,但仍然無法在另一個捆綁包中訪問amqtx。

請建議任何缺少的導入,導出/錯誤配置,以便在connection_impl.jar中可以訪問connection.jar中的amqtx組件

不知道您是否可以完全做到這一點,但是絕對不建議在另一個包中導入spring xml。

最好在第一個捆綁軟件中將bean導出為服務,然后在第二個捆綁軟件中將其引用為服務。 這也將以更好的方式解耦您的捆綁包。

我同意克里斯蒂安。 使用OSGI包時,通常是隔離的,除非您為另一個包使用的庫顯式設置導出,或者要公開OSGI服務。 如果您想設置一個OSGI服務,我的建議是使用Spring Framework,因為它相對干凈。 您可以通過接口公開您的組件。 我將在下面顯示一個示例。

通常,我喜歡由3個捆綁軟件來管理。 我通常有一個包,其中僅包含將在單獨的包之間公開的接口。 捆綁包是接口的實現,捆綁包是利用接口的注入實現來完成任務的包。

//接口捆綁將公開一個接口和任何關聯的Pojos。 例如:

public interface AlternateFacilityApi {
    public String getAlternateFacility(String facilityName);
}

//實現捆綁包將基於您接口的實現bean注冊OSGI服務(是,以前的捆綁包是OSGI關聯的依賴項)

public Class MyAlternateFacilityImpl implements AlternateFacilityApi {
    public String getAlternateFacility(String facilityName) {
        return "Bob's house";
    }

//駱駝語境

<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"
    xmlns:ctx="http://www.springframework.org/schema/context"
    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">

    <osgi:service id="AlternateFacilityService" ref="alternateFacilityService" interface="com.world.api.AlternateFacilityApi" />

    <bean id="alternateFacilityService" class="com.world.service.MyAlternateFacilityImpl" 
/>
</beans>

//確定,最后該流程希望利用OSGI服務

public Class AlternateFacilityLookup {
    private AlternateFacilityApi apiLookupBean;

    public String getMyAlternateFacility(String facilityName) {
        return apiLookupBean.getAlternateFacility(facilityName);
    }

    //excluded setters and getters
}

//駱駝語境

<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"
    xmlns:ctx="http://www.springframework.org/schema/context"
    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">

    <osgi:reference id="AlternateFacilityLookupService" interface="com.world.api.AlternateFacilityApi" />

    <bean id="MyAlternateFacilityBean" class="com.awesome.AlternateFacilityLookup">
        <property name="apiLookupBean" ref="AlternateFacilityLookupService" />
    </bean>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM