Terracotta 10.11 | Ehcache API Developer Guide | Cache Event Listeners | Registering Event Listeners during runtime
 
Registering Event Listeners during runtime
Cache event listeners may also be added and removed while the cache is being used.
ListenerObject listener = new ListenerObject(); // 1
cache.getRuntimeConfiguration().registerCacheEventListener(listener,
EventOrdering.ORDERED,
EventFiring.ASYNCHRONOUS, EnumSet.of(EventType.CREATED,
EventType.REMOVED)); // 2

cache.put(1L, "one");
cache.put(2L, "two");
cache.remove(1L);
cache.remove(2L);

cache.getRuntimeConfiguration().deregisterCacheEventListener(listener); // 3

cache.put(1L, "one again");
cache.remove(1L);
1
Create a CacheEventListener implementation instance.
2
Register it on the RuntimeConfiguration, indicating the delivery mode and events of interest. The following put() and remove() cache calls will make the listener receive events.
3
Unregister the previously registered CacheEventListener instance. The following put() and remove() cache calls will have no effect on the listener anymore.