3 Accessing the Data Grid from Java

In this exercise, you develop a simple, Java console-based application to access, update, and remove simple types of information from a Coherence clustered cache. You also are introduced to the Coherence NamedCache and CacheFactory Java APIs.

Using the Eclipse IDE, you will perform the following tasks:

This chapter contains the following sections:

3.1 Introduction

All Coherence caches are named, have a lifetime scoped by the cluster instance in which they exist, and implement the com.tangosol.net.NamedCache interface. The NamedCache interface is an extension of the java.util.Map interface and holds data and resources that are shared among the cluster members. Each NamedCache object holds data as key/value pairs. Keys and values can be both simple and complex object types. The NamedCache interface provides extensions to the Map interface, such as locking and synchronization, storage integration, queries, event aggregations, and transactions. Table 3-1 describes some of the commonly used methods within the NamedCache interface.

Table 3-1 Methods in the NamedCache Interface

Method Name Description

void clear()

Removes all entries from the NamedCache object.

boolean containsKey(Object key)

Returns true if the NamedCache object contains an entry for the key.

boolean containsValue(Objectvalue)

Returns true if there is at least one entry with this value in the NamedCache object.

Object get(Object key)

Gets the entry from the NamedCache object for that key.

Object put(Object key,Objectvalue)

Puts an object in the cache and returns the previous value (if any).

Object remove(Object key)

Removes the mapping for this key from this map if present. Inherited from the ConcurrentMap interface.

Set entrySet()

Returns a set of key/value pairs.

Collection values()

Gets all values back as a collection.

CacheService getCacheService()

Returns the CacheService to which this NamedCache belongs.


The com.tangosol.net.CacheFactory class is typically used to get an instance of a NamedCache object. Table 3-2 describes some of the more commonly used methods in the CacheFactory class.

Table 3-2 Methods in the CacheFactory Class

Method Name Description

static Cluster ensureCluster()

Obtains a cluster object running Coherence services.

static void shutdown()

Shuts down all clustered services.

static NamedCache getCache(String cache)

Returns an instance of a cache. Either joins an existing cache in the cluster or creates the cache if this is the first member.


For a full list of methods in the NamedCache interface and the CacheFactory class, see the Javadoc in C:\oracle\product\coherence\doc.

3.2 Creating Your First Coherence-Based Java Program

This section describes how to create a Java program that enables you to access, update, and remove simple types of information from a Coherence clustered cache.

  1. Create a Program to Put Values in the Cache

  2. Create a Program to Get Values from the Cache

3.2.1 Create a Program to Put Values in the Cache

