Coherence Hello World

To introduce fundamental concept of oracle coherence application, we will be building simple coherence hello world standalone java application. After running this example you will understand basics of oracle coherence and could move towards integrating in your own application. Please follow steps below:

  • Install oracle coherence using this tutorial
  • Create maven project name: CoherenceJavaApplication below is final structure of the project:

Coherence Hello World

  • 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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javahonk</groupId>
	<artifactId>CoherenceJavaApplication</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>CoherenceJavaApplication Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>coherence</groupId>
			<artifactId>coherence</artifactId>
			<version>1.0.0</version>
			<scope>system</scope>
			<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/coherence.jar</systemPath>
		</dependency>
	</dependencies>
	<build>
		<finalName>CoherenceJavaApplication</finalName>
	</build>
</project>
  • Define your cache: Caches should be defined in cache deployment descriptor it should be refer by the name of your application. This makes easy for configuration changes without changing any application code. We will create distributed cache name javahonk-hello-world. To define distributed cache create XML file name: example-config.xml inside src/main/resources directory and copy paste below code:
<?xml version="1.0" encoding="UTF-8"?>
<cache-config>
   <caching-scheme-mapping>
      <cache-mapping>
         <cache-name>javahonk-hello-world</cache-name>
         <scheme-name>distributed</scheme-name>
      </cache-mapping>
   </caching-scheme-mapping>
   
   <caching-schemes>
      <distributed-scheme>
         <scheme-name>distributed</scheme-name>
         <service-name>DistributedCache</service-name>
         <backing-map-scheme>
         	<local-scheme/>
         </backing-map-scheme>
         <autostart>true</autostart>
      </distributed-scheme>
   </caching-schemes>
</cache-config>
  • Define and configure cluster: Create XML file name tangosol-coherence-override.xml inside src/main/resources directory and copy paste below code:
<?xml version='1.0'?>
 
<!DOCTYPE coherence SYSTEM "coherence.dtd">

<coherence>
	<cluster-config>
		<member-identity>
			<cluster-name>javahonk</cluster-name>
		</member-identity>

		<multicast-listener>
			<address>225.4.7.0</address>
			<port>1476</port>
			<time-to-live>0</time-to-live>
		</multicast-listener>
	</cluster-config>

	<configurable-cache-factory-config>
      <init-params>
         <init-param>
            <param-type>java.lang.String</param-type>
            <param-value system-property="tangosol.coherence.cacheconfig">example-config.xml</param-value>
        </init-param>
      </init-params>
   </configurable-cache-factory-config>	
</coherence>

Note: You can choose any cluster name as you like and address should be chosen between multicast address which ranges from the 224.0.0.0 through 239.255.255.255. You could choose any unique number as your port. Save this file on same location where example-config.xml was saved.

  • Now start cache server instance using DefaultCacheServer class also include location of the coherence.jar file as below:
java -cp COHERENCE_HOME\lib\coherence.jar;COHERENCE_HOME\config com.tangosol.net.DefaultCacheServer

Real command: If you followed tutorial above to install oracle coherence on your local computer then command will be as below:

java -cp C:\Oracle\Middleware\Oracle_Home\coherence\lib\coherence.jar;C:\Oracle\Middleware\Oracle_Home\coherence\config com.tangosol.net.DefaultCacheServer

Coherence Hello World

  • Started server screen shot:

Coherence Hello World

  • Create standalone java program HelloWorld.java inside com.javahonk package, get cache from CacheFactory, add key value to the cache, retrieve and print its value on the console.
package com.javahonk;

import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;


public class HelloWorld {
	public static void main(String[] args) {
		 
	      String key = "JavaHonk";
	      String value = "Java Honk Hello World!";
	 
	      CacheFactory.ensureCluster();
	      NamedCache cache = CacheFactory.getCache("javahonk-hello-world");
	 
	      cache.put(key, value);
	      System.out.println((String)cache.get(key));
	 
	      CacheFactory.shutdown();
	   }
}
  • To  run oracle coherence application right click HelloWorld.java –> Run As –> Java Application you will see below Java Honk Hello World! output:

Coherence Hello World

  • That’s it. For more information on Oracle coherence please visit its official site here

download  Download sample project:  CoherenceJavaApplication

3 thoughts on “Coherence Hello World”
  1. Hi Java Honk,
    Do you have any advise on making the coherence.java file read the tangosol-coherence-override.xml, I have followed your example to the teeth but I keep getting this error:
    Error: Could not find or load main class com.tangosol.net.DefaultCacheServer

    1. you will have to start cache server instance using DefaultCacheServer class also include location of the coherence.jar. Please go through installation and test tutorial by using search in JavaHonk.

  2. Hello Java Honk
    I am developing a EJB using maven, my question is where should I put both xml files when I build (mvn clean install) my project?

    thank you!

Leave a Reply

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