Package org.kaaproject.kaa.client.notification

Provides implementation of a notification management.

See: Description

Package org.kaaproject.kaa.client.notification Description

Provides implementation of a notification management.

The Kaa Notifications subsystem enables messages delivery from the Kaa cluster to endpoints (EP). It is based on auto-generated classes according to the topic's notification schema used during SDK generation.

Notification topics can be mandatory or optional. Mandatory topic notifications are delivered in an enforced manner. Optional topics require subscription. It is the responsibility of the client code to register the topic update listener and subscribe to optional topics.

Topics - usage examples

Access to available topics
  KaaDesktop kaa = new KaaDesktop();
  KaaClient kaaClient = kaa.getClient();

  // Kaa initialization (profile container, schema and configuration storages etc.)

  NotificationManager notificationManager = kaaClient.getNotificationManager();
  List<Topic> topics = notificationManager.getTopics();

  for (Topic topic : topics) {
  System.out.printf("Id: %s, name: %s, type: %s"
              , topic.getId(), topic.getName(), topic.getSubscriptionType());
  }
  
  
Topic update subscription

If there is need to know about topic list updates, do following (NotificationTopicListListener):

  notificationManager.addTopicListListener(new NotificationTopicListListener() {
      \@Override
      public void onListUpdated(List<Topic> topics) {
          for (Topic topic : topics) {
              System.out.printf("Id: %s, name: %s, type: %s"
                      , topic.getId(), topic.getName(), topic.getSubscriptionType());
          }
      }
  });
  
  

To remove topic update listener, call NotificationManager.removeTopicListListener(NotificationTopicListListener) with an appropriate listener.

Notifications - usage examples

In order to receive notifications, both mandatory or optional, there should be add an appropriate listener. The listener may be one for all topics. Also there is possibility to add listener for specific topic notifications.

Assume, notification schema has the following form:

  {
          "type": "record",
          "name": "BasicNotification",
          "namespace": "org.kaaproject.kaa.client.example",
          "fields": [
              {
                  "name": "body",
                  "type": "string"
              }
          ]
      }
  
  

As mentioned earlier, there is two kind of topics - mandatory and optional. Further it will be discussed dealing with both of them.

Notification listener(s) for all topics

Below is an example of receiving notifications relating to all topics irrespective whether it is mandatory or optional:

  public class BasicNotificationListener extends AbstractNotificationListener<BasicNotification> {
      \@Override
      public void onNotification(String topicId, BasicNotification notification) {
          System.out.println("Got notification: " + notification.toString());
      }

      \@Override
      protected Class<BasicNotification> getNotificationClass() {
           return BasicNotification.class;
       }
  }

  ...

  BasicNotificationListener listener = new BasicNotificationListener();

  // Add listener
  notificationManager.addNotificationListener(listener);

  ...

  // Remove listener
  notificationManager.removeNotificationListener(listener);
  
  
Notification listener(s) for a specified topic

To add/remove listener(s) to receive notifications relating to the specified topic, do following

  BasicNotificationListener specificListener = new BasicNotificationListener();
  Map<String, List<NotificationListenerInfo>> subscriptions = new HashMap<>();

  // Add listener
  notificationManager.addNotificationListener("some_mandatory_topic_id", listener);

  ...

  // Remove listener
  notificationManager.removeNotificationListener("another_mandatory_topic_id", listener);
  
  
Optional topic (un)subscription

To receive notifications relating to some optional topic, firstly you should to subscribe to this topic:

  BasicNotificationListener listener = new BasicNotificationListener();
  notificationManager.addNotificationListener("optional_topic_id", listener);
  notificationManager.subscribeToTopic("optional_topic_id", true);
  
  

To unsubscribe from some optional topic, do following:

  // All added listeners will be removed automatically
  notificationManager.unsubscribeFromTopic("optional_topic_id", true);
  
  

There is a similar stuff to deal with a group of optional topics - NotificationManager.subscribeToTopics(java.util.List, boolean) and NotificationManager.unsubscribeFromTopics(java.util.List, boolean).

Performance

To increase Kaa performance in case of several subsequent subscription changes and avoid possible race conditions, we recommend to use following approach:

  // Make subscription changes
  notificationManager.subscribeToTopics(Arrays.asList(
          "optional_topic1", "optional_topic2", "optional_topic3"), false);
  notificationManager.unsubscribeFromTopic("optional_topic4", false);

  // Add listeners for optional topics here

  // Commit changes
  notificationManager.sync();
  
  
See Also:
NotificationManager, org.kaaproject.kaa.client.notification.AbstractNotificationListener, NotificationTopicListListener

Copyright © 2015. All rights reserved.