Disclaimer

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.

Sunday, April 24, 2011

Creating a robust, scale-out data abstraction layer with Oracle Coherence

Oracle Coherence is an in-memory data grid solution that enables organizations to predictably scale mission-critical applications by providing fast access to frequently used data. Data grid software is middleware that reliably manages data objects in memory across many servers. By automatically and dynamically partitioning data, Oracle Coherence enables continuous data availability and transactional integrity, even in the event of a server failure. Oracle Coherence provides organizations with a robust, scale-out data abstraction layer.

In today post, i will guide you through how to install, configure and run simple Java Coherence Application. Hopefully, it could help people understand the basic concept of Coherence and able to implement it in their real project. Good Luck!

1. Download the latest version of Oracle Coherence for Java here:
http://www.oracle.com/technetwork/middleware/coherence/downloads/coherence-101246.html

2. Download JDeveloper 11.1.1.3 here:
http://www.oracle.com/technetwork/developer-tools/jdev/downloads/soft11-098086.html

3. Unzip and Install Oracle Coherence and JDeveloper.
Ensure that the directory name you install into does not have any spaces.

4. Configure Java Home and Coherence
1. Setup JAVA_HOME for Oracle Coherence
• In the coherence.cmd and cache-server.cmd (located in [COHERENCE_HOME]\coherence\bin) set the JAVA_HOME variable to point to your JSDK 1.6 environment.

SET JAVA_HOME=[JDK PATH]

In Windows explorer change to the [COHERENCE_HOME]\coherence\bin directory.

• In the file coherence.cmd (or coherence.sh), find the line with set storage_enabled and
change the value false to true.

set storage_enabled=true

This option specifies if a process is a “Storage Enabled Member” when joining the cluster. Storage enabled members can store data as part of a Coherence cluster.

• Save the files and exit.

5. Run Coherence Cache Server
• From the [COHERENCE_HOME\coherence\bin directory run the following file: cache-server.cmd
• This will start up a storage enabled member in the cluster. When you start the first cache-server up you will notice a slight delay as the cache server looks for an existing cluster. Once it determines there are no clusters to join, it will start one.

Note: by default Coherence uses multicast to search for cluster members only. Multicast is not used for data transfer. If you cannot use or do not want to use multicast then this can be configured.

• You should see a number of messages displayed and at the bottom of your console, you should see below message:

Started DefaultCacheServer...

6. Run coherence console and test the Cache Server
  • Run coherence.cmd (windows) or coherence.sh (unix/linux)
  • You should now see the following prompt:
    Map (?):
  • This application shows you the basic distributed cache functionality build within
    Coherence
  • Type in the following to create a new named cache called test.
    cache test
  • Each named cache can be thought of as a table. A cluster can have many named caches. Each named cache holds one type of object. It can be a simple object such as a String, or a complex object that you define
  • Type in the command help to see the list of commands available. The following
    commands are the ones you will use most:
    • put – puts a name value pair (key and value) into the cache. Always returns the last
    value that the key had; get – retrieves the value for the given key from the named cache;
    • list – shows the contents of the named cache;
    • remove – removes a key value from the cache
  • Use the commands above and put and get some values. For ex. put name John [enter], get name [enter]
7. Create Java Application and connect to Cache Server
Since you're going to use Coherence API. You must configure coherence java libraries (under your [COHERENCE_HOME]/coherence/lib) before writing and compiling the applications.


Below is the code that you can use to put and get some value from Cache Server:

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

public class MyFirstSample {
public MyFirstSample() {
}

public static void main(String[] args) {

// ensure we are in a cluser
CacheFactory.ensureCluster();

// create or get a named cache called mycache
NamedCache myCache = CacheFactory.getCache("mycache");

// put key, value pair into the cache.
myCache.put("Name","John");

System.out.println("Value in cache is " + myCache.get("Name"));

}
}

That's it. Congratulations! you have successfully created your first robust, scale-out java application using Oracle Coherence Data Cache Solution. With this simple example, you could have different data sources i.e. Oracle Database, SAP, Siebel, etc. and put them onto the Cache Server that a variety of clients can connect, get some valuable information from it.


No comments: