OSGI中的多个CXF捆绑包绑定到同一地址

时间:2018-03-26 12:47:01

标签: spring osgi cxf apache-karaf karaf

我有多个暴露CXF JAXRS端点的WAR。他们有类似的web.xml

<web-app id="app1">
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:META-INF/spring/*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>app1</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>app1</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

他们共享常见的Spring配置(名为common-rest.xml

<beans>
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <context:annotation-config />  

    <bean id="httpDestinationRegistry"
    class="org.apache.cxf.transport.http.DestinationRegistryImpl" />

    <bean id="baseJaxRSServer"
        abstract="true"
        lazy-init="false"
        class="org.apache.cxf.jaxrs.spring.JAXRSServerFactoryBeanDefinitionParser.SpringJAXRSServerFactoryBean"
        init-method="create"
        p:address="${http.server}/" />
</beans>

每个bean都有类似的配置

<beans>
    <import resource="classpath:META-INF/app/common-rest.xml" />

    <bean id="app1JaxRSServer"
        parent="baseJaxRSServer"
        p:serviceBeans-ref="app1ServiceBeans" />
</beans>

在每个捆绑的“清单”中定义了精确路径

Web-ContextPath: app1

问题是我不能让多个捆绑包一起工作。使用单个捆绑包它可以正常工作,但如果我尝试再运行另一个,我会因创建app1JaxRSServer bean而异常

org.apache.cxf.service.factory.ServiceConstructionException: There is an endpoint already running on /.

使用Karaf 4.0.9,CXF 3.1.13,Spring 3.2.18

2 个答案:

答案 0 :(得分:0)

原因是

 <url-pattern>/*</url-pattern> 

如果您有多个具有此设置的捆绑包,则其端点将发生冲突。

尝试每个捆绑包使用不同的前缀。

答案 1 :(得分:0)

我找到了解决方案,DestionationRegistry存在问题,默认情况下是由CXF HTTP Transport OSGI Bundle(注册默认/ cxf / *端点的那个)创建的。我必须通过设置

来禁用此捆绑包
  

org.apache.cxf.osgi.http.transport.disable = true

在Karaf物业