Configuring Cache Listeners

You can register Cache Listeners for a region to handle data events such as when data is created, updated, or deleted. A Cache Listener is a bean that implements the com.gemstone.gemfire.cache.CacheListener interface.

The following configuration declares a cache listener for the Customer region:
<gfe:replicated-region id="Customer">
   <gfe:cache-listener>
      <bean class="org.springframework.data.gemfire.examples.util.LoggingCacheListener"/>
   </gfe:cache-listener>
</gfe:replicated-region>
The following Java class logs changes to region data. The class extends the GemFireCacheListenerAdapter class, which is an implementation of the CacheListener interface.
public class LoggingCacheListener extends CacheListenerAdapter {
	private static Log log = LogFactory.getLog(LoggingCacheListener.class);

	 
	@Override
	  public void afterCreate(EntryEvent event) {
	    final String regionName = event.getRegion().getName();
	    final Object key = event.getKey();
	    final Object newValue = event.getNewValue();
	    log.info("In region [" + regionName + "] created key [" + key
	        + "] value [" + newValue + "]");
	  }

	  @Override
	  public void afterDestroy(EntryEvent event) {
	    final String regionName = event.getRegion().getName();
	    final Object key = event.getKey();
	    log.info("In region [" + regionName + "] destroyed key [" + key
	        + "]");
	  }

	  @Override
	  public void afterUpdate(EntryEvent event) {
	    final String regionName = event.getRegion().getName();
	    final Object key = event.getKey();
	    final Object newValue = event.getNewValue();
	    final Object oldValue = event.getOldValue();
	    log.info("In region [" + regionName + "] updated key [" + key
	        + "] [oldValue [" + oldValue + "] new value [" + newValue +"]");
	  }
}

Additional Resources