Skip Headers
Oracle® Coherence Tutorial for Oracle Coherence
Release 3.7.1

E22622-03
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

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 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

    2011-03-14 17:01:25.443/0.297 Oracle Coherence 3.7.0.0 <Info> (thread=main, member=n/a): Loaded operational configuration from "jar:file:/C:/oracle/product/coherence/lib/coherence.jar!/tangosol-coherence.xml"
    2011-03-14 17:01:25.490/0.344 Oracle Coherence 3.7.0.0 <Info> (thread=main, member=n/a): Loaded operational overrides from "jar:file:/C:/oracle/product/coherence/lib/coherence.jar!/tangosol-coherence-override-dev.xml"
    2011-03-14 17:01:25.537/0.391 Oracle Coherence 3.7.0.0 <Info> (thread=main, member=n/a): Loaded operational overrides from "file:/C:/home/oracle/workspace/InsertValue/build/classes/tangosol-coherence-override.xml"
    2011-03-14 17:01:25.537/0.391 Oracle Coherence 3.7.0.0 <D5> (thread=main, member=n/a): Optional configuration override "/custom-mbeans.xml" is not specified
     
    Oracle Coherence Version 3.7.0.0 Build 22913
     Grid Edition: Development mode
    Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
     
    2011-03-14 17:01:25.771/0.625 Oracle Coherence GE 3.7.0.0 <Info> (thread=main, member=n/a): Loaded cache configuration from "jar:file:/C:/oracle/product/coherence/lib/coherence.jar!/coherence-cache-config.xml"
    2011-03-14 17:01:25.975/0.829 Oracle Coherence GE 3.7.0.0 <D4> (thread=main, member=n/a): TCMP bound to /130.35.99.213:8088 using SystemSocketProvider
    2011-03-14 17:01:29.490/4.344 Oracle Coherence GE 3.7.0.0 <Info> (thread=Cluster, member=n/a): Created a new cluster "cluster:0x96AB" with Member(Id=1, Timestamp=2011-03-14 17:01:25.975, Address=130.35.99.213:8088, MachineId=49877, Location=site:URL, machine_name,process:5000, Role=OracleHandsonMyFirstSample, Edition=Grid Edition, Mode=Development, CpuCount=2, SocketCount=1) UID=0x822363D50000012E552F4A57C2D51F98
    2011-03-14 17:01:29.490/4.344 Oracle Coherence GE 3.7.0.0 <Info> (thread=main, member=n/a): Started cluster Name=cluster:0x96AB
     
    Group{Address=224.3.7.0, Port=3155, TTL=4}
     
    MasterMemberSet
      (
      ThisMember=Member(Id=1, Timestamp=2011-03-14 17:01:25.975, Address=130.35.99.213:8088, MachineId=49877, Location=site:URL, machine_name,process:5000, Role=OracleHandsonMyFirstSample)
      OldestMember=Member(Id=1, Timestamp=2011-03-14 17:01:25.975, Address=130.35.99.213:8088, MachineId=49877, Location=site:URL, machine_name,process:5000, Role=OracleHandsonMyFirstSample)
      ActualMemberSet=MemberSet(Size=1, BitSetCount=2
        Member(Id=1, Timestamp=2011-03-14 17:01:25.975, Address=130.35.99.213:8088, MachineId=49877, Location=site:URL, machine_name,process:5000, Role=OracleHandsonMyFirstSample)
        )
      RecycleMillis=1200000
      RecycleSet=MemberSet(Size=0, BitSetCount=0
        )
      )
     
    TcpRing{Connections=[]}
    IpMonitor{AddressListSize=0}
     
    2011-03-14 17:01:29.521/4.375 Oracle Coherence GE 3.7.0.0 <D5> (thread=Invocation:Management, member=1): Service Management joined the cluster with senior service member 1
    2011-03-14 17:01:29.600/4.454 Oracle Coherence GE 3.7.0.0 <D5> (thread=DistributedCache, member=1): Service DistributedCache joined the cluster with senior service member 1
    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 that local storage is enabled 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

    2011-03-14 17:06:17.115/0.281 Oracle Coherence 3.7.0.0 <Info> (thread=main, member=n/a): Loaded operational configuration from "jar:file:/C:/oracle/product/coherence/lib/coherence.jar!/tangosol-coherence.xml"
    2011-03-14 17:06:17.162/0.328 Oracle Coherence 3.7.0.0 <Info> (thread=main, member=n/a): Loaded operational overrides from "jar:file:/C:/oracle/product/coherence/lib/coherence.jar!/tangosol-coherence-override-dev.xml"
    2011-03-14 17:06:17.193/0.359 Oracle Coherence 3.7.0.0 <Info> (thread=main, member=n/a): Loaded operational overrides from "file:/C:/home/oracle/workspace/InsertValue/build/classes/tangosol-coherence-override.xml"
    2011-03-14 17:06:17.193/0.359 Oracle Coherence 3.7.0.0 <D5> (thread=main, member=n/a): Optional configuration override "/custom-mbeans.xml" is not specified
     
    Oracle Coherence Version 3.7.0.0 Build 22913
     Grid Edition: Development mode
    Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
     
    2011-03-14 17:06:17.568/0.734 Oracle Coherence GE 3.7.0.0 <D4> (thread=main, member=n/a): TCMP bound to /130.35.99.213:8088 using SystemSocketProvider
    2011-03-14 17:06:21.115/4.281 Oracle Coherence GE 3.7.0.0 <Info> (thread=Cluster, member=n/a): Created a new cluster "cluster:0x96AB" with Member(Id=1, Timestamp=2011-03-14 17:06:17.568, Address=130.35.99.213:8088, MachineId=49877, Location=site:URL, machine_name,process:216, Role=OracleHandsonMyFirstSampleReader, Edition=Grid Edition, Mode=Development, CpuCount=2, SocketCount=1) UID=0x822363D50000012E5533BD60C2D51F98
    2011-03-14 17:06:21.115/4.281 Oracle Coherence GE 3.7.0.0 <Info> (thread=main, member=n/a): Started cluster Name=cluster:0x96AB
     
    Group{Address=224.3.7.0, Port=3155, TTL=4}
     
    MasterMemberSet
      (
      ThisMember=Member(Id=1, Timestamp=2011-03-14 17:06:17.568, Address=130.35.99.213:8088, MachineId=49877, Location=site:URL, machine_name,process:216, Role=OracleHandsonMyFirstSampleReader)
      OldestMember=Member(Id=1, Timestamp=2011-03-14 17:06:17.568, Address=130.35.99.213:8088, MachineId=49877, Location=site:URL, machine_name,process:216, Role=OracleHandsonMyFirstSampleReader)
      ActualMemberSet=MemberSet(Size=1, BitSetCount=2
        Member(Id=1, Timestamp=2011-03-14 17:06:17.568, Address=130.35.99.213:8088, MachineId=49877, Location=site:URL, machine_name,process:216, Role=OracleHandsonMyFirstSampleReader)
        )
      RecycleMillis=1200000
      RecycleSet=MemberSet(Size=0, BitSetCount=0
        )
      )
     
    TcpRing{Connections=[]}
    IpMonitor{AddressListSize=0}
     
    2011-03-14 17:06:21.162/4.328 Oracle Coherence GE 3.7.0.0 <D5> (thread=Invocation:Management, member=1): Service Management joined the cluster with senior service member 1
    2011-03-14 17:06:21.350/4.516 Oracle Coherence GE 3.7.0.0 <Info> (thread=main, member=1): Loaded cache configuration from "jar:file:/C:/oracle/product/coherence/lib/coherence.jar!/coherence-cache-config.xml"
    2011-03-14 17:06:21.412/4.578 Oracle Coherence GE 3.7.0.0 <D5> (thread=DistributedCache, member=1): Service DistributedCache joined the cluster with senior service member 1
    Value in cache is null
    2011-03-14 17:06:21.521/4.687 Oracle Coherence GE 3.7.0.0 <D4> (thread=ShutdownHook, member=1): ShutdownHook: stopping cluster node
    2011-03-14 17:06:21.521/4.687 Oracle Coherence GE 3.7.0.0 <D5> (thread=Cluster, member=1): Service Cluster left the cluster
    
  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

    2011-03-14 17:18:01.678/0.282 Oracle Coherence 3.7.0.0 <Info> (thread=main, member=n/a): Loaded operational configuration from "jar:file:/C:/oracle/product/coherence/lib/coherence.jar!/tangosol-coherence.xml"
    2011-03-14 17:18:01.725/0.329 Oracle Coherence 3.7.0.0 <Info> (thread=main, member=n/a): Loaded operational overrides from "jar:file:/C:/oracle/product/coherence/lib/coherence.jar!/tangosol-coherence-override-dev.xml"
    2011-03-14 17:18:01.756/0.360 Oracle Coherence 3.7.0.0 <Info> (thread=main, member=n/a): Loaded operational overrides from "file:/C:/home/oracle/workspace/InsertValue/build/classes/tangosol-coherence-override.xml"
    2011-03-14 17:18:01.771/0.375 Oracle Coherence 3.7.0.0 <D5> (thread=main, member=n/a): Optional configuration override "/custom-mbeans.xml" is not specified
     
    Oracle Coherence Version 3.7.0.0 Build 22913
     Grid Edition: Development mode
    Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
     
    2011-03-14 17:18:02.131/0.735 Oracle Coherence GE 3.7.0.0 <D4> (thread=main, member=n/a): TCMP bound to /130.35.99.213:8090 using SystemSocketProvider
    2011-03-14 17:18:02.631/1.235 Oracle Coherence GE 3.7.0.0 <Info> (thread=Cluster, member=n/a): This Member(Id=3, Timestamp=2011-03-14 17:18:02.459, Address=130.35.99.213:8090, MachineId=49877, Location=site:URL, machine_name,process:424, Role=OracleHandsonMyFirstSampleReader, Edition=Grid Edition, Mode=Development, CpuCount=2, SocketCount=1) joined cluster "cluster:0x96AB" with senior Member(Id=1, Timestamp=2011-03-14 17:16:43.021, Address=130.35.99.213:8088, MachineId=49877, Location=site:URL, machine_name,process:2692, Role=CoherenceServer, Edition=Grid Edition, Mode=Development, CpuCount=2, SocketCount=1)
    2011-03-14 17:18:02.631/1.235 Oracle Coherence GE 3.7.0.0 <D5> (thread=Cluster, member=n/a): Member 1 joined Service Cluster with senior member 1
    2011-03-14 17:18:02.631/1.235 Oracle Coherence GE 3.7.0.0 <D5> (thread=Cluster, member=n/a): Member 1 joined Service Management with senior member 1
    2011-03-14 17:18:02.631/1.235 Oracle Coherence GE 3.7.0.0 <D5> (thread=Cluster, member=n/a): Member 1 joined Service DistributedCache with senior member 1
    2011-03-14 17:18:02.631/1.235 Oracle Coherence GE 3.7.0.0 <D5> (thread=Cluster, member=n/a): Member 1 joined Service ReplicatedCache with senior member 1
    2011-03-14 17:18:02.631/1.235 Oracle Coherence GE 3.7.0.0 <D5> (thread=Cluster, member=n/a): Member 1 joined Service OptimisticCache with senior member 1
    2011-03-14 17:18:02.631/1.235 Oracle Coherence GE 3.7.0.0 <D5> (thread=Cluster, member=n/a): Member 1 joined Service InvocationService with senior member 1
    2011-03-14 17:18:02.631/1.235 Oracle Coherence GE 3.7.0.0 <Info> (thread=main, member=n/a): Started cluster Name=cluster:0x96AB
     
    Group{Address=224.3.7.0, Port=3155, TTL=4}
     
    MasterMemberSet
      (
      ThisMember=Member(Id=3, Timestamp=2011-03-14 17:18:02.459, Address=130.35.99.213:8090, MachineId=49877, Location=site:URL, machine_name,process:424, Role=OracleHandsonMyFirstSampleReader)
      OldestMember=Member(Id=1, Timestamp=2011-03-14 17:16:43.021, Address=130.35.99.213:8088, MachineId=49877, Location=site:URL, machine_name,process:2692, Role=CoherenceServer)
      ActualMemberSet=MemberSet(Size=2, BitSetCount=2
        Member(Id=1, Timestamp=2011-03-14 17:16:43.021, Address=130.35.99.213:8088, MachineId=49877, Location=site:URL, machine_name,process:2692, Role=CoherenceServer)
        Member(Id=3, Timestamp=2011-03-14 17:18:02.459, Address=130.35.99.213:8090, MachineId=49877, Location=site:URL, machine_name,process:424, Role=OracleHandsonMyFirstSampleReader)
        )
      RecycleMillis=1200000
      RecycleSet=MemberSet(Size=0, BitSetCount=0
        )
      )
     
    TcpRing{Connections=[1]}
    IpMonitor{AddressListSize=0}
     
    2011-03-14 17:18:02.662/1.266 Oracle Coherence GE 3.7.0.0 <D5> (thread=Invocation:Management, member=3): Service Management joined the cluster with senior service member 1
    2011-03-14 17:18:02.756/1.360 Oracle Coherence GE 3.7.0.0 <Info> (thread=main, member=3): Loaded cache configuration from "jar:file:/C:/oracle/product/coherence/lib/coherence.jar!/coherence-cache-config.xml"
    2011-03-14 17:18:02.818/1.422 Oracle Coherence GE 3.7.0.0 <D5> (thread=DistributedCache, member=3): Service DistributedCache joined the cluster with senior service member 1
    Value in cache is Gene Smith
    2011-03-14 17:18:02.850/1.454 Oracle Coherence GE 3.7.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

    2011-03-14 17:23:46.896/0.296 Oracle Coherence 3.7.0.0 <Info> (thread=main, member=n/a): Loaded operational configuration from "jar:file:/C:/oracle/product/coherence/lib/coherence.jar!/tangosol-coherence.xml"
    2011-03-14 17:23:46.959/0.359 Oracle Coherence 3.7.0.0 <Info> (thread=main, member=n/a): Loaded operational overrides from "jar:file:/C:/oracle/product/coherence/lib/coherence.jar!/tangosol-coherence-override-dev.xml"
    2011-03-14 17:23:46.990/0.390 Oracle Coherence 3.7.0.0 <Info> (thread=main, member=n/a): Loaded operational overrides from "file:/C:/home/oracle/workspace/InsertValue/build/classes/tangosol-coherence-override.xml"
    2011-03-14 17:23:46.990/0.390 Oracle Coherence 3.7.0.0 <D5> (thread=main, member=n/a): Optional configuration override "/custom-mbeans.xml" is not specified
     
    Oracle Coherence Version 3.7.0.0 Build 22913
     Grid Edition: Development mode
    Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
     
    2011-03-14 17:23:47.256/0.656 Oracle Coherence GE 3.7.0.0 <Info> (thread=main, member=n/a): Loaded cache configuration from "jar:file:/C:/oracle/product/coherence/lib/coherence.jar!/coherence-cache-config.xml"
    2011-03-14 17:23:47.443/0.843 Oracle Coherence GE 3.7.0.0 <D4> (thread=main, member=n/a): TCMP bound to /130.35.99.213:8088 using SystemSocketProvider
    2011-03-14 17:23:50.975/4.375 Oracle Coherence GE 3.7.0.0 <Info> (thread=Cluster, member=n/a): Created a new cluster "cluster:0x96AB" with Member(Id=1, Timestamp=2011-03-14 17:23:47.459, Address=130.35.99.213:8088, MachineId=49877, Location=site:URL, machine_name,process:2736, Role=OracleHandsonMyFirstSample, Edition=Grid Edition, Mode=Development, CpuCount=2, SocketCount=1) UID=0x822363D50000012E5543C283C2D51F98
    2011-03-14 17:23:50.990/4.390 Oracle Coherence GE 3.7.0.0 <Info> (thread=main, member=n/a): Started cluster Name=cluster:0x96AB
     
    Group{Address=224.3.7.0, Port=3155, TTL=4}
     
    MasterMemberSet
      (
      ThisMember=Member(Id=1, Timestamp=2011-03-14 17:23:47.459, Address=130.35.99.213:8088, MachineId=49877, Location=site:URL, machine_name,process:2736, Role=OracleHandsonMyFirstSample)
      OldestMember=Member(Id=1, Timestamp=2011-03-14 17:23:47.459, Address=130.35.99.213:8088, MachineId=49877, Location=site:URL, machine_name,process:2736, Role=OracleHandsonMyFirstSample)
      ActualMemberSet=MemberSet(Size=1, BitSetCount=2
        Member(Id=1, Timestamp=2011-03-14 17:23:47.459, Address=130.35.99.213:8088, MachineId=49877, Location=site:URL, machine_name,process:2736, Role=OracleHandsonMyFirstSample)
        )
      RecycleMillis=1200000
      RecycleSet=MemberSet(Size=0, BitSetCount=0
        )
      )
     
    TcpRing{Connections=[]}
    IpMonitor{AddressListSize=0}
     
    2011-03-14 17:23:51.037/4.437 Oracle Coherence GE 3.7.0.0 <D5> (thread=Invocation:Management, member=1): Service Management joined the cluster with senior service member 1
    2011-03-14 17:23:51.178/4.578 Oracle Coherence GE 3.7.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:48)
       at com.tangosol.coherence.component.util.daemon.queueProcessor.service.grid.partitionedService.PartitionedCache$BinaryMap.put(PartitionedCache.CDB:24)
       at com.tangosol.coherence.component.util.daemon.queueProcessor.service.grid.partitionedService.PartitionedCache$BinaryMap.put(PartitionedCache.CDB:1)
       at com.tangosol.util.ConverterCollections$ConverterMap.put(ConverterCollections.java:1673)
       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)
    2011-03-14 17:23:51.193/4.593 Oracle Coherence GE 3.7.0.0 <D4> (thread=ShutdownHook, member=1): ShutdownHook: stopping cluster node
    
  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 C:\oracle\product\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 Developer's Guide for 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.6.0_14"Java(TM) SE Runtime Environment (build 1.6.0_14-b08)Java HotSpot(TM) Client VM (build 14.0-b16, mixed mode)Coherence Command Line ToolCohQL>  
    
  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 Disabled (cache client) 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}
     
    2011-03-14 18:01:15.271/1.312 Oracle Coherence GE 3.7.0.0 <D5> (thread=Invocation:Management, member=4): Service Management joined the cluster with senior service member 1
    2011-03-14 18:01:15.381/1.422 Oracle Coherence GE 3.7.0.0 <Info> (thread=main, member=4): Loaded cache configuration from "jar:file:/C:/oracle/product/coherence/lib/coherence.jar!/coherence-cache-config.xml"
    2011-03-14 18:01:15.443/1.484 Oracle Coherence GE 3.7.0.0 <D5> (thread=DistributedCache, member=4): Service DistributedCache joined the cluster with senior service member 1
    null
    2011-03-14 18:01:15.475/1.516 Oracle Coherence GE 3.7.0.0 <D5> (thread=Invocation:Management, member=4): Service Management left the cluster
    2011-03-14 18:01:15.475/1.516 Oracle Coherence GE 3.7.0.0 <D5> (thread=DistributedCache, member=4): Service DistributedCache left the cluster
    2011-03-14 18:01:15.506/1.547 Oracle Coherence GE 3.7.0.0 <D5> (thread=Cluster, member=4): 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{AddressListSize=0}
     
    2011-03-14 18:07:21.537/1.344 Oracle Coherence GE 3.7.0.0 <D5> (thread=Invocation:Management, member=5): Service Management joined the cluster with senior service member 1
    2011-03-14 18:07:21.631/1.438 Oracle Coherence GE 3.7.0.0 <Info> (thread=main, member=5): Loaded cache configuration from "jar:file:/C:/oracle/product/coherence/lib/coherence.jar!/coherence-cache-config.xml"
    2011-03-14 18:07:21.693/1.500 Oracle Coherence GE 3.7.0.0 <D5> (thread=DistributedCache, member=5): Service DistributedCache joined the cluster with senior service member 1
    hello
    2011-03-14 18:07:21.725/1.532 Oracle Coherence GE 3.7.0.0 <D5> (thread=Invocation:Management, member=5): Service Management left the cluster
    2011-03-14 18:07:21.725/1.532 Oracle Coherence GE 3.7.0.0 <D5> (thread=DistributedCache, member=5): Service DistributedCache left the cluster
    2011-03-14 18:07:21.756/1.563 Oracle Coherence GE 3.7.0.0 <D5> (thread=Cluster, member=5): 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.