To create a Coherence Java-based application:

  1. Create a new Application Client Project in Eclipse. Name the project InsertValue. Ensure that the folder is C:\home\oracle\workspace\InsertValue.

    In the Configuration section of the New Application Client Project dialog box, click Modify. In the Project Facets dialog box, select CoherenceConfig from the Configuration drop down list.

    See "Creating a New Project in the Eclipse IDE" for detailed instructions.

  2. Create your first Coherence Java program. Name the class MyFirstSample and select the public static void main(String[] args) check box in the New Java Class dialog box.

    See "Creating a Java Class" for detailed information.

  3. In the Eclipse editor, write the code to create a NamedCache object, enter a value in the cache, and then verify the value that you entered. Example 3-1 illustrates a sample program.

    Example 3-1 Creating a NamedCache Object: Inserting and Verifying Values

    package com.oracle.handson;
    
    import com.tangosol.net.CacheFactory;
    import com.tangosol.net.NamedCache;
    
    public class MyFirstSample {
        public MyFirstSample() {
        }
    
        public static void main(String[] args) {
            
           // create or get a named cache called mycache 
           NamedCache myCache = CacheFactory.getCache("mycache");
           
           // put key, value pair into the cache. 
           myCache.put("Name","Gene Smith");
            
           System.out.println("Value in cache is " + myCache.get("Name"));
            
        }
    }
    
  4. Stop any running cache servers. See "Stopping Cache Servers" for detailed information.

  5. Run the program in the Eclipse IDE: right-click the MyFirstSample.java class in the editor and select Run As then Run Configuration. Double-click Oracle Coherence to create a Coherence configuration. Enter MyFirstSample in the Name field of the Run Configuration dialog box.

    In the Main tab, enter InsertValue in the Project field and com.oracle.handson.MyFirstSample in the Main class field.

    In the Coherence tab, enter a unique value, such as 3155, in the Cluster port field to ensure that Coherence is limited to your own host. Select Enabled (cache server) under Local storage.

    Note that in the Classpath tab, the coherence.jar file should be present in the Bootstrap Entries list. Click Apply, then Run.

    Messages similar to Example 3-2 are displayed:

    Example 3-2 Output of MyFirstSample Program

    2012-08-15 15:53:41.041/0.515 Oracle Coherence 12.1.2.0 <Info> (thread=main, member=n/a): Loaded operational configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/tangosol-coherence.xml"
    2012-08-15 15:53:41.088/0.562 Oracle Coherence 12.1.2.0 <Info> (thread=main, member=n/a): Loaded operational overrides from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/tangosol-coherence-override-dev.xml"
    2012-08-15 15:53:41.135/0.609 Oracle Coherence 12.1.2.0 <Info> (thread=main, member=n/a): Loaded operational overrides from "file:/C:/home/oracle/workspace/InsertValue/build/classes/tangosol-coherence-override.xml"
    2012-08-15 15:53:41.135/0.609 Oracle Coherence 12.1.2.0 <D5> (thread=main, member=n/a): Optional configuration override "cache-factory-config.xml" is not specified
    2012-08-15 15:53:41.135/0.609 Oracle Coherence 12.1.2.0 <D5> (thread=main, member=n/a): Optional configuration override "cache-factory-builder-config.xml" is not specified
    2012-08-15 15:53:41.135/0.609 Oracle Coherence 12.1.2.0 <D5> (thread=main, member=n/a): Optional configuration override "/custom-mbeans.xml" is not specified
     
    Oracle Coherence Version 12.1.2.0 Build 36845
     Grid Edition: Development mode
    Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
     
    2012-08-15 15:53:41.369/0.843 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=n/a): Loaded cache configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/coherence-cache-config.xml"
    2012-08-15 15:53:41.526/1.000 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=n/a): Loaded cache configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/internal-txn-cache-config.xml"
    2012-08-15 15:53:43.197/2.671 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=n/a): Created cache factory com.tangosol.net.ExtensibleConfigurableCacheFactory
    2012-08-15 15:53:44.197/3.671 Oracle Coherence GE 12.1.2.0 <D4> (thread=main, member=n/a): TCMP bound to /130.35.99.202:8088 using SystemDatagramSocketProvider
    2012-08-15 15:53:47.635/7.109 Oracle Coherence GE 12.1.2.0 <Info> (thread=Cluster, member=n/a): Created a new cluster "cluster:0x47DB" with Member(Id=1, Timestamp=2012-08-15 15:53:44.338, Address=130.35.99.202:8088, MachineId=18578, Location=site:,machine:tpfaeffl-lap7,process:4920, Role=OracleHandsonMyFirstSample, Edition=Grid Edition, Mode=Development, CpuCount=2, SocketCount=1)
    2012-08-15 15:53:47.635/7.109 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=n/a): Started cluster Name=cluster:0x47DB
     
    Group{Address=224.12.1.0, Port=3155, TTL=4}
     
    MasterMemberSet(
      ThisMember=Member(Id=1, Timestamp=2012-08-15 15:53:44.338, Address=130.35.99.202:8088, MachineId=18578, Location=site:,machine:tpfaeffl-lap7,process:4920, Role=OracleHandsonMyFirstSample)
      OldestMember=Member(Id=1, Timestamp=2012-08-15 15:53:44.338, Address=130.35.99.202:8088, MachineId=18578, Location=site:,machine:tpfaeffl-lap7,process:4920, Role=OracleHandsonMyFirstSample)
      ActualMemberSet=MemberSet(Size=1
        Member(Id=1, Timestamp=2012-08-15 15:53:44.338, Address=130.35.99.202:8088, MachineId=18578, Location=site:,machine:tpfaeffl-lap7,process:4920, Role=OracleHandsonMyFirstSample)
        )
      MemberId|ServiceVersion|ServiceJoined|MemberState
        1|12.1.2|2012-08-15 15:53:44.338|JOINED
      RecycleMillis=1200000
      RecycleSet=MemberSet(Size=0
        )
      )
     
    TcpRing{Connections=[]}
    IpMonitor{Addresses=0}
     
    2012-08-15 15:53:47.838/7.312 Oracle Coherence GE 12.1.2.0 <D5> (thread=Invocation:Management, member=1): Service Management joined the cluster with senior service member 1
    2012-08-15 15:53:47.901/7.390 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=1): Loaded Reporter configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/reports/report-group.xml"
    2012-08-15 15:53:48.276/7.750 Oracle Coherence GE 12.1.2.0 <D5> (thread=DistributedCache, member=1): Service DistributedCache joined the cluster with senior service member 1
    Value in cache is Gene Smith
    2012-08-15 15:53:48.432/7.906 Oracle Coherence GE 12.1.2.0 <D4> (thread=ShutdownHook, member=1): ShutdownHook: stopping cluster node
    2012-08-15 15:53:48.447/7.921 Oracle Coherence GE 12.1.2.0 <D5> (thread=Cluster, member=n/a): Service Cluster left the cluster
    2012-08-15 15:53:48.447/7.921 Oracle Coherence GE 12.1.2.0 <D5> (thread=DistributedCache, member=n/a): Service DistributedCache left the cluster
    

3.2.2 Create a Program to Get Values from the Cache

