来自一个bundle的Spring bean在另一个bundle中的camel路由中是不可访问的

时间:2016-06-21 09:57:57

标签: apache-camel osgi

我有一个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>

我有另一个包含camel路由的osgi包(connection_impl.jar)。我想使用&#34; amqtx&#34; bean作为camel端点中的组件。 以下是我在camel-context.xml中访问&#34; amqtx&#34;

的代码
<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 bundle 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组件

2 个答案:

答案 0 :(得分:2)

不确定是否可以完成这项工作,但绝对不建议在另一个包中导入spring xml。

最好将bean作为服务导出到第一个包中,并在第二个包中将其作为服务引用。这也将以更好的方式解耦您的捆绑。

答案 1 :(得分:2)

我同意Christian的观点。当您使用OSGI时,通常会隔离隔离区,除非您为另一个bundle使用的库显式设置导出,或者您正在公开OSGI服务。如果您想设置OSGI服务,我的建议是以Spring Framework的方式进行,因为它相对干净。您可以通过界面公开组件。我将在下面展示一个例子。

通常我喜欢有3个捆绑管理这个。我通常有一个包只包含我将在不同的包之间公开的接口。 Bundle,它是接口的实现,以及利用接口注入的实现来完成任务的bundle。

//接口捆绑包将公开一个接口和任何相关的Pojos。例如:

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

//实现包将根据您的接口中的实现bean注册一个OSGI服务(是的,以前的bundle是依赖OSGI连接)

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

// camel context

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

//好的,最后Process希望利用OSGI服务

public Class AlternateFacilityLookup {
    private AlternateFacilityApi apiLookupBean;

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

    //excluded setters and getters
}

// camel context

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