Event Notifications in Cache
The client application that is supposed to receive events must include the specific event handler method to respond to the event(s) raised. Whenever events are raised, the target event handler method executes the entire code written in the method body. By default event notifications are disabled (except for Cache Cleared) for any cache configuration and must be enabled for events to be published.
Pre-Requisites
- Add the following Maven dependencies in your
pom.xml
file:
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<artifactId>ncache-client</artifactId>
<version>5.2.0</version>
</dependency>
- Import the following packages in your application:
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.spi.CachingProvider;
- Make sure that the data is serailized or registered with NCache Compact Serialization format.
- Make sure that the cache is running.
Registering Cache Level Events
Cache Level notifications are generic events which will be triggered upon every execution of the appropriate registered event type. To register these events for notification in NCache API, you need to implement CacheDataModificationListener
, and then pass its instance to addCacheDataModificationListener
method with EventType, where in contrast in NCache’s JCache API provider, there is no need to register NCache specific events. NCache services will automatically invoke JCache event(s) (Created, Updated, Removed and Expired) incase any registered event with JCache is occurred.
In order to get event(s) notifications from JCache, you need to implement JCache’s events related interfaces as specified in its documentation. The example below assumes that you have implemented the event-filter and event-listener factories.
try
{
MutableCacheEntryListenerConfiguration _listenerConfig = null;
_listenerConfig = new MutableCacheEntryListenerConfiguration(
FactoryBuilder.factoryOf(_EventListenerFactory),
FactoryBuilder.factoryOf(_EventFIlterFactory),
_isOldValueRequired,
_isSynchronous);
jCache.registerCacheEntryListener(_listenerConfig);
jCache.put("key", "value");
Thread.sleep(5000);
}
catch(Exception ex)
{
// Handle exception
}
Unregister Cache Level Events
You can also unregister a previously registered cache level notification using the following removeCacheDataModificationListener
method in NCache API, where in JCache you need to call JCache’s deregisterCacheEnterListener
with passing a registered cache entry listener’s instance to unregister. For NCache API you need to specify the eventdescriptor
previously returned upon the registration of an event.
jCache.deregisterCacheEntryListener(_listenerConfig);
Register Item Level Events
Item level events are not associated with general events, and must be registered against some specific key(s) with the events being published only for these specific key(s) in NCache API, where in JCache, registering cache entry listener(s) also cover(s) item specific events.
In the following example, the cache will generate a notification when the appropriate operation is performed on the key.
try
{
//Register Listener
_cache = (com.alachisoft.ncache.web.caching.Cache) jCache.unwrap(com.alachisoft.ncache.web.caching.Cache.class);
//CacheEventListener eventListener = new CacheEventListener();
_cache.addCacheDataModificationListener("key", eventListener,
EnumSet.of(EventType.ItemAdded), EventDataFilter.Metadata);
}
catch(Exception ex)
{
// Handle exception
}
Unregister Item Level Notifications
You can also unregister a previously registered item level notification using the following removeCacheDataModificationListener
method. In this example you need to specify the appropriate key and listener previously provided upon registering of event.
try
{
cache.removeCacheDataModificationListener("key", eventListener,EnumSet.of(EventType.ItemRemoved));
}
catch(Exception ex)
{
// Handle exception
}
See Also
Initialize Cache
Add/Update in Cache
Clear Cache
Hibernate Caching
Data Expiration in Cache
NCache Java Session Module