示例:预订 WS-Notification 使用者

使用此任务为充当发布者注册角色的 JAX-RPC 客户机编写代码(根据提供的示例代码摘录),使用代理注册发布者(生产者)应用程序。

关于此任务

本示例的依据是,将 Java™ API for XML-based remote procedure calls (JAX-RPC) API 与使用 WSDL2Java 工具生成的代码(此代码针对创建 WS-Notification 服务点时生成的通知代理 WSDL 运行)以及 WebSphere® Application Server API 和 SPI 配合使用。

WebSphere Application Server 中,WS-Notification 服务有两个实现:V6.1 和 V7.0。此 JAX-RPC 示例可与 V6.1 或 V7.0 WS-Notification 服务点成功交互。 但是,如果要将 WS-Notification 与策略集配合使用(例如,启用与 WS-ReliableMessaging 的组合),那么 WS-Notification 应用程序必须编码为使用 Java API for XML-based Web Services (JAX-WS) 编程模型,并且必须与 V7.0 WS-Notification 服务点交互。 如果您不熟悉 JAX-WS 客户机应用程序编程,请参阅下列主题: 为 WS-Notification 编写 JAX-WS 应用程序一文包括 JAX-WS 订户客户机应用程序的示例。
原始预订:

在本示例中,第一个可选代码块说明如何创建原始预订Web Services Base Notification 规范的 4.2 节对此概念作了定义。

正常情况下,包装的预订将导致匹配的事件通知可用时驱动 NotificationConsumer 的 Notify 操作。如果 Subscriber 创建了原始预订,那么只将事件通知中特定于应用程序的内容(即 NotificationMessage 元素的内容)发送至目标使用者端点。注意,由于不使用 Notify 操作来传递事件通知,因此,同时指定了 UseRaw 的 Subscribe 请求(即原始预订请求)的 ConsumerReference 中指定的 Web Service 端点不必实现 NotificationConsumer 端口类型。

这意味着,使用者必须能够接受所指定主题上将要发布的每种类型应用程序内容的操作。此模式允许 WS-Notification 调用一组不支持 WS-Notification 但希望接收同一信息的现有 Web Service 应用程序。

JAX-WS 支持基于操作的派遣,并且 JAX-WS 原始使用者应用程序必须接受 http://docs.oasis-open.org/wsn/bw-2/NotificationConsumer/Notify 操作 URI。有关更多信息,请参阅故障诊断技巧作为代理型通知的原始使用者的 JAX-WS 应用程序必须识别通知代理 SOAP 操作

要为充当发布者注册角色的 JAX-RPC 客户机编写使用代理注册发布者(生产者)应用程序的代码,请完成以下步骤(参阅示例代码摘录以了解进一步信息)。

过程

  1. 查找 JAX-RPC 服务。 JNDI 名称特定于您的 Web Service 客户机信息。
  2. 获取要对其调用操作的端口的存根。
  3. 创建 ConsumerReference。 它将包含要预订的用户 Web Service 的地址或拉出点的引用。指定拉出点 EPR 会指示用户将使用基于拉出的通知。
  4. 创建过滤器。 它提供用户要预订的主题的名称。
  5. 创建主题表达式并将其添加至过滤器。 prefixMappings 是名称空间前缀与它们对应的表达式中使用的前缀的名称空间之间的映射。
  6. 创建 XPath 1.0 消息内容过滤器。 例如,可根据薪水级别在主题中选择一部分可用消息。有关消息内容过滤器用法的示例,请参阅过滤发布的消息内容
  7. 创建 InitialTerminationTime。 这是您希望预订终止的时间。 例如,可将时间设置为未来 1 年。
  8. 创建策略信息。
  9. 可选: 构造策略以指示用户要接收原始样式通知。
  10. 创建占位符以保存从代理返回的多个值:
    • 预订引用
    • 代理上的当前时间
    • 预订的终止时间
    • 任何其他元素
  11. 通过对存根调用关联方法来调用预订操作。
  12. 获取返回的值:
    • 已创建的预订的端点引用。这是预订的后续生存期管理(例如,暂停预订)所需的。
    • 代理上的当前时间。
    • 预订的终止时间。
    • 任何其他信息。

示例

