Management Level Event Notifications
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 Event notifications.
Prerequisites
- 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 the NCache Management Center.
- For API details, refer to: ICache, CacheItem, MemberJoined, MemberLeft, CacheStopped, CacheCleared.
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 a method that has the same signature as the CacheClearedCallback. The following method must be implemented in the application to perform the desired processing when the cache-cleared notification is fired.
// Register cache cleared event
// OnCacheCleared callback will be triggered on cache clear event
cache.NotificationService.CacheCleared += OnCacheCleared;
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 into the application:
// Perform the tasks after getting the cache clear event
Console.WriteLine("Cache has been cleared.");
If you do not want to receive any notifications for the cache clear event, add the following line in your application:
// UnRegister cache cleared event
cache.NotificationService.CacheCleared -= OnCacheCleared;
Cache Stopped Event
Similar to the cache cleared event, you can also receive notifications when the cache is stopped. The following method must be implemented in the application to perform processing when the cache stopped is fired.
// Register cache stopped event
// OnCacheStopped callback will be triggered when cache is stopped
cache.NotificationService.CacheStopped += OnCacheStopped;
To receive the cache-stopped event, the following line of code must be incorporated into the application:
// Perform the tasks after getting the cache stopped event
Console.WriteLine($"'{cacheName}' has been stopped.");
If you do not want to receive any notifications for cache-stopped events, add the following line in your application:
// UnRegister cache stopped event
cache.NotificationService.CacheStopped -= OnCacheStopped;
Member Joined Event
The user can also get notified, whenever a member joins a cluster. The following method must be implemented in the application to perform processing when a member joins a cluster.
// Perform task after Member Joined event gets fired
Console.WriteLine($"Node with IP:{nodeInfo.IpAddress} has joined the cluster.");
To receive the member joined event, the following line of code must be incorporated into the application:
// Register memebr join event
// OnMemeberJoined callback will be triggered when a new member joins cache
cache.NotificationService.MemberJoined += OnMemberJoined;
If you do not want to receive any notifications for member-joined events, add the following line to your application:
Member Left Event
The user can get notified whenever a member leaves a cluster. The following method must be implemented in the application to perform processing when a member leaves a cluster.
// Register memebr left event
// OnMemeberleft callback will be triggered when a member leaves cache
cache.NotificationService.MemberLeft += OnMemberLeft;
To receive the member left event, the following line of code must be incorporated into the application:
// Perform task after Member Left event gets fired
Console.WriteLine($"Node with IP:{nodeInfo.IpAddress} has left the cluster.");
If you do not want to receive any notifications for member left events, add the following line in your application:
Additional Resources
NCache provides a sample application for Management Level Event Notifications on GitHub.
See Also
.NET: Alachisoft.NCache.Runtime.Events namespace.
Java: com.alachisoft.ncache.events namespace.