MapTrigger is one of the most important features of Oracle Coherence to provide a highly customized cache management system. It represents a functional agent that allows to validate, reject or modify mutating operations against an underlying map. Also, they can prevent invalid transactions, enforce security, provide event logging and auditing, and gather statistics on data modifications.

For example, we have code that is working with a NamedCache, and we want to change an entry’s behavior or contents before the entry is inserted into the map. This change can be made without modifying all the existing code by enabling a map trigger.

MapTrigger interface is under com.tangosol.util package and extends Serializable as default. It exposes the following process method :

void process(MapTrigger.Entry entry) : This method is called by framework before the result of a mutating operation represented by the specified Entry object is committed into the underlying map. Also a MapTrigger Entry(MapTrigger.Entry) represents a pending change to an Entry that is about to committed to the underlying Map.

There are two ways to enable Map Trigger feature :

1) Programmatic-based : The MapTriggerListener is a special purpose com.tangosol.util.MapListener implementation that is used to register a MapTrigger with a corresponding NamedCache.
2) Configuration-based : The class-factory mechanism can be used in the coherence-cache-config.xml configuration file as the following :

In the following sample application, MapTrigger feature is enabled programmatically by using MapTriggerListener. A new DATA4J cluster is created and User bean is distributed by user-map NamedCache object.

Used Technologies :

JDK 1.7.0_40
Spring 3.2.4
Coherence 3.7.1
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).

Coherence Event Processing via Map Trigger Feature

STEP 2 : LIBRARIES

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

Coherence library is installed to Local Maven Repository manually and its description is added to pom.xml as below. Also if Maven is not used to manage the project, coherence.jar file can be added to classpath.

STEP 3 : CREATE USER BEAN

A new User Bean is created. It will be distributed between two nodes in DATA4J cluster.

STEP 4 : CREATE UserMapListener CLASS

A new User Map Listener is created. It listens distributed user-map events.

STEP 5 : CREATE UserMapTrigger CLASS

UserMapTrigger Class is created to execute required logic before the operation is committed. This trigger updates User’ s id and name & surname properties with upper case.

STEP 6 : CREATE IUserCacheService INTERFACE

A new IUserCacheService Interface is created for service layer and it exposes cache functionality.

STEP 7 : CREATE UserCacheService IMPL

UserCacheService is created by implementing IUserCacheService.

STEP 8 : CREATE CacheUpdater CLASS

CacheUpdater Class is created to add new entry to cache and monitor cache content.

CacheUpdater Class for the first member of the cluster :

CacheUpdater Class for the second member of the cluster :

STEP 9 : CREATE data4j-coherence-cache-config.xml

data4j-coherence-cache-config.xml contains caching-schemes(such as distributed or replicated) and caching-scheme-mapping configuration. Created cache configuration file should be defined in coherence-cache-config.xml.

STEP 10 : CREATE tangosol-coherence-override.xml

tangosol-coherence-override.xml contains cluster, member-identity and configurable-cache-factory configuration.

tangosol-coherence-override.xml for the first member of the cluster :

tangosol-coherence-override.xml for the second member of the cluster :

STEP 11 : CREATE applicationContext.xml

applicationContext.xml is created for Spring DI.

STEP 12 : CREATE Application CLASS

Application Class is created in order to run the application.

STEP 13 : BUILD PROJECT

After /DATA4J_Spring_Coherence_MapTrigger Project is build, data4j-spring-coherence-maptrigger-0.0.1-SNAPSHOT.jar will be created. The members of the cluster have to be built separately for each member.

STEP 14 : RUN PROJECT ON FIRST MEMBER OF THE CLUSTER

After created data4j-spring-coherence-maptrigger-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 :

Second member’ s console :

STEP 15 : SOURCE CODE

DATA4J_Spring_Coherence_MapTrigger

REFERENCES :

Managing Map Operations with Triggers
MapTrigger API