Cache Event Handler Examples

Some examples of cache event handlers.

Declaring and Loading an Event Handler with Parameters

This declares an event handler for a region in the cache.xml. The handler is a cache listener designed to communicate changes to a DB2 database. The declaration includes the listener’s parameters, which are the database path, username, and password.
<region name="exampleRegion"> 
  <region-attributes> 
  . . . 
    <cache-listener> 
      <class-name>JDBCListener</class-name> 
      <parameter name="url"> 
        <string>jdbc:db2:SAMPLE</string> 
      </parameter> 
      <parameter name="username"> 
        <string>gfeadmin</string> 
      </parameter> 
      <parameter name="password"> 
        <string>admin1</string> 
      </parameter> 
    </cache-listener> 
  </region-attributes> 
  </region>
This code listing shows part of the implementation of the JDBCListener declared in the cache.xml. This listener implements the Declarable interface. When an entry is created in the cache, this listener’s afterCreate callback method is triggered to update the database. Here the listener’s properties, provided in the cache.xml, are passed into the Declarable.init method and used to create a database connection.
. . .
public class JDBCListener
extends CacheListenerAdapter
implements Declarable {
  public void afterCreate(EntryEvent e) {
  . . .
    // Initialize the database driver and connection using input parameters
    Driver driver = (Driver) Class.forName(DRIVER_NAME).newInstance();
    Connection connection =
      DriverManager.getConnection(_url, _username, _password);
      System.out.println(_connection);
        . . .
  }
    . . .
  public void init(Properties props) {
    this._url = props.getProperty("url");
    this._username = props.getProperty("username");
    this._password = props.getProperty("password");
  }
}

Installing an Event Handler Through the API

This listing defines a cache listener using AttributesFactory.
AttributesFactory fac = new AttributesFactory(); 
fac.addCacheListener(new SimpleCacheListener()); 
Region newReg = this.cache.createRegion(name, fac.create()); 
You can create a cache writer similarly, using the AttributesFactory method setCacheWriter, like this:
fac.setCacheWriter(new SimpleCacheWriter());

Installing Multiple Listeners on a Region

XML:
<region name="exampleRegion">
  <region-attributes>
    . . .
    <cache-listener>
      <class-name>myCacheListener1</class-name>
    </cache-listener>
    <cache-listener>
      <class-name>myCacheListener2</class-name>
    </cache-listener>
    <cache-listener>
      <class-name>myCacheListener3</class-name>
    </cache-listener>
  </region-attributes>
</region>
API:
AttributesFactory fac = new AttributesFactory(this.currRegion.getAttributes()); 
fac.setScope(Scope.DISTRIBUTED_NO_ACK); 
CacheListener listener1 = new myCacheListener1(); 
CacheListener listener2 = new myCacheListener2(); 
CacheListener listener3 = new myCacheListener3(); 
fac.initCacheListeners(new CacheListener[] { listener1, listener2, listener3}); 
Region nr = cache.createRegion(name, fac.create()); 
regionDefaultAttrMap.put(nr.getFullPath(), fac.create());

Installing a Write-Behind Cache Listener

//GatewayEventListener that performs WBCL work
<cache>
	 <gateway-hub id="DB">
		  <gateway id="DB">
			   <gateway-listener>
				    <class-name>MyGatewayListener</class-name>
			   </gateway-listener>
			   <gateway-queue ... 
		  </gateway>
	 </gateway-hub>

// Enable gateway queueing for the region(s) where I need WBCL
  <region name="data">
	   <region-attributes refid="REPLICATE" enable-gateway="true"/> 
  </region>
</cache>

Installing a BridgeMembershipListener

DistributedSystem ds = DistributedSystem.connect(properties);
MyMembershipListenerImpl myListener = new MyMembershipListenerImpl();
BridgeMembership.registerBridgeMembershipListener(myListener);
Cache cache = CacheFactory.create(ds);