Gemfire Cache Listeners are registered with a region to handle region events such as entries being created, updated, destroyed, etc. A Cache Listener can be any bean that implements the com.gemstone.gemfire.cache.CacheListener interface. A region may have multiple listeners. Cache Listeners is important feature to observe data events at cluster-wide.

The methods of Cache Listener Interface :

- void afterCreate(EntryEvent event) : Handles the event of new key being added to a region.
- void afterDestroy(EntryEvent event) : Handles the event of an entry being destroyed.
- void afterInvalidate(EntryEvent event) : Handles the event of an entry’s value being invalidated.
- void afterRegionClear(RegionEvent event) : Handles the event of a region being cleared.
- void afterRegionCreate(RegionEvent event) : Handles the event of a region being created.
- void afterRegionDestroy(RegionEvent event) : Handles the event of a region being destroyed.
- void afterRegionInvalidate(RegionEvent event) : Handles the event of a region being invalidated.
- void afterRegionLive(RegionEvent event) : Handles the event of a region being live after receiving the marker from the server.
- void afterUpdate(EntryEvent event) : Handles the event of an entry’s value being modified in a region.
- void close() : Called when the region containing this callback is closed or destroyed, when the cache is closed, or when a callback is removed from a region using an AttributesMutator.

Cache Listeners can be configured as the following :

1- Gemfire cache configuration xml file :

2- Spring Data Gemfire Project helps the developers for easier integration. Listeners can be defined via the following configuration :

In this article, only UserCacheListener has been configured for userRegion. However, A region may have multiple listeners via the following configuration :

This article shows how to enable GemFire Cache Listener Feature. Let’ s look at the details.

Used Technologies :

JDK 1.7.0_40
Spring 3.2.3
SpringData-GemFire 1.3.0
GemFire 7.0
Maven 3.0.4

STEP 1 : CREATE MAVEN PROJECT

A maven project is created as below. (It can be created by using Maven or IDE Plug-in).

GemFire Cache Listener Support

STEP 2 : DEPENDENCIES

Firstly, dependencies are added to Maven’ s pom.xml.

STEP 3 : CREATE USER BEAN

A new User Bean is created. This bean will be distributed between two nodes in cluster.

STEP 4 : CREATE UserCacheListener CLASS

A new UserCacheListener class is created. This listener receives distributed userRegion events.

STEP 5 : CREATE IUserCacheService INTERFACE

A new IUserCacheService Interface is created for service layer.

STEP 6 : CREATE UserCacheService IMPL

UserCacheService is created by implementing IUserCacheService.

STEP 7 : CREATE gemfire.properties

gemfire.properties file is created as the following :

STEP 8 : CREATE cache.xml

cache.xml file is created as the following. It contains cache/region configurations.

STEP 9 : CREATE applicationContext.xml

Spring applicationContext.xml is created.

STEP 10 : CREATE Application CLASS

Application Class is created to run the application.

STEP 11 : BUILD PROJECT

After DATA4J_SpringData_GemFire_ListenerSupport Project is build, data4j_springdata_gemfire_listenersupport-0.0.1-SNAPSHOT.jar will be created.

STEP 12 : RUN PROJECT ON FIRST MEMBER OF THE CLUSTER

After created data4j_springdata_gemfire_listenersupport-0.0.1-SNAPSHOT.jar file is run at the members of the cluster, the following output logs will be shown on first member’ s console :

STEP 13 : SOURCE CODEs

DATA4J_SpringData_GemFire_ListenerSupport

REFERENCES

Spring Data GemFire
GemFire Documentation