管理层事件通知
可以注册事件以进行管理操作,包括缓存清除、缓存停止和成员加入/离开。在这里,我们描述如何注册和注销管理级事件通知。
- 了解与所有人员合作所需的标准先决条件 NCache 客户端功能请参阅给定页面 客户端 API 先决条件.
- 确保 启用事件通知 使用 NCache 管理中心。
- 有关 API 详细信息,请参阅: 缓存, 缓存数据修改监听器, 缓存状态事件监听器, 获取事件类型, 移除缓存通知监听器, 缓存状态通知类型, 会员加入, 移除缓存状态事件监听器, 添加缓存状态事件监听器, 缓存停止, 添加缓存清除监听器, 移除缓存清除监听器, 获取通知服务.
缓存清除事件
在某些情况下,当缓存被清除时,应用程序必须得到通知。 对于清除缓存事件,用户必须实现一个与清除缓存事件具有相同签名的方法 缓存清除回调。 必须在应用程序中实现以下方法,以便在触发缓存清除通知时执行所需的处理。
// Register cache cleared event
// OnCacheCleared callback will be triggered on cache clear event
cache.NotificationService.CacheCleared += OnCacheCleared;
CacheClearedListener clearedListener = new CacheClearedListenerImpl();
// Register cache cleared event
cache.getNotificationService().addCacheClearedListener(clearedListener);
备注
为确保操作是故障安全的,建议处理应用程序中的任何潜在异常,如中所述 处理故障.
要接收缓存清除事件,必须将以下代码行合并到应用程序中:
// Perform the tasks after getting the cache clear event
Console.WriteLine("Cache has been cleared.");
System.out.println("Cache has been cleared..");
如果您不想收到有关缓存清除事件的任何通知,请在应用程序中添加以下行:
// UnRegister cache cleared event
cache.NotificationService.CacheCleared -= OnCacheCleared;
// Unregister cache cleared event
cache.getNotificationService().removeCacheClearedListener(clearedListener);
缓存停止事件
与缓存清除事件类似,您也可以在缓存停止时收到通知。 必须在应用程序中实现以下方法,以便在触发缓存停止时执行处理。
// Register cache stopped event
// OnCacheStopped callback will be triggered when cache is stopped
cache.NotificationService.CacheStopped += OnCacheStopped;
CacheStatusEventListener stoppedListener = new CacheStatusEventListenerImpl();
EnumSet<CacheStatusNotificationType> statusNotificationTypes = EnumSet.of(CacheStatusNotificationType.CacheStopped);
// Register cache stopped event
cache.getNotificationService().addCacheStatusEventListener(stoppedListener, statusNotificationTypes);
System.out.println("Registered for cache stopped notifications.");
要接收缓存停止事件,必须将以下代码行合并到应用程序中:
// Perform the tasks after getting the cache stopped event
Console.WriteLine($"'{cacheName}' has been stopped.");
// Perform the tasks after getting the cache stopped event
System.out.println("'" + cacheName + "' has been stopped.");
如果您不想接收有关缓存停止事件的任何通知,请在应用程序中添加以下行:
// Un-Register cache stopped event
cache.NotificationService.CacheStopped -= OnCacheStopped;
// Un-Register cache stopped event
cache.getNotificationService().removeCacheStatusEventListener(stoppedListener, statusNotificationTypes);
会员参加活动
每当成员加入集群时,用户也可以获得通知。 当成员加入集群时,应用程序中必须实现以下方法来执行处理。
// Perform task after Member Joined event gets fired
Console.WriteLine($"Node with IP:{nodeInfo.IpAddress} has joined the cluster.");
System.out.println("Node with IP:" + nodeInfo.getIpAddress() + " has joined the cluster.");
要接收会员加入事件,必须将以下代码行合并到应用程序中:
// Register memebr join event
// OnMemeberJoined callback will be triggered when a new member joins cache
cache.NotificationService.MemberJoined += OnMemberJoined;
CacheStatusEventListener memberJoinedListener = new CacheStatusEventListenerImpl();
EnumSet<CacheStatusNotificationType> statusNotificationTypes = EnumSet.of(CacheStatusNotificationType.MemberJoined);
// Register member joined event
cache.getNotificationService().addCacheStatusEventListener(memberJoinedListener, statusNotificationTypes);
System.out.println("Registered for member joined notifications.");
如果您不想收到有关会员加入活动的任何通知,请将以下行添加到您的应用程序中:
// Un-Register member join event
cache.NotificationService.MemberJoined -= OnMemberJoined;
cache.getNotificationService().removeCacheStatusEventListener(memberJoinedListener, statusNotificationTypes);
会员离开事件
每当成员离开集群时,用户都会收到通知。 当成员离开集群时,应用程序中必须实现以下方法来执行处理。
// Register memebr left event
// OnMemeberleft callback will be triggered when a member leaves cache
cache.NotificationService.MemberLeft += OnMemberLeft;
CacheStatusEventListener memberLeftListener = new CacheStatusEventListenerImpl();
EnumSet<CacheStatusNotificationType> statusNotificationTypes = EnumSet.of(CacheStatusNotificationType.ALL);
// Register member left event
cache.getNotificationService().addCacheStatusEventListener(memberLeftListener, statusNotificationTypes);
System.out.println("Registered for member left notifications.");
要接收会员离开事件,必须将以下代码行合并到应用程序中:
// Perform task after Member Left event gets fired
Console.WriteLine($"Node with IP:{nodeInfo.IpAddress} has left the cluster.");
// Perform task after Member Left event gets fired
System.out.println("Node with IP:" + nodeInfo.getIpAddress() + " has left the cluster.");
如果您不想收到有关成员离开事件的任何通知,请在您的应用程序中添加以下行:
// Un-Register memeber left event
cache.NotificationService.MemberLeft -= OnMemberLeft;
// Unregister member left event.
cache.getNotificationService().removeCacheStatusEventListener(memberLeftListener, statusNotificationTypes);
更多资讯
NCache 提供管理级别事件通知的示例应用程序 GitHub上.
参见
.NET: Alachisoft.NCache.运行时.事件 命名空间。
Java的: COM。alachisoft.ncache。事件 命名空间。