How to use the INotificationService API in SAP Portal 7.30
In this blog I would like to explain and demonstrate how to use the PRT Notification Service API – INotificationService.
(Java doc link: http://help.sap.com/javadocs/NW73/SPS07/EP/com/sapportals/portal/prt/service/notification/INotificationService.html)
In any Notification Service scenario, there are two types of players: Publisher and Listener.
Where there is only one publisher, there can me multiple listeners.
A. Publisher
This component publishes an event.
The publisher component sends events to listeners. You can attach a data container to the message to pass on parameters (Using the TopicDataContainer object).
In order to publish, you can use either one of the following methods:
1) Publish – Publishes a message to all the listeners: local listeners and cluster listeners that registered for the given topic. This method is a-synchronic.
void publish(String topic,
TopicDataContainer container)
2) PublishAndWait – Publishes a message to a specific listener with a know Id. This method is synchronic.
TopicDataContainer publishAndWait(int id,
String topic,
TopicDataContainer container)
** tip: you can use IClusterInformation service in order to retrieve the Id of a specific server node.
(Java doc link:
3) BroadcastAndWait – Publishes a message to all the listeners and wait for the response.
You can attach a data container to the message to pass on parameters. This method is synchronic.
TopicDataContainer[] broadcastAndWait(String topic,
TopicDataContainer container)
B. Listener
This component waits to be notified that a message with a topic it has subscribed to has been published. The topic is represented by a name.
The listener has to implement the ‘ITopicListener’ or ‘ITopicActionListener’ interface of the Notification service API.
- In case you want to use the ‘Publish’ method – you need to implement the ‘ITopicListener’ interface.
- In case you want to use the ‘PublishAndWait’ or ‘BroadcastAndWait’ methods – you need to implement the ‘ITopicActionListener’ interface.
In order to implement the listener:
1) The listener has to subscribe to the required topic. For subscribing to a topic you need to use the following method:
Subscribe – Enable an ITopicListener to register for all messages that correspond to the topic
void subscribe(String topic,
ITopicListener listener) or
void subscribe(String topic,
ITopicActionListener listener)
** tip: In case of cluster with multiple nodes, In order that all the nodes will be notified, each node has to subscribe separately. The notification will be sent only to the nodes which are subscribed. In case you want all the nodes in your cluster to be notified, you need to trigger the subscribe method from each one of them separately.
2) The ‘handleTopic’ method will be triggered by a publish invocation. You need to implement one of these methods:
- For ‘ITopicListener’ Interface use:
void handleTopic(TopicDataContainer topicData)
- For ‘ITopicActionListener’ interface use:
TopicDataContainer
handleTopic(TopicDataContainer topicData)
3) In order to stop being notified about a particular topic use the following method:
void unsubscribe(String topic,
ITopicListener listener) or
void unsubscribe(String topic,
ITopicActionListener listener)
C. Code Sample
1) Publisher –
public class Publisher
{
public INotificationService notification;
public void doPublish()
{
TopicDataContainer container = new TopicDataContainer(“TopicName1”);
INotificationService notification =
(INotificationService) PortalRuntime.getRuntimeResources().getService(INotificationService.KEY);
container.addTopicData(“Topic2”, “Topic2Str”);
// 3 options to publish – choose one of them //
//*****************************************************************************
//——————————–
// // 1
// Publish – a-synchronic //
// //
// ——————————-
notification.publish(“TopicName1”,container);
//*****************************************************************************
//*****************************************************************************
//——————————-
// // 2
// PublishAndWait – synchronic //
// //
//——————————-
// Get the ID of the relevant node, where is executed this component
IClusterInformation clusterContext =
(IClusterInformation) PortalRuntime.getRuntimeResources().getService(IClusterInformation.KEY);
int clusterId = clusterContext.getNodeId();
TopicDataContainer resultContainer = notification.publishAndWait(clusterId, “TopicName1”,container);
//*****************************************************************************
//*****************************************************************************
//———————————-
// // 3
// BroadcastAndWait – synchronic //
// //
//——————————-—
TopicDataContainer[] resultContainer = notification.broadcastAndWait(“TopicName1”, container);
//*****************************************************************************
2) Subscriber –
public class Subscriber implements ITopicActionListener
{
public void eventSubscription ()
{
INotificationService notification =
(INotificationService) PortalRuntime.getRuntimeResources().getService(INotificationService.KEY);
notification.subscribe(“TopicName1”,this);
}
public void eventUnSubscription ()
{
INotificationService notification =
(INotificationService) PortalRuntime.getRuntimeResources().getService(INotificationService.KEY);
notification.unsubscribe(“TopicName1”, this);
}
// When using publishAndWait or broadcastAndWait
public TopicDataContainer handleTopic(TopicDataContainer topicdatacontainer)
{
// Do what you need to do
String cont = topicdatacontainer.toString(); // for example
return topicdatacontainer;
}
}
Or
public class Subscriber implements ITopicListener
{
public void eventSubscription ()
{
INotificationService notification =
(INotificationService) PortalRuntime.getRuntimeResources().getService(INotificationService.KEY);
notification.subscribe(“TopicName1”,this);
}
public void eventUnSubscription ()
{
INotificationService notification =
(INotificationService) PortalRuntime.getRuntimeResources().getService(INotificationService.KEY);
notification.unsubscribe(“TopicName1”, this);
}
// When using publish
public void handleTopic(TopicDataContainer container)
{
// Do what you need to do
String cont = topicdatacontainer.toString(); // for example
}
}