Example: Registering a WS-Notification publisher
Use this task to write the code for a subscriber client application that can subscribe a consumer application with a broker, based on the example code extract provided.
About this task
這個範例的基礎如下:搭配 WSDL2Java 工具產生的程式碼(針對建立 WS-Notification 服務點所產生的「通知分配管理系統 WSDL」來執行這個工具)及 WebSphere® Application Server API 和 SPI,來使用 Java™ API for XML 型遠端程序呼叫 (JAX-RPC) API。
In WebSphere Application Server there are two implementations
of the WS-Notification service: Version 6.1 and Version 7.0. 這個 JAX-RPC 範例可以與 6.1 版或 7.0 版 WS-Notification 服務點順利互動。 不過,如果您想要搭配原則集來使用 WS-Notification,例如,能夠與 WS-ReliableMessaging 組合起來,您的 WS-Notification 應用程式就必須編碼為使用 Java API for XML 型 Web 服務 (JAX-WS) 程式設計模型,且必須與 7.0 版 WS-Notification 服務點互動。 如果您還不熟悉如何撰寫 JAX-WS 用戶端應用程式,請參閱下列主題:
To write the code for a subscriber client application that can subscribe a consumer application with a broker, complete the following steps, referring to the example code extract for further information.
Procedure
Example
The following example code represents a JAX-RPC client acting in the publisher registration role, registering a publisher (producer) application with a broker.
// 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 a reference for the publisher (producer) being registered. This contains the address of the
// producer web service.
EndpointReference publisherEPR =
EndpointReferenceManager.createEndpointReference(new URI("http://myserver.mysom.com:9080/Producer"));
// Create a list (array) of topic expressions to describe the topics to which the producer publishes
// messages. For this example you add one topic
Map prefixMappings = new HashMap();
prefixMappings.put("abc", "uri:mytopicns");
TopicExpression topic =
new TopicExpression(TopicExpression.SIMPLE_TOPIC_EXPRESSION, "abc:xyz", prefixMappings);
TopicExpression[] topics = new TopicExpression[] {topic};
// Indicate that you do not want the publisher to use demand based publishing
Boolean demand = Boolean.FALSE;
// Set a value for the initial termination time of the registration. For example, set a value 1 year in
// the future
Calendar initialTerminationTime = Calendar.getInstance();
initialTerminationTime.add(Calendar.YEAR, 1);
// Create holders to hold the multiple values returned from the broker:
// PublisherRegistrationReference: An endpoint reference for use in lifetime management of
// the registration
EndpointReferenceTypeHolder pubRegMgrEPR = new EndpointReferenceTypeHolder();
// ConsumerReference: An endpoint reference for use in subsequent publishing of messages
EndpointReferenceTypeHolder consEPR = new EndpointReferenceTypeHolder();
// Invoke the RegisterPublisher operation by calling the associated method on the stub
stub.registerPublisher(publisherEPR, topics, demand, initialTerminationTime, null, pubRegMgrEPR, consEPR);
// Retrieve the PublisherRegistrationReference
EndpointReference registrationEPR = pubRegMgrEPR.value;
// Retrieve the ConsumerReference
EndpointReference consumerReferenceEPR = consEPR.value;