• Webinars
  • Docs
  • Download
  • Blogs
  • Contact Us
Try Free
Show / Hide Table of Contents

Management Level Event Notifications

Note

This feature is only available in NCache Enterprise Edition.

Events can be registered for management operations including cache clear, cache stopped, and member joined/left. Here we describe how to register and unregister management level events.

Prerequisites

  • .NET/.NET Core
  • Java
  • To learn about the standard prerequisites required to work with all NCache client side features please refer to the given page on Client Side API Prerequisites.
  • Make sure to enable event notifications using NCache Web Manager.
  • For API details, refer to: ICache, CacheItem, MemberJoined, MemberLeft, CacheStopped, CacheCleared.
  • To learn about the standard prerequisites required to work with all NCache client side features please refer to the given page on Client Side API Prerequisites.
  • Make sure to enable event notifications using NCache Web Manager.
  • For API details, refer to: Cache, CacheDataModificationListener, CacheStatusEventListener, getEventType, removeCacheNotificationListener, CacheStatusNotificationType, MemberJoined, removeCacheStatusEventListener, addCacheStatusEventListener, CacheStopped, addCacheClearedListener, removeCacheClearedListener, getNotificationService.

Cache Clear Event

Some cases may occur in which the application must get notified on cache being cleared. For clear cache event, user has to implement method which has the same signature as CacheClearedCallback. Following method must be implemented in application to perform desired processing when cache cleared is fired.

  • .NET/.NET Core
  • Java
public void OnCacheCleared()
{
    // Perform the tasks after getting the cache clear event
}
public class CacheClearedNotificationListener implements CacheClearedListener 
{
    @Override
    public void onCacheCleared() 
    {
        // Perform the tasks after getting the cache clear event
    }
}

For receiving cache clear event, the following line of code must be incorporated in the application:

  • .NET/.NET Core
  • Java
cache.NotificationService.CacheCleared += OnCacheCleared;
//Create cache cleared listener instance.
CacheClearedListener cacheClearEventListener = new CacheClearedNotificationListener();

//Register cache cleared event.
cache.getNotificationService().addCacheClearedListener(cacheClearEventListener);

If you do not want to further receive any notifications for cache clear event, add the following line in your application:

  • .NET/.NET Core
  • Java
cache.NotificationService.CacheCleared -= OnCacheCleared;
//Unregister cache cleared event.
cache.getNotificationService().removeCacheClearedListener(cacheClearEventListener);

Cache Stopped Event

Similar to the cache cleared event, you can also receive notifications when cache is stopped. Following method must be implemented in application to perform processing when cache stopped is fired.

  • .NET/.NET Core
  • Java
public void OnCacheStopped(string cacheName)
{
    // Perform the tasks after getting the cache stopped event
}
public class CacheStatusChangedNotificationListiner implements CacheStatusEventListener {
    @Override
    public void onCacheStatusChanged(ClusterEvent event) {
        switch (event.getEventType())
        {
            case CacheStopped:
                // Perform the tasks after getting the cache stopped event
                break;
        }
    }
}

For receiving cache stopped event, the following line of code must be incorporated into application:

  • .NET/.NET Core
  • Java
cache.NotificationService.CacheStopped += OnCacheStopped;
//Create cache stopped listener instance.
CacheStatusEventListener cacheStoppedEventListener = new CacheStatusChangedNotificationListiner();

//Register cache stopped event.
EnumSet<CacheStatusNotificationType> cacheStoppedEvent = EnumSet.of(CacheStatusNotificationType.CacheStopped);
cache.getNotificationService().addCacheStatusEventListener(cacheStoppedEventListener, cacheStoppedEvent);

If you do not want to further receive any notifications for cache stopped event, add the following line in your application:

  • .NET/.NET Core
  • Java
cache.NotificationService.CacheStopped -= OnCacheStopped;
//Unregister cache stopped event.
cache.getNotificationService().removeCacheStatusEventListener(cacheStoppedEventListener, cacheStoppedEvent);

Member Joined Event

The user can also get notified whenever a member joins a cluster. Following method must be implemented in application to perform processing when a member joins a cluster.

  • .NET/.NET Core
  • Java
public void OnMemberJoined(NodeInfo nodeInfo)
{
    // Perform task after Member Joined event gets fired
}
public class CacheStatusChangedNotificationListiner implements CacheStatusEventListener {
    @Override
    public void onCacheStatusChanged(ClusterEvent event) {
        switch (event.getEventType())
        {
            case MemberJoined:
                // Perform task after Member Joined event gets fired
                break;
        }
    }
}

For receiving member joined event, the following line of code must be incorporated into application:

  • .NET/.NET Core
  • Java
cache.NotificationService.MemberJoined += OnMemberJoined;
//Create member joined listener instance.
CacheStatusEventListener memberJoinedListener = new CacheStatusChangedNotificationListiner();

//Register member joined event.
EnumSet<CacheStatusNotificationType> memberJoinedEvent = EnumSet.of(CacheStatusNotificationType.MemberJoined);
cache.getNotificationService().addCacheStatusEventListener(memberJoinedListener, memberJoinedEvent);

If you do not want to further receive any notifications for member joined event, add the following line in your application:

  • .NET/.NET Core
  • Java
cache.NotificationService.MemberJoined -= OnMemberJoined;
//Unregister member joined event.
cache.getNotificationService().removeCacheStatusEventListener(memberJoinedListener, memberJoinedEvent);

Member Left Event

The user can get notified whenever a member leaves a cluster. Following method must be implemented in application to perform processing when a member leaves a cluster.

  • .NET/.NET Core
  • Java
public void OnMemberLeft(NodeInfo nodeInfo)
{
    // Perform task after Member Left event gets fired
}
public class CacheStatusChangedNotificationListiner implements CacheStatusEventListener {
    @Override
    public void onCacheStatusChanged(ClusterEvent event) {
        switch (event.getEventType())
        {
            case MemberLeft:
                // Perform task after Member Left event gets fired
                break;
        }
    }
}

For receiving member left event, the following line of code must be incorporated into application:

  • .NET/.NET Core
  • Java
cache.NotificationService.MemberLeft += OnMemberLeft;
//Create member left listener instance.
CacheStatusEventListener memberLeftListener = new CacheStatusChangedNotificationListiner();

//Register member left event.
EnumSet<CacheStatusNotificationType> memberLeftEvent = EnumSet.of(CacheStatusNotificationType.MemberLeft);
cache.getNotificationService().addCacheStatusEventListener(memberLeftListener, memberLeftEvent);

If you do not want to further receive any notifications for member left event, add the following line in your application:

  • .NET/.NET Core
  • Java
cache.NotificationService.MemberLeft -= OnMemberLeft;
//Unregister member left event.
cache.getNotificationService().removeCacheStatusEventListener(memberLeftListener, memberLeftEvent);

Additional Resources

For a full functioning .NET, Java, and Node.js application executing events, you can use the sample shipped with NCache which is placed on GitHub.

See Also

Cache Level Event Notifications
Item Level Event Notifications
Pub/Sub Messaging
Search Cache with LINQ

Back to top Copyright © 2017 Alachisoft