To create a Java class that gets the value from your cache, instead of using a put and then a get method:

  1. Create another Java class with a main method named MyFirstSampleReader. See "Creating a Java Class" for detailed information. Example 3-3 illustrates a sample program.

    Example 3-3 Getting a Value from the Cache

    package com.oracle.handson;
    
    import com.tangosol.net.CacheFactory;
    import com.tangosol.net.NamedCache;
    
    public class MyFirstSampleReader {
        
        public MyFirstSampleReader() {
        }
    
        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");
                    
            System.out.println("Value in cache is " + myCache.get("Name"));
        }
    }
    
  2. Run the MyFirstSampleReader class in the Eclipse IDE: right-click the MyFirstSampleReader.java class in the editor and select Run As then Run Configuration.

    Enter MyFirstSampleReader in the Name field of the Run Configuration dialog box. Ensure that InsertValue appears in the Project field in the Main tab and that local storage is enabled and Cluster port is set to 3155 to limit Coherence to your own host in the Coherence tab. Click Apply, then Run.

    Example 3-4 illustrates the output from the program. Note that a null value is returned. Although the MyFirstSample program successfully created and populated the NamedCache cache, it only existed within the MyFirstSample process memory. When the MyFirstSample program terminated so did the cache.

    Example 3-4 Output of the MyFirstSampleReader Program

    2012-08-15 16:00:43.229/0.328 Oracle Coherence 12.1.2.0 <Info> (thread=main, member=n/a): Loaded operational configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/tangosol-coherence.xml"
    2012-08-15 16:00:43.291/0.390 Oracle Coherence 12.1.2.0 <Info> (thread=main, member=n/a): Loaded operational overrides from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/tangosol-coherence-override-dev.xml"
    2012-08-15 16:00:43.322/0.421 Oracle Coherence 12.1.2.0 <Info> (thread=main, member=n/a): Loaded operational overrides from "file:/C:/home/oracle/workspace/InsertValue/build/classes/tangosol-coherence-override.xml"
    2012-08-15 16:00:43.338/0.437 Oracle Coherence 12.1.2.0 <D5> (thread=main, member=n/a): Optional configuration override "cache-factory-config.xml" is not specified
    2012-08-15 16:00:43.338/0.437 Oracle Coherence 12.1.2.0 <D5> (thread=main, member=n/a): Optional configuration override "cache-factory-builder-config.xml" is not specified
    2012-08-15 16:00:43.338/0.437 Oracle Coherence 12.1.2.0 <D5> (thread=main, member=n/a): Optional configuration override "/custom-mbeans.xml" is not specified
     
    Oracle Coherence Version 12.1.2.0 Build 36845
     Grid Edition: Development mode
    Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
     
    2012-08-15 16:00:43.932/1.031 Oracle Coherence GE 12.1.2.0 <D4> (thread=main, member=n/a): TCMP bound to /130.35.99.202:8088 using SystemDatagramSocketProvider
    2012-08-15 16:00:47.338/4.437 Oracle Coherence GE 12.1.2.0 <Info> (thread=Cluster, member=n/a): Created a new cluster "cluster:0x47DB" with Member(Id=1, Timestamp=2012-08-15 16:00:44.057, Address=130.35.99.202:8088, MachineId=18578, Location=site:,machine:tpfaeffl-lap7,process:3752, Role=OracleHandsonMyFirstSampleReader, Edition=Grid Edition, Mode=Development, CpuCount=2, SocketCount=1)
    2012-08-15 16:00:47.338/4.437 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=n/a): Started cluster Name=cluster:0x47DB
     
    Group{Address=224.12.1.0, Port=3155, TTL=4}
     
    MasterMemberSet(
      ThisMember=Member(Id=1, Timestamp=2012-08-15 16:00:44.057, Address=130.35.99.202:8088, MachineId=18578, Location=site:,machine:tpfaeffl-lap7,process:3752, Role=OracleHandsonMyFirstSampleReader)
      OldestMember=Member(Id=1, Timestamp=2012-08-15 16:00:44.057, Address=130.35.99.202:8088, MachineId=18578, Location=site:,machine:tpfaeffl-lap7,process:3752, Role=OracleHandsonMyFirstSampleReader)
      ActualMemberSet=MemberSet(Size=1
        Member(Id=1, Timestamp=2012-08-15 16:00:44.057, Address=130.35.99.202:8088, MachineId=18578, Location=site:,machine:tpfaeffl-lap7,process:3752, Role=OracleHandsonMyFirstSampleReader)
        )
      MemberId|ServiceVersion|ServiceJoined|MemberState
        1|12.1.2|2012-08-15 16:00:44.057|JOINED
      RecycleMillis=1200000
      RecycleSet=MemberSet(Size=0
        )
      )
     
    TcpRing{Connections=[]}
    IpMonitor{Addresses=0}
     
    2012-08-15 16:00:47.385/4.484 Oracle Coherence GE 12.1.2.0 <D5> (thread=Invocation:Management, member=1): Service Management joined the cluster with senior service member 1
    2012-08-15 16:00:47.447/4.546 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=1): Loaded Reporter configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/reports/report-group.xml"
    2012-08-15 16:00:47.635/4.734 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=1): Loaded cache configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/coherence-cache-config.xml"
    2012-08-15 16:00:47.744/4.843 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=1): Loaded cache configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/internal-txn-cache-config.xml"
    2012-08-15 16:00:47.854/4.953 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=1): Created cache factory com.tangosol.net.ExtensibleConfigurableCacheFactory
    2012-08-15 16:00:47.963/5.062 Oracle Coherence GE 12.1.2.0 <D5> (thread=DistributedCache, member=1): Service DistributedCache joined the cluster with senior service member 1
    Value in cache is null
    
  3. Start the DefaultCacheServer that you created in "Launching a Cache Server in the Eclipse IDE". Ensure that InsertValue appears in the Project field in the Main tab and that local storage is enabled in the Coherence tab. Click Apply, then Run.

  4. Run MyFirstSample to put the value in the NamedCache, and then run MyFirstSampleReader to read the value from the cache. Note the output illustrated in Example 3-5. The Gene Smith value stored by MyFirstSample is returned by MyFirstSampleReader.

    Example 3-5 Output of MyFirstSampleReader Program with a Running Cache Server

    2012-08-15 16:13:52.760/0.313 Oracle Coherence 12.1.2.0 <Info> (thread=main, member=n/a): Loaded operational configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/tangosol-coherence.xml"
    2012-08-15 16:13:52.807/0.360 Oracle Coherence 12.1.2.0 <Info> (thread=main, member=n/a): Loaded operational overrides from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/tangosol-coherence-override-dev.xml"
    2012-08-15 16:13:52.854/0.407 Oracle Coherence 12.1.2.0 <Info> (thread=main, member=n/a): Loaded operational overrides from "file:/C:/home/oracle/workspace/InsertValue/build/classes/tangosol-coherence-override.xml"
    2012-08-15 16:13:52.854/0.407 Oracle Coherence 12.1.2.0 <D5> (thread=main, member=n/a): Optional configuration override "cache-factory-config.xml" is not specified
    2012-08-15 16:13:52.854/0.407 Oracle Coherence 12.1.2.0 <D5> (thread=main, member=n/a): Optional configuration override "cache-factory-builder-config.xml" is not specified
    2012-08-15 16:13:52.854/0.407 Oracle Coherence 12.1.2.0 <D5> (thread=main, member=n/a): Optional configuration override "/custom-mbeans.xml" is not specified
     
    Oracle Coherence Version 12.1.2.0 Build 36845
     Grid Edition: Development mode
    Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
     
    2012-08-15 16:13:53.416/0.969 Oracle Coherence GE 12.1.2.0 <D4> (thread=main, member=n/a): TCMP bound to /130.35.99.202:8090 using SystemDatagramSocketProvider
    2012-08-15 16:13:53.776/1.329 Oracle Coherence GE 12.1.2.0 <Info> (thread=Cluster, member=n/a): This Member(Id=3, Timestamp=2012-08-15 16:13:53.596, Address=130.35.99.202:8090, MachineId=18578, Location=site:,machine:tpfaeffl-lap7,process:5544, Role=OracleHandsonMyFirstSampleReader, Edition=Grid Edition, Mode=Development, CpuCount=2, SocketCount=1) joined cluster "cluster:0x47DB" with senior Member(Id=1, Timestamp=2012-08-15 16:13:03.869, Address=130.35.99.202:8088, MachineId=18578, Location=site:,machine:tpfaeffl-lap7,process:3468, Role=CoherenceServer, Edition=Grid Edition, Mode=Development, CpuCount=2, SocketCount=1)
    2012-08-15 16:13:53.979/1.532 Oracle Coherence GE 12.1.2.0 <D5> (thread=Cluster, member=n/a): Member 1 joined Service Management with senior member 1
    2012-08-15 16:13:53.979/1.532 Oracle Coherence GE 12.1.2.0 <D5> (thread=Cluster, member=n/a): Member 1 joined Service DistributedCache with senior member 1
    2012-08-15 16:13:53.979/1.532 Oracle Coherence GE 12.1.2.0 <D5> (thread=Cluster, member=n/a): Member 1 joined Service ReplicatedCache with senior member 1
    2012-08-15 16:13:53.979/1.532 Oracle Coherence GE 12.1.2.0 <D5> (thread=Cluster, member=n/a): Member 1 joined Service OptimisticCache with senior member 1
    2012-08-15 16:13:53.979/1.532 Oracle Coherence GE 12.1.2.0 <D5> (thread=Cluster, member=n/a): Member 1 joined Service InvocationService with senior member 1
    2012-08-15 16:13:53.979/1.532 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=n/a): Started cluster Name=cluster:0x47DB
     
    Group{Address=224.12.1.0, Port=3155, TTL=4}
     
    MasterMemberSet(
      ThisMember=Member(Id=3, Timestamp=2012-08-15 16:13:53.596, Address=130.35.99.202:8090, MachineId=18578, Location=site:,machine:tpfaeffl-lap7,process:5544, Role=OracleHandsonMyFirstSampleReader)
      OldestMember=Member(Id=1, Timestamp=2012-08-15 16:13:03.869, Address=130.35.99.202:8088, MachineId=18578, Location=site:,machine:tpfaeffl-lap7,process:3468, Role=CoherenceServer)
      ActualMemberSet=MemberSet(Size=2
        Member(Id=1, Timestamp=2012-08-15 16:13:03.869, Address=130.35.99.202:8088, MachineId=18578, Location=site:,machine:tpfaeffl-lap7,process:3468, Role=CoherenceServer)
        Member(Id=3, Timestamp=2012-08-15 16:13:53.596, Address=130.35.99.202:8090, MachineId=18578, Location=site:,machine:tpfaeffl-lap7,process:5544, Role=OracleHandsonMyFirstSampleReader)
        )
      MemberId|ServiceVersion|ServiceJoined|MemberState
        1|12.1.2|2012-08-15 16:13:03.869|JOINED,
        3|12.1.2|2012-08-15 16:13:53.596|JOINED
      RecycleMillis=1200000
      RecycleSet=MemberSet(Size=0
        )
      )
     
    TcpRing{Connections=[1]}
    IpMonitor{Addresses=0}
     
    2012-08-15 16:13:54.010/1.563 Oracle Coherence GE 12.1.2.0 <D5> (thread=Invocation:Management, member=3): Service Management joined the cluster with senior service member 1
    2012-08-15 16:13:54.026/1.579 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=3): Loaded Reporter configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/reports/report-group.xml"
    2012-08-15 16:13:54.104/1.657 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=3): Loaded cache configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/coherence-cache-config.xml"
    2012-08-15 16:13:54.260/1.813 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=3): Loaded cache configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/internal-txn-cache-config.xml"
    2012-08-15 16:13:54.385/1.938 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=3): Created cache factory com.tangosol.net.ExtensibleConfigurableCacheFactory
    2012-08-15 16:13:54.479/2.032 Oracle Coherence GE 12.1.2.0 <D5> (thread=DistributedCache, member=3): Service DistributedCache joined the cluster with senior service member 1
    2012-08-15 16:13:54.541/2.094 Oracle Coherence GE 12.1.2.0 <D4> (thread=DistributedCache, member=3): Asking member 1 for primary ownership of PartitionSet{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127}
    Value in cache is Gene Smith
    2012-08-15 16:13:54.619/2.172 Oracle Coherence GE 12.1.2.0 <D4> (thread=ShutdownHook, member=3): ShutdownHook: stopping cluster node
    

    This should not be the case for a process that joins the cluster only to perform an operation, such as entering a value and then terminating, like MyFirstSample. By default, all processes start as storage-enabled. The process can store data as part of the cluster. Modify the process so that it is not storage-enabled.

    1. Right click the MyFirstSample class, select Run As then Run Configurations. See "Creating a Runtime Configuration" for detailed information.

    2. In the Coherence tab, select Disabled (cache client) under Local storage.

      This similar to setting the Java parameter -Dtangosol.coherence.distributed.localstorage=false.

  5. Shut down any running cache servers and rerun your MyFirstSample class.

    You will receive a message similar to the one in Example 3-6 indicating that storage is not enabled on the cluster, because you have set this member to be storage-disabled.

    Example 3-6 Output of the MyFirstSample Class with Cache Storage Disabled

    2012-08-15 16:22:38.994/0.328 Oracle Coherence 12.1.2.0 <Info> (thread=main, member=n/a): Loaded operational configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/tangosol-coherence.xml"
    2012-08-15 16:22:39.041/0.375 Oracle Coherence 12.1.2.0 <Info> (thread=main, member=n/a): Loaded operational overrides from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/tangosol-coherence-override-dev.xml"
    2012-08-15 16:22:39.088/0.422 Oracle Coherence 12.1.2.0 <Info> (thread=main, member=n/a): Loaded operational overrides from "file:/C:/home/oracle/workspace/InsertValue/build/classes/tangosol-coherence-override.xml"
    2012-08-15 16:22:39.088/0.422 Oracle Coherence 12.1.2.0 <D5> (thread=main, member=n/a): Optional configuration override "cache-factory-config.xml" is not specified
    2012-08-15 16:22:39.088/0.422 Oracle Coherence 12.1.2.0 <D5> (thread=main, member=n/a): Optional configuration override "cache-factory-builder-config.xml" is not specified
    2012-08-15 16:22:39.088/0.422 Oracle Coherence 12.1.2.0 <D5> (thread=main, member=n/a): Optional configuration override "/custom-mbeans.xml" is not specified
     
    Oracle Coherence Version 12.1.2.0 Build 36845
     Grid Edition: Development mode
    Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
     
    2012-08-15 16:22:39.307/0.641 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=n/a): Loaded cache configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/coherence-cache-config.xml"
    2012-08-15 16:22:39.401/0.735 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=n/a): Loaded cache configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/internal-txn-cache-config.xml"
    2012-08-15 16:22:39.713/1.047 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=n/a): Created cache factory com.tangosol.net.ExtensibleConfigurableCacheFactory
    2012-08-15 16:22:39.947/1.281 Oracle Coherence GE 12.1.2.0 <D4> (thread=main, member=n/a): TCMP bound to /130.35.99.202:8088 using SystemDatagramSocketProvider
    2012-08-15 16:22:43.338/4.672 Oracle Coherence GE 12.1.2.0 <Info> (thread=Cluster, member=n/a): Created a new cluster "cluster:0x47DB" with Member(Id=1, Timestamp=2012-08-15 16:22:40.057, Address=130.35.99.202:8088, MachineId=18578, Location=site:,machine:tpfaeffl-lap7,process:3348, Role=OracleHandsonMyFirstSample, Edition=Grid Edition, Mode=Development, CpuCount=2, SocketCount=1)
    2012-08-15 16:22:43.338/4.672 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=n/a): Started cluster Name=cluster:0x47DB
     
    Group{Address=224.12.1.0, Port=3155, TTL=4}
     
    MasterMemberSet(
      ThisMember=Member(Id=1, Timestamp=2012-08-15 16:22:40.057, Address=130.35.99.202:8088, MachineId=18578, Location=site:,machine:tpfaeffl-lap7,process:3348, Role=OracleHandsonMyFirstSample)
      OldestMember=Member(Id=1, Timestamp=2012-08-15 16:22:40.057, Address=130.35.99.202:8088, MachineId=18578, Location=site:,machine:tpfaeffl-lap7,process:3348, Role=OracleHandsonMyFirstSample)
      ActualMemberSet=MemberSet(Size=1
        Member(Id=1, Timestamp=2012-08-15 16:22:40.057, Address=130.35.99.202:8088, MachineId=18578, Location=site:,machine:tpfaeffl-lap7,process:3348, Role=OracleHandsonMyFirstSample)
        )
      MemberId|ServiceVersion|ServiceJoined|MemberState
        1|12.1.2|2012-08-15 16:22:40.057|JOINED
      RecycleMillis=1200000
      RecycleSet=MemberSet(Size=0
        )
      )
     
    TcpRing{Connections=[]}
    IpMonitor{Addresses=0}
     
    2012-08-15 16:22:43.416/4.750 Oracle Coherence GE 12.1.2.0 <D5> (thread=Invocation:Management, member=1): Service Management joined the cluster with senior service member 1
    2012-08-15 16:22:43.494/4.828 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=1): Loaded Reporter configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/reports/report-group.xml"
    2012-08-15 16:22:43.651/4.985 Oracle Coherence GE 12.1.2.0 <D5> (thread=DistributedCache, member=1): Service DistributedCache joined the cluster with senior service member 1
    Exception in thread "main" com.tangosol.net.RequestPolicyException: No storage-enabled nodes exist for service DistributedCache
       at com.tangosol.coherence.component.util.daemon.queueProcessor.service.grid.partitionedService.PartitionedCache$BinaryMap.onMissingStorage(PartitionedCache.CDB:32)
       at com.tangosol.coherence.component.util.daemon.queueProcessor.service.grid.partitionedService.PartitionedCache$BinaryMap.ensureRequestTarget(PartitionedCache.CDB:50)
       at com.tangosol.coherence.component.util.daemon.queueProcessor.service.grid.partitionedService.PartitionedCache$BinaryMap.put(PartitionedCache.CDB:22)
       at com.tangosol.coherence.component.util.daemon.queueProcessor.service.grid.partitionedService.PartitionedCache$BinaryMap.put(PartitionedCache.CDB:1)
       at com.oracle.common.collections.ConverterCollections$ConverterMap.put(ConverterCollections.java:1534)
       at com.tangosol.coherence.component.util.daemon.queueProcessor.service.grid.partitionedService.PartitionedCache$ViewMap.put(PartitionedCache.CDB:1)
       at com.tangosol.coherence.component.util.SafeNamedCache.put(SafeNamedCache.CDB:1)
       at com.oracle.handson.MyFirstSample.main(MyFirstSample.java:16)
    
  6. Restart the DefaultCacheServer cache server and run MyFirstSample and MyFirstSampleReader again. You should now see that the data is persisted between running the two Java examples.

