Wednesday, September 07, 2011

Getting started with Tangosol Coherence


This tip will get you started with Coherence so you can see how easy it is to begin to use in your applications for caching data within a cluster.

Grab a download of Coherence and unzip it, say into a directory called tangosol

Run the cache-server.cmd located in the tangosol/bin directory (replace the .cmd with .sh if you're running on *NIX). This starts a cache server which is storage enabled - the storage enabled bit simply means that this member within the cluster will store data.

Compile and run the below little program, it puts a couple of entries into a cache. It then gets their values out, along with a couple entries which don't currently exist in the cache.

The only JARs you need to reference are tangosol.jar and coherence.jar in the tangosol/lib directory.


public class Tip1 {
  public static void main(String[] args) {
    NamedCache cache = CacheFactory.getCache("people");

    String key1 = "dave";
    cache.put(key1, new Long(36));
    String key2 = "jenny";
    cache.put(key2, new Long(25));

    String key3 = "stan";
    String key4 = "jane";

    System.out.println(key1 + "=" + cache.get(key1));
    System.out.println(key2 + "=" + cache.get(key2));
    System.out.println(key3 + "=" + cache.get(key3));
    System.out.println(key4 + "=" + cache.get(key4));

Next run coherence.cmd in the tangosol/bin . This starts a nice little command-line application which allows you to explore some of Coherence's functionality without writing any programs.

At the Coherence command-line application's prompt type cache people - this just tells the command-line application that we want to manipulate the people cache. Some XML should have been displayed for the default distributed cache. The distributed cache is nowadays referred to as a 'partitioned cache' to better describe what it is actually doing with the data within the cluster.

Type get dave and press enter, you should see the value for Dave.
Next type put stan 55 and press enter, then put jane 40 . This will put two new entries into the cache using the Coherence command-line application.
Run the Tip1 application again and you should now see values for Stan and Jane.
From the Coherence command-line application, type remove stan .
Running the Tip1 application will show that Stan no longer has an entry.

To remove an entry using Java code simply use cache.remove(key) . As you might have noticed by now, it all seems very Map like - it is, because Coherence's NamedCache interface extends from interfaces which extend from java.util.Map.

For a bit of fun, start another cache-server.cmd and from Coherence command-line application type: get jane . You'll see no difference from before, the value will be displayed.

Now, kill the first instance of the cache-server.cmd and from the Coherence command-line application type get jane . Yep - still no difference, Coherence has automatically failed over without any loss of data. The data had been partitioned (split) across the two cache servers, when one of them was killed, the other simply promoted the 'backups' it was storing to be the primary copies ensuring all of the data was still accessible. If you start the first instance again, then Coherence will seamlessly 'fail back'.

Hope this gets you going with Coherence. If you want to explore the Coherence command-line application a little more then type help to see the valid commands and their syntax. The following link has descriptions for many of the commands:

Don't forget to check out the Wiki as well:

No comments: