Oracle Coherence Cache Listener Example

Caching is very important part of the application. There are many caching products are available in which Oracle Coherence is quite famous in industry wide. Here I will give you overview how you can create coherence listener on any cache. Please follow below steps:

  • To understand better I will create maven project name: CoherenceCacheListenerExample and below is final structure:

Oracle Coherence Cache Listener Example

Its quite simple you will need only below class:

  • CoherenceCacheListener.java:
package com.javahonk;

import javax.annotation.PostConstruct;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;
import com.tangosol.util.MapEvent;
import com.tangosol.util.MapListener;


public class CoherenceCacheListener {
	
	private MapListener cacheListener;
	
	private static final Logger logger = LogManager.getLogger(CoherenceCacheListener.class.getName());
	
	@PostConstruct
	public void init(){
		
		cacheListener = new ContractsCacheListener();
	}

	public void startContractsListener() {
		
		logger.info("ContractsListener starting");
		//NamedCache contractCache = (NamedCache) CacheRegistry.getNamedCache("ContractDataSetBySmId");
		
		NamedCache contractCache = (NamedCache) CacheFactory.getCache("ContractDataSetBySmId");
		
		contractCache.addMapListener(cacheListener);
	}
	
	class ContractsCacheListener implements MapListener
    {

		@Override
		public void entryDeleted(MapEvent evt) {
			Object newContract = evt.getNewValue();
			System.out.println(newContract);
		}

		@Override
		public void entryInserted(MapEvent evt) {

			logger.info("Received New Contract notification");

			try {
				Object newContract = evt.getNewValue();
				System.out.println(newContract);

			} catch (Exception e) {

				logger.error("Exception while processing new Contract Data Item\n {}",e);

			}

		}

		@Override
		public void entryUpdated(MapEvent evt) {

			logger.info("Received Updated Contract notification");

			try {
				Object newContract = evt.getNewValue();
				System.out.println(newContract);

			} catch (Exception e) {
				logger.error("Exception while processing modified Contract Data Item\n {}", e);
			}

		}
		
    }
}

If you are using spring then you can use below context and java load and call listener:

  • spring-cache-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
	
	<context:annotation-config />
	<context:component-scan base-package="com.javahonk" />
	
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>OracleCoherenceCache.properties</value>
			</list>
		</property>
		<property name="ignoreUnresolvablePlaceholders" value="true"/>
		<property name="ignoreResourceNotFound" value="true"/>
	</bean>
	
	<bean id="coherenceCacheListener" class="com.javahonk.CoherenceCacheListener" />
	
</beans>
  • custom-types-pof-config.xml:
<?xml version="1.0"?>

<!DOCTYPE pof-config SYSTEM "pof-config.dtd">

<pof-config>
	<user-type-list>
		<include>coherence-pof-config.xml</include>
	 	<include>mercury-df-pof-config.xml</include>
	</user-type-list>
</pof-config>
  • OracleCoherenceCacheApp.java:
package com.javahonk;

import java.text.ParseException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class OracleCoherenceCacheApp {
	
	private static final Logger LOGGER = LogManager.getLogger(OracleCoherenceCacheApp.class.getName());
	
	public static void main(String[] args) throws ParseException {
		
		LOGGER.info("Starting Oracle coherence cache application...");
		
		ApplicationContext context = new ClassPathXmlApplicationContext("spring-cache-context.xml");
		
		context.getBean(CoherenceCacheListener.class).startContractsListener();
		
		Thread thread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				while (!Thread.interrupted()) {
				}
			}
		});
		thread.start();
		
		((AbstractApplicationContext) context).close();
	}

	
}
  • pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<groupId>com.wfs.otc.cache</groupId>
	<artifactId>CoherenceCacheListener</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>CoherenceCacheListener</name>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<org.apache.log4j.version>2.1</org.apache.log4j.version>
		<clusterAPI.version>1.0</clusterAPI.version>
		<coherence.version>3.7</coherence.version>
		<coherence-incubator.version>11.3.0</coherence-incubator.version>
		<galaxy.version>v_10_0_20141003</galaxy.version>
		<xercesImpl.version>unknown</xercesImpl.version>
		<org.springframework.version>4.1.5.RELEASE</org.springframework.version>
		<joda.version>2.7</joda.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>com.javahonk.clusterAPI</groupId>
			<artifactId>ClusterClient</artifactId>
			<version>${clusterAPI.version}</version>
		</dependency>

		<dependency>
			<groupId>com.javahonk.clusterAPI</groupId>
			<artifactId>ClusterInfoInvocation</artifactId>
			<version>${clusterAPI.version}</version>
		</dependency>

		<dependency>
			<groupId>com.oracle.coherence</groupId>
			<artifactId>coherence</artifactId>
			<version>${coherence.version}</version>
		</dependency>

		<dependency>
			<groupId>com.oracle.coherence.incubator</groupId>
			<artifactId>coherence-common</artifactId>
			<version>${coherence-incubator.version}</version>
		</dependency>

		<dependency>
			<groupId>com.oracle.coherence.incubator</groupId>
			<artifactId>coherence-commandpattern</artifactId>
			<version>${coherence-incubator.version}</version>
		</dependency>

		<dependency>
			<groupId>com.oracle.coherence.incubator</groupId>
			<artifactId>coherence-messagingpattern</artifactId>
			<version>${coherence-incubator.version}</version>
		</dependency>

		<dependency>
			<groupId>com.oracle.coherence.incubator</groupId>
			<artifactId>coherence-processingpattern</artifactId>
			<version>${coherence-incubator.version}</version>
		</dependency>

		<dependency>
			<groupId>datafabric-common</groupId>
			<artifactId>datafabric-common</artifactId>
			<version>1.0-SNAPSHOT</version>
			<scope>system</scope>
			<systemPath>${basedir}/lib/datafabric-common-1.0-SNAPSHOT.jar</systemPath>
		</dependency>
		
		<dependency>
			<groupId>com.javahonk.galaxy</groupId>
			<artifactId>eqdframework</artifactId>
			<version>${galaxy.version}</version>
		</dependency>

		<dependency>
			<groupId>com.javahonk.galaxy</groupId>
			<artifactId>eqd</artifactId>
			<version>${galaxy.version}</version>
		</dependency>

		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-api</artifactId>
			<version>${org.apache.log4j.version}</version>
		</dependency>

		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>${org.apache.log4j.version}</version>
		</dependency>

		<dependency>
			<groupId>xerces</groupId>
			<artifactId>xercesImpl</artifactId>
			<version>${xercesImpl.version}</version>
		</dependency>

		<dependency>
			<groupId>joda-time</groupId>
			<artifactId>joda-time</artifactId>
			<version>${joda.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

	</dependencies>
</project>

For more details documentation please refer Oracle Coherence here

Leave a Reply

Your email address will not be published. Required fields are marked *