3.3 Creating Your First Coherence-Based Java Application

In this exercise, you develop a simple Java console-based application to access, update, and remove simple types of information from a Coherence clustered cache.

To perform this exercise, you must complete "Testing a Coherence Installation".

Unlike client/server applications, in which client applications typically connect and disconnect from a server application, Coherence-based clustered applications simply ensure they are in a cluster, after which they can use the services of the cluster. Coherence-based applications typically do not connect to a cluster of applications; they become part of the cluster.

  1. Create the Console Application

  2. Run the Console Application

3.3.1 Create the Console Application

To create a Java console-based application to access, update, and remove simple types of information from a Coherence clustered cache:

  1. Examine the methods in the CacheFactory class using the Coherence Java documentation (Javadoc) that is shipped in the ...\coherence\doc folder.

  2. Write a simple Java console application (Java class) called YourFirstCoherenceApplication that uses the CacheFactory class to join a cluster (using the ensureCluster method), and then leave the cluster (using the shutdown method). See "Creating a Java Class" for detailed information on creating a Java class.

    1. Examine the methods that are available in the NamedCache interface using the Javadoc.

    2. Extend your application to use the CacheFactory method getCache to acquire a NamedCache for the cache called mycache (the same cache name used in the exercise "Testing a Coherence Installation").

    3. With the NamedCache instance, use the get method to retrieve the value for the key message (the same key used in the exercise "Testing a Coherence Installation").

    4. Write the value to standard output using the System.out.println(….) method.

      Example 3-7 illustrates a sample Coherence-based Java application:

      Example 3-7 A Coherence-Based Java Application

      package com.oracle.handson;
      
      import com.tangosol.net.CacheFactory;
      import com.tangosol.net.NamedCache;
      
      public class YourFirstCoherenceApplication {
      public YourFirstCoherenceApplication() {
      }
      
         public static void main(String[] args) {
      
            CacheFactory.ensureCluster();
      
            NamedCache myCache = CacheFactory.getCache("mycache");
      
            String message = (String)myCache.get("message");
      
            System.out.println(message);
      
            CacheFactory.shutdown();
      YourFirstCoherenceApplication yourfirstcoherenceapplication = new YourFirstCoherenceApplication(); 
         }
      
      }
      

3.3.2 Run the Console Application

To run the Coherence application.

  1. Start the DefaultCacheServer cache server that you created in "Launching a Cache Server in the Eclipse IDE".

  2. Create a run configuration for a cache client that uses Coherence Query Language and the QueryHelper API. Right click the project and select Run As then Run Configurations.

    Note:

    For more information about Coherence Query Language, see "Using Coherence Query Language" in Oracle Fusion Middleware Developing Applications with Oracle Coherence.

    1. In the Run Configurations dialog box click Oracle Coherence then the New launch configuration icon. Enter QueryPlus as the Name of the configuration.

    2. In the Main tab, under Project click the Browse button and select the InsertValue project. Under Main class, select Include system libraries when searching for a main class and click the Search button. In the Select Main Type dialog box, enter QueryPlus and select QueryPlus - com.tangosol.coherence.dslquery. Click OK. The Main tab should look similar to Figure 3-1.

      Figure 3-1 Main Tab for the Query Client Configuration

      Main Tab for the Query Client Configuration
      Description of "Figure 3-1 Main Tab for the Query Client Configuration"

    3. In the Coherence tab, select Disabled (cache client) under Local storage. Enter a unique value for the Cluster port (the value must be the same as the value defined for the cache server you defined in the previous section).

    4. In the Arguments tab, enter -showversion in the VM arguments field.

    5. In the Common tab, select Shared file and click the Browse button to navigate to the \InsertValue project name. Ensure that the Allocate console checkbox is selected. Click Apply.

  3. Click Run to start the QueryPlus client. You should see output similar to Example 3-8 in the Eclipse Console.

    Example 3-8 Output for the QueryPlus Cache Client

    java version "1.7.0"
    Java(TM) SE Runtime Environment (build 1.7.0-b147)
    Java HotSpot(TM) Client VM (build 21.0-b17, mixed mode, sharing)
    
    Coherence Command Line Tooljline library cannot be loaded, so you cannot use the arrow keys for line editing and history. 
    
    CohQL> 
    
  4. At the CohQL> prompt, enter the following command to create a cache named mycache and to connect to it.

    CohQL> create cache "mycache"
    
  5. Create a run configuration for YourFirstCoherenceApplication. In the Run Configurations dialog box, enter YourFirstCoherenceApplication in the Name field. In the Main tab, enter InsertValue in the Project field and com.oracle.handson.YourFirstCoherenceApplication as the Main class.

    In the Coherence tab, select Enabled (cache server) under Local storage, and enter a unique value for the Cluster port (this must be the same value that you used for the cache server and cache client).

  6. Execute YourFirstCoherenceApplication from the Eclipse IDE and view the result.

    Example 3-9 illustrates the output from YourFirstCoherenceApplication. The output indicates that there is no data in the cache for the message key.

    Example 3-9 Output of the YourFirstCoherenceApplication Class

    ...
    TcpRing{Connections=[2]}
    IpMonitor{AddressListSize=0}
     
    2012-08-15 17:20:53.276/1.625 Oracle Coherence GE 12.1.2.0 <D5> (thread=Invocation:Management, member=3): Service Management joined the cluster with senior service member 1
    2012-08-15 17:20:53.307/1.656 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=3): Loaded Reporter configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/reports/report-group.xml"
    2012-08-15 17:20:53.463/1.812 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=3): Loaded cache configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/coherence-cache-config.xml"
    2012-08-15 17:20:53.604/1.953 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=3): Loaded cache configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/internal-txn-cache-config.xml"
    2012-08-15 17:20:53.744/2.093 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=3): Created cache factory com.tangosol.net.ExtensibleConfigurableCacheFactory
    2012-08-15 17:20:53.838/2.187 Oracle Coherence GE 12.1.2.0 <D5> (thread=DistributedCache, member=3): Service DistributedCache joined the cluster with senior service member 1
    2012-08-15 17:20:53.885/2.234 Oracle Coherence GE 12.1.2.0 <D4> (thread=DistributedCache, member=3): Asking member 1 for primary ownership of PartitionSet{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127}
    null
    2012-08-15 17:20:54.041/2.390 Oracle Coherence GE 12.1.2.0 <D5> (thread=Invocation:Management, member=n/a): Service Management left the cluster
    2012-08-15 17:20:54.057/2.406 Oracle Coherence GE 12.1.2.0 <Info> (thread=DistributedCache, member=n/a): Remains to transfer before shutting down: 0 primary partitions, 129 backup partitions
    2012-08-15 17:20:54.088/2.437 Oracle Coherence GE 12.1.2.0 <D5> (thread=DistributedCache, member=n/a): Service DistributedCache left the cluster
    2012-08-15 17:20:54.135/2.484 Oracle Coherence GE 12.1.2.0 <D5> (thread=Cluster, member=n/a): Service Cluster left the cluster
    
  7. Using the running QueryPlus cache client in the Eclipse Console, change the key message. For example, enter the following at the CohQL> prompt:

    CohQL> insert into "mycache" key "message" value "hello"
    

    Rerun YourFirstCoherenceApplication from the Eclipse IDE to see the changed values. Example 3-10 illustrates that the cache now holds the value hello for the key message.

    Example 3-10 Output of the YourFirstCoherenceApplication Class with a New Key Value

    ...
    TcpRing{Connections=[2]}
    IpMonitor{Addresses=0}
     
    2012-08-15 17:37:39.760/2.109 Oracle Coherence GE 12.1.2.0 <D5> (thread=Invocation:Management, member=3): Service Management joined the cluster with senior service member 1
    2012-08-15 17:37:39.807/2.156 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=3): Loaded Reporter configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/reports/report-group.xml"
    2012-08-15 17:37:40.244/2.593 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=3): Loaded cache configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/coherence-cache-config.xml"
    2012-08-15 17:37:40.338/2.687 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=3): Loaded cache configuration from "jar:file:/C:/oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/internal-txn-cache-config.xml"
    2012-08-15 17:37:40.541/2.890 Oracle Coherence GE 12.1.2.0 <Info> (thread=main, member=3): Created cache factory com.tangosol.net.ExtensibleConfigurableCacheFactory
    2012-08-15 17:37:40.729/3.078 Oracle Coherence GE 12.1.2.0 <D5> (thread=DistributedCache, member=3): Service DistributedCache joined the cluster with senior service member 1
    hello
    2012-08-15 17:37:40.838/3.187 Oracle Coherence GE 12.1.2.0 <D4> (thread=DistributedCache, member=3): Asking member 1 for primary ownership of PartitionSet{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127}
    2012-08-15 17:37:40.979/3.328 Oracle Coherence GE 12.1.2.0 <D5> (thread=Invocation:Management, member=n/a): Service Management left the cluster
    2012-08-15 17:37:41.010/3.359 Oracle Coherence GE 12.1.2.0 <Info> (thread=DistributedCache, member=n/a): Remains to transfer before shutting down: 0 primary partitions, 129 backup partitions
    2012-08-15 17:37:41.041/3.390 Oracle Coherence GE 12.1.2.0 <D5> (thread=DistributedCache, member=n/a): Service DistributedCache left the cluster
    2012-08-15 17:37:41.104/3.453 Oracle Coherence GE 12.1.2.0 <D5> (thread=Cluster, member=n/a): Service Cluster left the cluster
    
  8. In the run configuration for YourFirstCoherenceApplication, change the value of the Local storage from Enabled to Disabled. Notice that the output is the same as the previous run.

  9. Shut down your cache server and cache client instances. Restart the cache server and then rerun YourFirstCoherenceApplication (with the new value for Local storage). Note the output is now null.

  10. If you change the value of the message key in your application (using the put method), verify that the new value available through the cache client.

    1. For example, comment out the get method and add the put method.

      //String message = (String)myCache.get("message");
        String message = (String)myCache.put("message", "bye");
      
    2. Run YourFirstCoherenceApplication.

    3. Run the get command in the QueryPlus cache client.

      select key(), value() from "mycache" where key() is "message"
      

      The output bye is displayed.