VMware GemFire Java API Reference
Interface CacheListener<K,V>
-
- All Superinterfaces:
CacheCallback,Declarable
- All Known Subinterfaces:
RegionMembershipListener<K,V>,RegionRoleListener<K,V>
- All Known Implementing Classes:
CacheListenerAdapter,RegionMembershipListenerAdapter,RegionRoleListenerAdapter
public interface CacheListener<K,V> extends CacheCallback
A listener to handle region or entry related events.
Instead of implementing this interface it is recommended that you extend the
CacheListenerAdapterclass.Avoiding the risk of deadlock
The methods on a
CacheListenerare invoked while holding a lock on the entry described by theEntryEvent, as a result if the listener method takes a long time to execute then it will cause the operation that caused it to be invoked to take a long time. In addition, listener code which callsRegionmethods could result in a deadlock. For example, inafterUpdate(EntryEvent)for entry key k1,put(k2, someVal)is called at the same timeafterUpdate(EntryEvent)for entry key k2 callsput(k1, someVal)a deadlock may result. This co-key dependency example can be extended to a co-Region dependency where listener code in Region "A" performs Region operations on "B" and listener code in Region "B" performs Region operations on "A". Deadlocks may be either java-level or distributed multi-VM dead locks depending on Region configuration. To be assured of no deadlocks, listener code should cause some other thread to access the region and must not wait for that thread to complete the task.WARNING: To avoid risk of deadlock, do not invoke CacheFactory.getAnyInstance() from within any callback methods. Instead use EntryEvent.getRegion().getCache() or RegionEvent.getRegion().getCache().
Concurrency
Multiple events, on different entries, can cause concurrent invocation of
CacheListenermethods. Any exceptions thrown by the listener are caught by GemFire and logged.Declaring instances in Cache XML files
To declare a CacheListener in a Cache XML file, it must also implement
Declarable- Since:
- GemFire 3.0
- See Also:
AttributesFactory.addCacheListener(org.apache.geode.cache.CacheListener<K, V>),AttributesFactory.initCacheListeners(org.apache.geode.cache.CacheListener<K, V>[]),RegionAttributes.getCacheListeners(),AttributesMutator.addCacheListener(org.apache.geode.cache.CacheListener<K, V>),AttributesMutator.removeCacheListener(org.apache.geode.cache.CacheListener<K, V>),AttributesMutator.initCacheListeners(org.apache.geode.cache.CacheListener<K, V>[])
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidafterCreate(EntryEvent<K,V> event)Handles the event of new key being added to a region.voidafterDestroy(EntryEvent<K,V> event)Handles the event of an entry being destroyed.voidafterInvalidate(EntryEvent<K,V> event)Handles the event of an entry's value being invalidated.voidafterRegionClear(RegionEvent<K,V> event)Handles the event of a region being cleared.voidafterRegionCreate(RegionEvent<K,V> event)Handles the event of a region being created.voidafterRegionDestroy(RegionEvent<K,V> event)Handles the event of a region being destroyed.voidafterRegionInvalidate(RegionEvent<K,V> event)Handles the event of a region being invalidated.voidafterRegionLive(RegionEvent<K,V> event)Handles the event of a region being live after receiving the marker from the server.voidafterUpdate(EntryEvent<K,V> event)Handles the event of an entry's value being modified in a region.-
Methods inherited from interface org.apache.geode.cache.CacheCallback
close
-
Methods inherited from interface org.apache.geode.cache.Declarable
init, initialize
-
-
-
-
Method Detail
-
afterCreate
void afterCreate(EntryEvent<K,V> event)
Handles the event of new key being added to a region. The entry did not previously exist in this region in the local cache (even with a null value).- Parameters:
event- the EntryEvent- See Also:
Region.create(Object, Object),Region.put(Object, Object),Region.get(Object)
-
afterUpdate
void afterUpdate(EntryEvent<K,V> event)
Handles the event of an entry's value being modified in a region. This entry previously existed in this region in the local cache, but its previous value may have been null.- Parameters:
event- the EntryEvent- See Also:
Region.put(Object, Object)
-
afterInvalidate
void afterInvalidate(EntryEvent<K,V> event)
Handles the event of an entry's value being invalidated.- Parameters:
event- the EntryEvent- See Also:
Region.invalidate(Object)
-
afterDestroy
void afterDestroy(EntryEvent<K,V> event)
Handles the event of an entry being destroyed.- Parameters:
event- the EntryEvent- See Also:
Region.destroy(Object)
-
afterRegionInvalidate
void afterRegionInvalidate(RegionEvent<K,V> event)
Handles the event of a region being invalidated. Events are not invoked for each individual value that is invalidated as a result of the region being invalidated. Each subregion, however, gets its ownregionInvalidatedevent invoked on its listener.- Parameters:
event- the RegionEvent- See Also:
Region.invalidateRegion(),Region.localInvalidateRegion()
-
afterRegionDestroy
void afterRegionDestroy(RegionEvent<K,V> event)
Handles the event of a region being destroyed. Events are not invoked for each individual entry that is destroyed as a result of the region being destroyed. Each subregion, however, gets its ownafterRegionDestroyedevent invoked on its listener.- Parameters:
event- the RegionEvent- See Also:
Region.destroyRegion(),Region.localDestroyRegion(),Region.close(),RegionService.close()
-
afterRegionClear
void afterRegionClear(RegionEvent<K,V> event)
Handles the event of a region being cleared. Events are not invoked for each individual entry that is removed as a result of the region being cleared.- Parameters:
event- the RegionEvent- Since:
- GemFire 5.0
- See Also:
Region.clear()
-
afterRegionCreate
void afterRegionCreate(RegionEvent<K,V> event)
Handles the event of a region being created. Events are invoked for each individual region that is created.Note that this method is only called for creates done in the local vm. To be notified of creates done in remote vms use
RegionMembershipListener.afterRemoteRegionCreate(org.apache.geode.cache.RegionEvent<K, V>).- Parameters:
event- the RegionEvent- Since:
- GemFire 5.0
- See Also:
Cache.createRegion(java.lang.String, org.apache.geode.cache.RegionAttributes<K, V>),Region.createSubregion(java.lang.String, org.apache.geode.cache.RegionAttributes<SK, SV>)
-
afterRegionLive
void afterRegionLive(RegionEvent<K,V> event)
Handles the event of a region being live after receiving the marker from the server.- Parameters:
event- the RegionEvent- Since:
- GemFire 5.5
- See Also:
Cache.readyForEvents()
-
-