Management Level Event Notifications
Note
This feature is only available in NCache Enterprise Edition.
Events can also be registered for management operations on cache. This provides the user with the ease of getting notified when a management operation is performed.
These events are used for validation whether operations should be continued on cache or not. For example, the client is a multi-threaded application where a thread is dedicated to inserting items at a constant rate. If during insertion the cache is stopped, the thread will throw OperationFailedException
. To prevent the exception from being thrown, the thread must take appropriate action upon stopping of cache by registering for event against cache stop action.
This example also applies to cache clear notification if client application’s thread validates operations during its execution while cache is cleared. All validations will fail if client application would not consider clear cache event.
Following management operations when performed fire cache level management events:
Cache Cleared Notification:
Cache Cleared notification is fired when cache is cleared. All applications which have registered this event will receive a notification when the cache is cleared.
Cache Stopped Notification:
Cache Stopped notification is fired when cache is stopped. All applications which have registered this event will receive a notification when the cache is stopped.
Member Joined Notification:
Member Joined notification is fired when a member joins the cluster. All applications which have registered this event will receive a notification when a node joins in the cluster.
Member Left Notification:
Member Left notification is fired when a member leaves the cluster. All applications which have registered this event will receive a notification when a node leaves the cluster.
Pre-Requisites
- Include the following namespaces in your application:
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Events
Alachisoft.NCache.Runtime.Exceptions
- The application must be connected to cache before performing the operation.
- Cache must be running.
- Make sure that the data being added is serializable.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Cache Clear Event
Some cases may occur in which the application must get notified on cache being cleared. For example, cache is cleared and contains no items, whereas the client application keeps fetching the item(s) from the cache. This will result in an exception on performing the Get operation.
In order to avoid situations like these, Cache Cleared event must be used. It notifies the user whenever the cache is cleared.
For clear cache event, user has to implement method which has same signature as of the CacheClearedCallback. Following method must be implemented in application to perform task when cache cleared is fired.
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 into application:
cache.NotificationService.CacheCleared += OnCacheCleared;
- If you do not want to further receive any notifications for cache clear event, add the following line in your application:
cache.NotificationService.CacheCleared -= OnCacheCleared;
Cache Stopped Event
Similar to the cache cleared event, you can also recieve notifications when cache is stopped. If application continues to perform operations when the cache is stopped, without knowing that the cache is stopped, exception will be thrown.
To avoid situations like these, Cache Stopped event must be used. It notifies the user whenever the cache is stopped so that the user handles the application accordingly.
Following method must be implemented in application to perform task when cache cleared is fired.
public void OnCacheStopped(string cacheName)
{
// Perform the tasks after getting the cache stopped event
}
- For receiving cache stopped event, the following line of code must be incorporated into application:
cache.NotificationService.CacheStopped += OnCacheStopped;
- If you do not want to further receive any notifications for cache stopped event, add the following line in your application:
cache.NotificationService.CacheStopped -= OnCacheStopped;
Member Joined/Left Event
The user can also get notified whenever a member joins or leaves a cluster. For example, a cache administrator or a user monitoring the state of cache during operations could automate their tasks for monitoring the state of cluster. Hence, if a member joins or leaves the cluster, the event should be handled to prevent any interruption in the automated tasks.
Following method must be implemented in application to perform task when a member joins or leaves a cluster.
public void OnMemberJoined(NodeInfo nodeInfo)
{
// Perform task after Member Joined event gets fired
}
public void OnMemberLeft(NodeInfo nodeInfo)
{
// Perform task after Member Left event gets fired
}
- For receiving member joined or left event, the following line of code must be incorporated into application:
cache.NotificationService.MemberJoined += OnMemberJoined;
cache.NotificationService.MemberLeft += OnMemberLeft;
- If you do not want to further receive any notifications for member joined or left event, add the following line in your application:
cache.NotificationService.MemberJoined -= OnMemberJoined;
cache.NotificationService.MemberLeft -= OnMemberLeft;
Additional Resources
For a full functioning .NET application executing events, you can use the sample shipped with NCache which is placed at:
- .NET Framework: %NCHOME%\samples\dotnet\Events
- .NET Core: %NCHOME%\samples\dotnetcore\Events
- Java: %NCHOME%\samples\java\Events
See Also
Cache Level Event Notifications
Item Level Event Notifications
Pub/Sub Messaging
Search Cache with LINQ