• 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 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 Manager.
  • For API details, refer to: Cache, CacheDataModificationListener, CacheStatusEventListener, getEventType, removeCacheNotificationListener, CacheStatusNotificationType, MemberJoined, removeCacheStatusEventListener, addCacheStatusEventListener, CacheStopped, addCacheClearedListener, removeCacheClearedListener, getNotificationService.

Cache Clear Event

In some cases the application must get notified when the cache is cleared. For the clear cache event, the user has to implement method which has the same signature as the CacheClearedCallback. The following method must be implemented in the application to perform desired processing when the cache cleared notification is fired.

  • .NET/.NET Core
  • Java
// Register cache cleared event
// OnCacheCleared callback will be triggered on cache clear event
cache.NotificationService.CacheCleared += OnCacheCleared;
public class CacheClearedNotificationListener implements CacheClearedListener 
{
    @Override
    public void onCacheCleared() 
    {
        // Perform the tasks after getting the cache clear event
    }
}
Note

To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.

To receive the cache clear event, the following line of code must be incorporated in the application:

  • .NET/.NET Core
  • Java
// Perform the tasks after getting the cache clear event
Console.WriteLine("Cache has been cleared.");
//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 the cache clear event, add the following line in your application:

  • .NET/.NET Core
  • Java
// UnRegister cache cleared event
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. The following method must be implemented in application to perform processing when cache stopped is fired.

  • .NET/.NET Core
  • Java
// Register cache stopped event
// OnCacheStopped callback will be triggered when cache is stopped
cache.NotificationService.CacheStopped += OnCacheStopped;
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;
        }
    }
}

To receive the cache stopped event, the following line of code must be incorporated into application:

  • .NET/.NET Core
  • Java
// Perform the tasks after getting the cache stopped event
Console.WriteLine($"'{cacheName}' has been stopped.");
//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
// Un-Register cache stopped event
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. The following method must be implemented in application to perform processing when a member joins a cluster.

  • .NET/.NET Core
  • Java
// Perform task after Member Joined event gets fired
Console.WriteLine($"Node with IP:{nodeInfo.IpAddress} has joined the cluster.");
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;
        }
    }
}

To receive the member joined event, the following line of code must be incorporated into application:

  • .NET/.NET Core
  • Java
// Register memebr join event
// OnMemeberJoined callback will be triggered when a new member joins cache
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
// Un-Register memeber join event
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. The following method must be implemented in application to perform processing when a member leaves a cluster.

  • .NET/.NET Core
  • Java
// Register memebr join event
// OnMemeberJoined callback will be triggered when a new member joins cache
cache.NotificationService.MemberLeft += OnMemberLeft;
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;
        }
    }
}

To receive the member left event, the following line of code must be incorporated into application:

  • .NET/.NET Core
  • Java
// Perform task after Member Left event gets fired
Console.WriteLine($"Node with IP:{nodeInfo.IpAddress} has left the cluster.");
//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
// Un-Register memeber left event
cache.NotificationService.MemberLeft -= OnMemberLeft;
//Unregister member left event.
cache.getNotificationService().removeCacheStatusEventListener(memberLeftListener, memberLeftEvent);

Additional Resources

NCache provides a sample application for Management Level Event Notifications 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