示例:预订 WS-Notification 使用者
使用此任务为充当发布者注册角色的 JAX-RPC 客户机编写代码(根据提供的示例代码摘录),使用代理注册发布者(生产者)应用程序。
关于此任务
本示例的依据是,将 Java™ API for XML-based remote procedure calls (JAX-RPC) API 与使用 WSDL2Java 工具生成的代码(此代码针对创建 WS-Notification 服务点时生成的通知代理 WSDL 运行)以及 WebSphere® Application Server API 和 SPI 配合使用。
- JAX-WS
- JAX-WS 客户机编程模型
- 实现静态 JAX-WS Web Service 客户机
- 为 WS-Notification 编写 JAX-WS 应用程序
- Web Service 提示和技巧:JAX-RPC 与 JAX-WS,第 1 部分
在本示例中,第一个可选代码块说明如何创建原始预订。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 客户机编写使用代理注册发布者(生产者)应用程序的代码,请完成以下步骤(参阅示例代码摘录以了解进一步信息)。
过程
示例
// 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;