21.3. 启用 WS-RM

概述

要启用可靠的消息传递,必须将 WS-RM 拦截器添加到入站和出站消息和故障的拦截器链中。由于 WS-RM 拦截器使用 WS-Addressing,因此拦截器必须在拦截器链上也存在 WS-Addressing 拦截器。

您可以通过以下两种方式之一确保存在这些拦截器:

  • 显式,使用 Spring beans 将它们添加到分配链中
  • 隐式,使用 WS-Policy 断言,这会导致 Apache CXF 运行时代表您的添加拦截器。

Spring Bean:明确添加拦截器

要启用 WS-RM,将 WS-RM 和 WS-Addressing 拦截器添加到 Apache CXF 总线,或使用 Spring bean 配置将 WS-RM 和 WS-Addressing 拦截器添加到消费者或服务端点。这是在 WS-RM 示例中执行的方法,可在 InstallDir/samples/ws_rm 目录中找到。配置文件 ws-rm.cxf 显示了 WS-RM 和 WS-Addressing 拦截器被添加一站为 Spring Bean (请参阅 例 21.1 “使用 Spring Bean 启用 WS-RM”)。

例 21.1. 使用 Spring Bean 启用 WS-RM

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/
   beans http://www.springframework.org/schema/beans/spring-beans.xsd">
   <bean id="mapAggregator" class="org.apache.cxf.ws.addressing.MAPAggregator"/>
   <bean id="mapCodec" class="org.apache.cxf.ws.addressing.soap.MAPCodec"/>
   <bean id="rmLogicalOut" class="org.apache.cxf.ws.rm.RMOutInterceptor">
        <property name="bus" ref="cxf"/>
   </bean>
   <bean id="rmLogicalIn" class="org.apache.cxf.ws.rm.RMInInterceptor">
        <property name="bus" ref="cxf"/>
   </bean>
   <bean id="rmCodec" class="org.apache.cxf.ws.rm.soap.RMSoapInterceptor"/>
   <bean id="cxf" class="org.apache.cxf.bus.CXFBusImpl">
        <property name="inInterceptors">
            <list>
                <ref bean="mapAggregator"/>
                <ref bean="mapCodec"/>
                <ref bean="rmLogicalIn"/>
                <ref bean="rmCodec"/>
            </list>
        </property>
        <property name="inFaultInterceptors">
            <list>
                <ref bean="mapAggregator"/>
                <ref bean="mapCodec"/>
                <ref bean="rmLogicalIn"/>
                <ref bean="rmCodec"/>
            </list>
        </property>
        <property name="outInterceptors">
            <list>
                <ref bean="mapAggregator"/>
                <ref bean="mapCodec"/>
                <ref bean="rmLogicalOut"/>
                <ref bean="rmCodec"/>
            </list>
        </property>
        <property name="outFaultInterceptors">
            <list>
                <ref bean="mapAggregator">
                <ref bean="mapCodec"/>
                <ref bean="rmLogicalOut"/>
                <ref bean="rmCodec"/>
            </list>
        </property>
    </bean>
</beans>

例 21.1 “使用 Spring Bean 启用 WS-RM” 中显示的代码可以按如下方式进行说明:

Apache CXF 配置文件是一个 Spring XML 文件。您必须包含一个打开 Spring Bean 元素,用于声明由 Bean 元素封装的子元素的命名空间和 schema 文件。

配置每个 WS-Addressing interceptors-MAPAggregatorMAPCodec。有关 WS-Addressing 的更多信息,请参阅 第 20 章 部署 WS-Addressing

配置每个 WS-RM 拦截器-RMOutInterceptorRMInInterceptorRMSoapInterceptor

将 WS-Addressing 和 WS-RM 拦截器添加到用于入站消息的拦截器链。

将 WS-Addressing 和 WS-RM 拦截器添加到用于入站故障的拦截器链中。

将 WS-Addressing 和 WS-RM 拦截器添加到用于出站消息的拦截器链。

将 WS-Addressing 和 WS-RM 拦截器添加到用于出站故障的拦截器链。

WS-Policy 框架:隐式添加拦截器

WS-Policy 框架提供可用于使用 WS-Policy 的基础架构和 API。它符合 2006 年 11 月的草案,发布 Web Services Policy 1.5-FrameworkWeb Services Policy 1.5-Attachment 规格。

要使用 Apache CXF WS-Policy 框架启用 WS-RM,请执行以下操作:

  1. 在您的客户端和服务器端点中添加策略功能。例 21.2 “使用 WS-RM 配置 WS-Policy” 显示 jaxws:feature 元素内嵌套的 bean 引用。reference bean 指定 AddressingPolicy,它定义为同一配置文件中的一个单独的元素。

    例 21.2. 使用 WS-RM 配置 WS-Policy

    <jaxws:client>
        <jaxws:features>
          <ref bean="AddressingPolicy"/>
        </jaxws:features>
    </jaxws:client>
    <wsp:Policy wsu:Id="AddressingPolicy" xmlns:wsam="http://www.w3.org/2007/02/addressing/metadata">
        <wsam:Addressing>
          <wsp:Policy>
            <wsam:NonAnonymousResponses/>
          </wsp:Policy>
        </wsam:Addressing>
    </wsp:Policy>
  2. 将可靠的消息传递策略添加到 wsdl:service 元素 - 或其他 WSDL 元素,这些元素可用作策略或策略引用元素的附件点 - 到 WSDL 文件,如 例 21.3 “在 WSDL 文件中添加 RM 策略” 所示。

    例 21.3. 在 WSDL 文件中添加 RM 策略

    <wsp:Policy wsu:Id="RM"
       xmlns:wsp="http://www.w3.org/2006/07/ws-policy"
       xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <wsam:Addressing xmlns:wsam="http://www.w3.org/2007/02/addressing/metadata">
            <wsp:Policy/>
        </wsam:Addressing>
        <wsrmp:RMAssertion xmlns:wsrmp="http://schemas.xmlsoap.org/ws/2005/02/rm/policy">
            <wsrmp:BaseRetransmissionInterval Milliseconds="10000"/>
        </wsrmp:RMAssertion>
    </wsp:Policy>
    ...
    <wsdl:service name="ReliableGreeterService">
        <wsdl:port binding="tns:GreeterSOAPBinding" name="GreeterPort">
            <soap:address location="http://localhost:9020/SoapContext/GreeterPort"/>
            <wsp:PolicyReference URI="#RM" xmlns:wsp="http://www.w3.org/2006/07/ws-policy"/>
        </wsdl:port>
    </wsdl:service>