!--a11y-->
Notification Service
Example 
These are examples of code to demonstrate the usage of the PRT Notification Service API.
In any Notification Service scenario, there are two components:
· publisher
This is a component that publishes an event. The publisher component sends events by using the publish() method or the publishAndWait() method to notify receivers that an event has occurred. When publishing an event, the publisher can associate data to this event using a TopicDataContainer object.

void publish( String topicName, TopicDataContainer) TopicDataContainer publishAndWait( String nodeID, String topicName, TopicDataContainer) |
· listener
This is a component that subscribes to an event so that it can be notified when an event of this type occurs. The listener component needs to implement the ITopicListener interface of the Notification service API. An event is always associated to a topic represented by a name.
...
a. The listener subscribes to an event using the subscribe(String topicName, ITopicListener) method of the Notification Service.
b. The event is received in the handleTopic() method of the ITopicListener - void handleTopic(TopicDataContainer) triggered by a published invocation.
c. TopicDataContainer handle(TopicDataContainer) is triggered by a publishAndWait() invocation.
d. The listener must indicate to the Notification Service that it wants to stop so that it can be notified of a particular event by using the unsubscribe method - void unsubscribe(String topicName)
INotificationService notification = (INotificationService) PortalRuntime.getRuntimeResources().getService( INotificationService.KEY); notification.subscribe(Constants.LISTENER_ID, this); |
public void handleTopic(TopicDataContainer value) { // the value received is a container of TopicData for (java.util.Enumeration e = value.getTopicDataKeys(); e.hasMoreElements(); ) { TopicData topicData = value.getTopicDataValue((String) e.nextElement()); String data = topicData.getTopicValue(); } } |
TopicDataContainer container = new TopicDataContainer(Constants.LISTENER_ID);
TopicData[] topics = { // wrap a String object new TopicData(TopicDataContainer.STRING, dateString), // wrap a file object new FileData("c:/log.txt"), // wrap an int new TopicData(TopicDataContainer.INT, String.valueOf(clusterNodeId)), // wrap an InputString new StreamData(new ByteArrayInputStream("Hello World!".getBytes()))};
// Add in TopicDataContainer all topic data; // each topic data must have a unique name for (int i = 0; i < topics.length; i++) { container.addTopicData("name_" + i, topics[i]); }
INotificationService notification = (INotificationService) PortalRuntime.getRuntimeResources().getService( INotificationService.KEY);
/** * The name of the topic is the same name used in the subscribe() method **/
notification.publish( Constants.LISTENER_ID, TopicCreationSample.createTopicDataContainer()); |
public TopicDataContainer handleTopic(TopicDataContainer value) { // in the sample, we only store the TopicDataContainer to display later mm_listTopicDataContainerReceived.add(value);
/** * The publisher expects a responce **/ //it is possible to get the cluster element Id for the Emitter String emitterID = value.getTopicDataValue(ITopicActionListener.ID).getTopicValue();
TopicDataContainer container = new TopicDataContainer("My Response"); container.addTopicData( Constants.RESPONSE_KEY, new TopicData( TopicDataContainer.STRING, "Hi: Response for " + emitterID + "!")); return container; } |
/** * Get a reference on the Cluster Manager Service to get the Node Id * of the cluster. **/ IClusterInformation clusterContext = (IClusterInformation) PortalRuntime.getRuntimeResources().getService( IClusterInformation.KEY);
// Get the ID node, where is executed this component int clusterId = clusterContext.getNodeId();
TopicDataContainer response = notification.publishAndWait( clusterId, Constants.LISTENER_ID, topicDataContainer); |
