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

    2013-11-14 14:46:52.652/0.343 Oracle Coherence 12.1.3.0.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"
    2013-11-14 14:46:52.746/0.437 Oracle Coherence 12.1.3.0.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"
    2013-11-14 14:46:52.855/0.546 Oracle Coherence 12.1.3.0.0 <Info> (thread=main, member=n/a): Loaded operational overrides from "file:/C:/home/oracle/workspace/InsertValue/build/classes/tangosol-coherence-override.xml"
    2013-11-14 14:46:52.855/0.546 Oracle Coherence 12.1.3.0.0 <D5> (thread=main, member=n/a): Optional configuration override "cache-factory-config.xml" is not specified
    2013-11-14 14:46:52.855/0.546 Oracle Coherence 12.1.3.0.0 <D5> (thread=main, member=n/a): Optional configuration override "cache-factory-builder-config.xml" is not specified
    2013-11-14 14:46:52.855/0.546 Oracle Coherence 12.1.3.0.0 <D5> (thread=main, member=n/a): Optional configuration override "/custom-mbeans.xml" is not specified
     
    Oracle Coherence Version 12.1.3.0.0 Build 48392
     Grid Edition: Development mode
    Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
     
    2013-11-14 14:46:53.511/1.202 Oracle Coherence GE 12.1.3.0.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"
    2013-11-14 14:46:53.652/1.343 Oracle Coherence GE 12.1.3.0.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"
    2013-11-14 14:46:54.369/2.060 Oracle Coherence GE 12.1.3.0.0 <Info> (thread=main, member=n/a): Created cache factory com.tangosol.net.ExtensibleConfigurableCacheFactory
    2013-11-14 14:46:55.336/3.027 Oracle Coherence GE 12.1.3.0.0 <D4> (thread=main, member=n/a): TCMP bound to /10.159.162.203:8088 using SystemDatagramSocketProvider
    2013-11-14 14:46:59.237/6.928 Oracle Coherence GE 12.1.3.0.0 <Info> (thread=Cluster, member=n/a): Created a new cluster "cluster:0x47DB" with Member(Id=1, Timestamp=2013-11-14 14:46:55.665, Address=10.159.162.203:8088, MachineId=47251, Location=site:,machine:TPFAEFFL-LAP,process:8184, Role=OracleHandsonMyFirstSample, Edition=Grid Edition, Mode=Development, CpuCount=4, SocketCount=4)
    2013-11-14 14:46:59.237/6.928 Oracle Coherence GE 12.1.3.0.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=2013-11-14 14:46:55.665, Address=10.159.162.203:8088, MachineId=47251, Location=site:,machine:TPFAEFFL-LAP,process:8184, Role=OracleHandsonMyFirstSample)
      OldestMember=Member(Id=1, Timestamp=2013-11-14 14:46:55.665, Address=10.159.162.203:8088, MachineId=47251, Location=site:,machine:TPFAEFFL-LAP,process:8184, Role=OracleHandsonMyFirstSample)
      ActualMemberSet=MemberSet(Size=1
        Member(Id=1, Timestamp=2013-11-14 14:46:55.665, Address=10.159.162.203:8088, MachineId=47251, Location=site:,machine:TPFAEFFL-LAP,process:8184, Role=OracleHandsonMyFirstSample)
        )
      MemberId|ServiceVersion|ServiceJoined|MemberState
        1|12.1.3|2013-11-14 14:46:55.665|JOINED
      RecycleMillis=1200000
      RecycleSet=MemberSet(Size=0
        )
      )
     
    TcpRing{Connections=[]}
    IpMonitor{Addresses=0}
     
    2013-11-14 14:46:59.270/6.961 Oracle Coherence GE 12.1.3.0.0 <D5> (thread=Invocation:Management, member=1): Service Management joined the cluster with senior service member 1
    2013-11-14 14:46:59.301/6.992 Oracle Coherence GE 12.1.3.0.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"
    2013-11-14 14:46:59.784/7.475 Oracle Coherence GE 12.1.3.0.0 <Info> (thread=NameService:TcpAcceptor, member=1): TcpAcceptor now listening for connections on 10.159.162.203:8088.3
    2013-11-14 14:47:00.159/7.850 Oracle Coherence GE 12.1.3.0.0 <D5> (thread=DistributedCache, member=1): Service DistributedCache joined the cluster with senior service member 1
    2013-11-14 14:47:00.190/7.881 Oracle Coherence GE 12.1.3.0.0 <D5> (thread=DistributedCache, member=1): This member has become the distribution coordinator for MemberSet(Size=1
      Member(Id=1, Timestamp=2013-11-14 14:46:55.665, Address=10.159.162.203:8088, MachineId=47251, Location=site:,machine:TPFAEFFL-LAP,process:8184, Role=OracleHandsonMyFirstSample)
      )
    Value in cache is Gene Smith
    

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 com.oracle.handson.MyFirstSampleReader appears in the Main class field. In the Coherence tab, ensure that local storage is enabled and Cluster port is set to 3155 to limit Coherence to your own host. 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

    2013-11-14 14:55:44.990/0.327 Oracle Coherence 12.1.3.0.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"
    2013-11-14 14:55:45.099/0.436 Oracle Coherence 12.1.3.0.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"
    2013-11-14 14:55:45.193/0.530 Oracle Coherence 12.1.3.0.0 <Info> (thread=main, member=n/a): Loaded operational overrides from "file:/C:/home/oracle/workspace/InsertValue/build/classes/tangosol-coherence-override.xml"
    2013-11-14 14:55:45.209/0.546 Oracle Coherence 12.1.3.0.0 <D5> (thread=main, member=n/a): Optional configuration override "cache-factory-config.xml" is not specified
    2013-11-14 14:55:45.209/0.546 Oracle Coherence 12.1.3.0.0 <D5> (thread=main, member=n/a): Optional configuration override "cache-factory-builder-config.xml" is not specified
    2013-11-14 14:55:45.209/0.546 Oracle Coherence 12.1.3.0.0 <D5> (thread=main, member=n/a): Optional configuration override "/custom-mbeans.xml" is not specified
     
    Oracle Coherence Version 12.1.3.0.0 Build 48392
     Grid Edition: Development mode
    Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
     
    2013-11-14 14:55:46.754/2.091 Oracle Coherence GE 12.1.3.0.0 <D4> (thread=main, member=n/a): TCMP bound to /10.159.162.203:8088 using SystemDatagramSocketProvider
    2013-11-14 14:55:50.966/6.303 Oracle Coherence GE 12.1.3.0.0 <Info> (thread=Cluster, member=n/a): Created a new cluster "cluster:0x47DB" with Member(Id=1, Timestamp=2013-11-14 14:55:47.097, Address=10.159.162.203:8088, MachineId=47251, Location=site:,machine:TPFAEFFL-LAP,process:7304, Role=OracleHandsonMyFirstSampleReader, Edition=Grid Edition, Mode=Development, CpuCount=4, SocketCount=4)
    2013-11-14 14:55:50.966/6.303 Oracle Coherence GE 12.1.3.0.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=2013-11-14 14:55:47.097, Address=10.159.162.203:8088, MachineId=47251, Location=site:,machine:TPFAEFFL-LAP,process:7304, Role=OracleHandsonMyFirstSampleReader)
      OldestMember=Member(Id=1, Timestamp=2013-11-14 14:55:47.097, Address=10.159.162.203:8088, MachineId=47251, Location=site:,machine:TPFAEFFL-LAP,process:7304, Role=OracleHandsonMyFirstSampleReader)
      ActualMemberSet=MemberSet(Size=1
        Member(Id=1, Timestamp=2013-11-14 14:55:47.097, Address=10.159.162.203:8088, MachineId=47251, Location=site:,machine:TPFAEFFL-LAP,process:7304, Role=OracleHandsonMyFirstSampleReader)
        )
      MemberId|ServiceVersion|ServiceJoined|MemberState
        1|12.1.3|2013-11-14 14:55:47.097|JOINED
      RecycleMillis=1200000
      RecycleSet=MemberSet(Size=0
        )
      )
     
    TcpRing{Connections=[]}
    IpMonitor{Addresses=0}
     
    2013-11-14 14:55:51.014/6.351 Oracle Coherence GE 12.1.3.0.0 <D5> (thread=Invocation:Management, member=1): Service Management joined the cluster with senior service member 1
    2013-11-14 14:55:51.030/6.367 Oracle Coherence GE 12.1.3.0.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"
    2013-11-14 14:55:51.482/6.819 Oracle Coherence GE 12.1.3.0.0 <Info> (thread=NameService:TcpAcceptor, member=1): TcpAcceptor now listening for connections on 10.159.162.203:8088.3
    2013-11-14 14:55:51.841/7.178 Oracle Coherence GE 12.1.3.0.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"
    2013-11-14 14:55:51.966/7.303 Oracle Coherence GE 12.1.3.0.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"
    2013-11-14 14:55:52.168/7.505 Oracle Coherence GE 12.1.3.0.0 <Info> (thread=main, member=1): Created cache factory com.tangosol.net.ExtensibleConfigurableCacheFactory
    2013-11-14 14:55:52.232/7.569 Oracle Coherence GE 12.1.3.0.0 <D5> (thread=DistributedCache, member=1): Service DistributedCache joined the cluster with senior service member 1
    2013-11-14 14:55:52.263/7.600 Oracle Coherence GE 12.1.3.0.0 <D5> (thread=DistributedCache, member=1): This member has become the distribution coordinator for MemberSet(Size=1
      Member(Id=1, Timestamp=2013-11-14 14:55:47.097, Address=10.159.162.203:8088, MachineId=47251, Location=site:,machine:TPFAEFFL-LAP,process:7304, Role=OracleHandsonMyFirstSampleReader)
      )
    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

    2013-11-14 15:07:24.940/0.374 Oracle Coherence 12.1.3.0.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"
    2013-11-14 15:07:25.034/0.468 Oracle Coherence 12.1.3.0.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"
    2013-11-14 15:07:25.112/0.546 Oracle Coherence 12.1.3.0.0 <Info> (thread=main, member=n/a): Loaded operational overrides from "file:/C:/home/oracle/workspace/InsertValue/build/classes/tangosol-coherence-override.xml"
    2013-11-14 15:07:25.112/0.546 Oracle Coherence 12.1.3.0.0 <D5> (thread=main, member=n/a): Optional configuration override "cache-factory-config.xml" is not specified
    2013-11-14 15:07:25.112/0.546 Oracle Coherence 12.1.3.0.0 <D5> (thread=main, member=n/a): Optional configuration override "cache-factory-builder-config.xml" is not specified
    2013-11-14 15:07:25.112/0.546 Oracle Coherence 12.1.3.0.0 <D5> (thread=main, member=n/a): Optional configuration override "/custom-mbeans.xml" is not specified
     
    Oracle Coherence Version 12.1.3.0.0 Build 48392
     Grid Edition: Development mode
    Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
     
    2013-11-14 15:07:26.922/2.356 Oracle Coherence GE 12.1.3.0.0 <D4> (thread=main, member=n/a): TCMP bound to /10.159.162.203:8090 using SystemDatagramSocketProvider
    2013-11-14 15:07:28.343/3.777 Oracle Coherence GE 12.1.3.0.0 <D5> (thread=Cluster, member=n/a): Member(Id=1, Timestamp=2013-11-14 15:06:21.971, Address=10.159.162.203:8088, MachineId=47251, Location=site:,machine:TPFAEFFL-LAP,process:6980, Role=CoherenceServer) joined Cluster with senior member 1
    2013-11-14 15:07:28.343/3.777 Oracle Coherence GE 12.1.3.0.0 <Info> (thread=Cluster, member=n/a): This Member(Id=3, Timestamp=2013-11-14 15:07:28.156, Address=10.159.162.203:8090, MachineId=47251, Location=site:,machine:TPFAEFFL-LAP,process:8096, Role=OracleHandsonMyFirstSampleReader, Edition=Grid Edition, Mode=Development, CpuCount=4, SocketCount=4) joined cluster "cluster:0x47DB" with senior Member(Id=1, Timestamp=2013-11-14 15:06:21.971, Address=10.159.162.203:8088, MachineId=47251, Location=site:,machine:TPFAEFFL-LAP,process:6980, Role=CoherenceServer, Edition=Grid Edition, Mode=Development, CpuCount=4, SocketCount=4)
    2013-11-14 15:07:28.422/3.856 Oracle Coherence GE 12.1.3.0.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=2013-11-14 15:07:28.156, Address=10.159.162.203:8090, MachineId=47251, Location=site:,machine:TPFAEFFL-LAP,process:8096, Role=OracleHandsonMyFirstSampleReader)
      OldestMember=Member(Id=1, Timestamp=2013-11-14 15:06:21.971, Address=10.159.162.203:8088, MachineId=47251, Location=site:,machine:TPFAEFFL-LAP,process:6980, Role=CoherenceServer)
      ActualMemberSet=MemberSet(Size=2
        Member(Id=1, Timestamp=2013-11-14 15:06:21.971, Address=10.159.162.203:8088, MachineId=47251, Location=site:,machine:TPFAEFFL-LAP,process:6980, Role=CoherenceServer)
        Member(Id=3, Timestamp=2013-11-14 15:07:28.156, Address=10.159.162.203:8090, MachineId=47251, Location=site:,machine:TPFAEFFL-LAP,process:8096, Role=OracleHandsonMyFirstSampleReader)
        )
      MemberId|ServiceVersion|ServiceJoined|MemberState
        1|12.1.3|2013-11-14 15:06:21.971|JOINED,
        3|12.1.3|2013-11-14 15:07:28.156|JOINED
      RecycleMillis=1200000
      RecycleSet=MemberSet(Size=1
        Member(Id=2, Timestamp=2013-11-14 15:06:53.752, Address=10.159.162.203:8090, MachineId=47251)
        )
      )
     
    TcpRing{Connections=[1]}
    IpMonitor{Addresses=0}
     
    2013-11-14 15:07:28.453/3.887 Oracle Coherence GE 12.1.3.0.0 <D5> (thread=Invocation:Management, member=3): Service Management joined the cluster with senior service member 1
    2013-11-14 15:07:28.469/3.903 Oracle Coherence GE 12.1.3.0.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"
    2013-11-14 15:07:28.906/4.340 Oracle Coherence GE 12.1.3.0.0 <Info> (thread=NameService:TcpAcceptor, member=3): TcpAcceptor now listening for connections on 10.159.162.203:8090.3
    2013-11-14 15:07:29.280/4.714 Oracle Coherence GE 12.1.3.0.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"
    2013-11-14 15:07:29.389/4.823 Oracle Coherence GE 12.1.3.0.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"
    2013-11-14 15:07:29.593/5.027 Oracle Coherence GE 12.1.3.0.0 <Info> (thread=main, member=3): Created cache factory com.tangosol.net.ExtensibleConfigurableCacheFactory
    2013-11-14 15:07:29.671/5.105 Oracle Coherence GE 12.1.3.0.0 <D5> (thread=DistributedCache, member=3): Service DistributedCache joined the cluster with senior service member 1
    Value in cache is Gene Smith
    2013-11-14 15:07:29.702/5.136 Oracle Coherence GE 12.1.3.0.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

    ...
    TcpRing{Connections=[]}
    IpMonitor{Addresses=0}
     
    2013-11-14 15:11:40.253/7.039 Oracle Coherence GE 12.1.3.0.0 <D5> (thread=Invocation:Management, member=1): Service Management joined the cluster with senior service member 1
    2013-11-14 15:11:40.284/7.070 Oracle Coherence GE 12.1.3.0.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"
    2013-11-14 15:11:40.721/7.507 Oracle Coherence GE 12.1.3.0.0 <Info> (thread=NameService:TcpAcceptor, member=1): TcpAcceptor now listening for connections on 10.159.162.203:8088.3
    2013-11-14 15:11:41.080/7.866 Oracle Coherence GE 12.1.3.0.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:27)
            at com.tangosol.coherence.component.util.daemon.queueProcessor.service.grid.partitionedService.PartitionedCache$BinaryMap.ensureRequestTarget(PartitionedCache.CDB:43)
            at com.tangosol.coherence.component.util.daemon.queueProcessor.service.grid.partitionedService.PartitionedCache$BinaryMap.put(PartitionedCache.CDB:23)
            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:1531)
            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_25"
    Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
    Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
    
    Coherence Command Line Tool
    jline 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{Addresses=0}
     
    2013-11-14 15:41:33.008/3.793 Oracle Coherence GE 12.1.3.0.0 <D5> (thread=Invocation:Management, member=3): Service Management joined the cluster with senior service member 1
    2013-11-14 15:41:33.039/3.824 Oracle Coherence GE 12.1.3.0.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"
    2013-11-14 15:41:33.570/4.355 Oracle Coherence GE 12.1.3.0.0 <Info> (thread=NameService:TcpAcceptor, member=3): TcpAcceptor now listening for connections on 10.159.162.203:8092.3
    2013-11-14 15:41:33.975/4.760 Oracle Coherence GE 12.1.3.0.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"
    2013-11-14 15:41:34.132/4.917 Oracle Coherence GE 12.1.3.0.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"
    2013-11-14 15:41:34.475/5.260 Oracle Coherence GE 12.1.3.0.0 <Info> (thread=main, member=3): Created cache factory com.tangosol.net.ExtensibleConfigurableCacheFactory
    2013-11-14 15:41:34.538/5.323 Oracle Coherence GE 12.1.3.0.0 <D5> (thread=DistributedCache, member=3): Service DistributedCache joined the cluster with senior service member 1
    null
    2013-11-14 15:41:44.601/15.386 Oracle Coherence GE 12.1.3.0.0 <D5> (thread=DistributedCache, member=n/a): Service DistributedCache left the cluster
    2013-11-14 15:41:44.601/15.386 Oracle Coherence GE 12.1.3.0.0 <D5> (thread=Invocation:Management, member=n/a): Service Management left the cluster
    2013-11-14 15:41:44.651/15.436 Oracle Coherence GE 12.1.3.0.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}
     
    2013-11-14 15:45:01.006/3.986 Oracle Coherence GE 12.1.3.0.0 <D5> (thread=Invocation:Management, member=4): Service Management joined the cluster with senior service member 1
    2013-11-14 15:45:01.037/4.017 Oracle Coherence GE 12.1.3.0.0 <Info> (thread=main, member=4): Loaded Reporter configuration from "jar:file:/C:/Oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/reports/report-group.xml"
    2013-11-14 15:45:01.583/4.563 Oracle Coherence GE 12.1.3.0.0 <Info> (thread=NameService:TcpAcceptor, member=4): TcpAcceptor now listening for connections on 10.159.162.203:8092.3
    2013-11-14 15:45:01.973/4.953 Oracle Coherence GE 12.1.3.0.0 <Info> (thread=main, member=4): Loaded cache configuration from "jar:file:/C:/Oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/coherence-cache-config.xml"
    2013-11-14 15:45:02.083/5.063 Oracle Coherence GE 12.1.3.0.0 <Info> (thread=main, member=4): Loaded cache configuration from "jar:file:/C:/Oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/internal-txn-cache-config.xml"
    2013-11-14 15:45:02.286/5.266 Oracle Coherence GE 12.1.3.0.0 <Info> (thread=main, member=4): Created cache factory com.tangosol.net.ExtensibleConfigurableCacheFactory
    2013-11-14 15:45:02.364/5.344 Oracle Coherence GE 12.1.3.0.0 <D5> (thread=DistributedCache, member=4): Service DistributedCache joined the cluster with senior service member 1
    hello
    2013-11-14 15:45:12.397/15.377 Oracle Coherence GE 12.1.3.0.0 <D5> (thread=DistributedCache, member=n/a): Service DistributedCache left the cluster
    2013-11-14 15:45:12.398/15.378 Oracle Coherence GE 12.1.3.0.0 <D5> (thread=Invocation:Management, member=n/a): Service Management left the cluster
    2013-11-14 15:45:12.447/15.427 Oracle Coherence GE 12.1.3.0.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.