以下示例代码描述可通过代理预订使用者应用程序的订户客户机应用程序:
// Look up the JAX-RPC service. The JNDI name is specific to your web services client implementation
InitialContext context = new InitialContext();
javax.xml.rpc.Service service = (javax.xml.rpc.Service) context.lookup(
    "java:comp/env/services/NotificationBroker");

// Get a stub for the port on which you want to invoke operations
NotificationBroker stub = (NotificationBroker) service.getPort(NotificationBroker.class);

// Create the ConsumerReference. This contains the address of the consumer web service that is being
// subscribed, or alternatively is a reference to a pull point (see alternative below). Specifying a
// pull point EPR indicates that the consumer is to use pull-based notifications.
EndpointReference consumerEPR = 
    EndpointReferenceManager.createEndpointReference(new URI("http://myserver.mycom.com:9080/Consumer"));

/*
// Alternative ConsumerReference for pull-based notifications:

EndpointReference consumerEPR = pullPointEPR;

*/

// Create the Filter. This provides the name of the topic to which you want to subscribe the consumer
Filter filter = new Filter();

// Create a topic expression and add it to the filter. The prefixMappings are mappings between namespace
// prefixes and their corresponding namespaces for prefixes used in the expression
Map prefixMappings = new HashMap();
prefixMappings.put("abc", "uri:example");
TopicExpression exp =
    new TopicExpression(TopicExpression.SIMPLE_TOPIC_EXPRESSION, "abc:ExampleTopic", prefixMappings);        
filter.addTopicExpression(exp);


//Create an XPath 1.0 message content filter
//This example selects a subset of the available messages in the topic, based upon salary level
String filterExpression = "/company/department/employee/salary > 10000";
URI xpathURI = new URI(http://www.w3.org/TR/1999/REC-xpath-19991116);

QueryExpression qexp = 
    new QueryExpression(xpathURI, filterExpression);

filter.addMessageContentExpression(qexp);

// Create the InitialTerminationTime. This is the time when you want the subscription to terminate.
// For example, set a time of 1 year in the future.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, 1);
AbsoluteOrRelativeTime initialTerminationTime = new AbsoluteOrRelativeTime(cal);

// Create the Policy information
SOAPElement[] policyElements = null;

/*
Optional
--------
The following lines show how to construct a policy indicating that the consumer is to 
receive raw style notifications:

    javax.xml.soap.SOAPFactory soapFactory = javax.xml.soap.SOAPFactory.newInstance();
    SOAPElement useRawElement = null;

    if (soapFactory instanceof IBMSOAPFactory) {
        // You can use the value add methods provided by the IBMSOAPFactory API to create the SOAPElement
        // from an XML string.
       String useRawElementXML = "<mno:UseRaw xmlns:mno=\"http://docs.oasis-open.org/wsn/b-2\"/>";
        useRawElement = ((IBMSOAPFactory) soapFactory).createElementFromXMLString(useRawElementXML);
    } else {
        useRawElement = soapFactory.createElement("UseRaw", "mno", "http://docs.oasis-open.org/wsn/b-2");
    }

    policyElements =  new SOAPElement[] { useRawElement };
*/

// Create holders to hold the multiple values returned from the broker:
// The subscription reference
EndpointReferenceTypeHolder subscriptionRefHolder = new EndpointReferenceTypeHolder();

// The current time at the broker
CalendarHolder currentTimeHolder = new CalendarHolder();

// The termination time for the subscription
CalendarHolder terminationTimeHolder = new CalendarHolder();

// Any additional elements
AnyArrayHolder anyOtherElements = new AnyArrayHolder();

// Invoke the Subscribe operation by calling the associated method on the stub
stub.subscribe(consumerEPR,
                       filter,
                       initialTerminationTime,
                       policyElements,
                       anyOtherElements,
                       subscriptionRefHolder,
                       currentTimeHolder,
                       terminationTimeHolder);

// Get the returned values:
// An endpoint reference for the subscription that has been created. It is required for
// subsequent lifetime management of the subscription, for example pausing the subscription
com.ibm.websphere.wsaddressing.EndpointReference subscriptionRef = subscriptionRefHolder.value;

// The current time at the broker
Calendar currentTime = currentTimeHolder.value;

// The termination time of the subscription
Calendar terminationTime = terminationTimeHolder.value;

// Any other information
SOAPElement[] otherElements = anyOtherElements.value;

指示主题类型的图标 任务主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=tjwsn_ex_sub
文件名:tjwsn_ex_sub.html