ヘッダーをスキップ
Oracle® Coherenceチュートリアル
リリース3.7.1
B65030-01
  ドキュメント・ライブラリへ移動
ライブラリ
製品リストへ移動
製品
目次へ移動
目次
索引へ移動
索引

前
 
次
 

3 Javaからデータ・グリッドへのアクセス

この演習では、Coherenceクラスタ・キャッシュの単純なタイプの情報にアクセスし、それらの情報を更新および削除するJavaコンソール・ベースの単純なアプリケーションを開発します。また、Coherence Java APIについても学習します。Eclipse IDEを使用して、次の作業を行います。

この章では次の項について説明します。

3.1 概要

Coherenceのキャッシュはすべて名前が付いており、それらのキャッシュが存在するクラスタ・インスタンスによって存続期間が調べられ、com.tangosol.net.NamedCacheインタフェースを実装します。NamedCacheインタフェースはjava.util.Mapインタフェースを拡張したもので、クラスタ・メンバー間で共有しているデータおよびリソースを保持します。各NamedCacheオブジェクトは、キーと値のペアとしてデータを保持します。キーと値のオブジェクト・タイプは単純でも複合でもかまいません。NamedCacheインタフェースは、ロックおよび同期、記憶域統合、問合せ、イベント集計、トランザクションなどの拡張機能をMapインタフェースに提供します。表3-1に、NamedCacheインタフェースでよく使用されるメソッドをいくつか示します。

表3-1 NamedCacheインタフェースのメソッド

メソッド名 説明

void clear()

NamedCacheオブジェクトからすべてのエントリを削除します。

boolean containsKey(Object key)

NamedCacheオブジェクトにキーのエントリが格納されている場合、trueを返します。

boolean containsValue(Objectvalue)

この値を含むエントリがNamedCacheオブジェクトに1つ以上ある場合、trueを返します。

Object get(Object key)

NamedCacheオブジェクトからそのキーのエントリを取得します。

Object put(Object key,Objectvalue)

オブジェクトをキャッシュに挿入し、前の値を返します(ある場合)。

Object remove(Object key)

このマップからこのkeyのマッピングを削除します(存在する場合)。ConcurrentMapインタフェースからの継承です。

Set entrySet()

一連のキーと値のペアを返します。

Collection values()

すべての値をコレクションとして取得しなおします。

CacheService getCacheService()

このNamedCacheが属するCacheServiceを返します。


com.tangosol.net.CacheFactoryクラスは通常、NamedCacheオブジェクトのインスタンスを取得するために使用します。表3-2に、CacheFactoryクラスでよく使用されるメソッドをいくつか示します。

表3-2 CacheFactoryクラスのメソッド

メソッド名 説明

static Cluster ensureCluster()

Coherenceのサービスを実行しているクラスタ・オブジェクトを取得します。

static void shutdown()

すべてのクラスタ・サービスをシャットダウンします。

static NamedCache getCache(String cache)

キャッシュのインスタンスを返します。クラスタ内の既存のキャッシュに参加するか、これが最初のメンバーである場合はキャッシュを作成します。


NamedCacheインタフェースおよびCacheFactoryクラスのメソッドの詳細な一覧は、C:\oracle\product\coherence\docにあるJavadocを参照してください。

3.2 最初のCoherenceベースのJavaプログラムの作成

ここでは、Coherenceクラスタ・キャッシュの単純なタイプの情報にアクセスし、それらの情報を更新および削除するJavaプログラムの作成方法について説明します。

  1. キャッシュに値を挿入するプログラムの作成

  2. キャッシュから値を取得するプログラムの作成

3.2.1 キャッシュに値を挿入するプログラムの作成

CoherenceのJavaベース・アプリケーションを作成するには:

  1. Eclipseで新しいアプリケーション・クライアント・プロジェクトを作成します。プロジェクトにInsertValueという名前を付けます。フォルダがC:\home\oracle\workspace\InsertValueであることを確認してください。

    New Application Client Project」ダイアログ・ボックスの「Configuration」セクションで、「Modify」をクリックします。「Project Facets」ダイアログ・ボックスで、「Configuration」ドロップダウン・リストから「CoherenceConfig」を選択します。

    詳細な手順は、「Eclipse IDEでの新規プロジェクトの作成」を参照してください。

  2. 最初のCoherence Javaプログラムを作成します。「New Java Classダイアログボックスで、クラスにMyFirstSampleという名前を付け、「public static void main(String[] args)」チェック・ボックスを選択します。

    詳細は、「Javaクラスの作成」を参照してください。

  3. Eclipseエディタで、NamedCacheオブジェクトの作成、キャッシュの値の入力および入力した値の検証を実行するコードを記述します。例3-1にサンプル・プログラムを示します。

    例3-1 NamedCacheオブジェクトの作成: 値の挿入と検証

    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. 実行中のキャッシュ・サーバーがあれば停止します。詳細は、「キャッシュ・サーバーの停止」を参照してください。

  5. Eclipse IDEでプログラムを実行します。エディタでMyFirstSample.javaクラスを右クリックし、「Run As」→「Run Configuration」を選択します。「Oracle Coherence」をダブルクリックして、Coherenceの構成を作成します。「Run Configuration」ダイアログ・ボックスの「Name」フィールドにMyFirstSampleと入力します。

    Main」タブで、「Project」フィールドにInsertValueと入力し、「Main class」フィールドにcom.oracle.handson.MyFirstSampleと入力します。

    Coherence」タブで、「Cluster port」フィールドに一意の値(3155など)を入力して、Coherenceが自分のホストに制限されるようにします。「Local storage」で「Enabled (cache server)」を選択します。

    Classpath」タブでは、「Bootstrap Entries」リストにcoherence.jarファイルが表示されているはずです。「Apply」→「Run」をクリックします。

    例3-2のようなメッセージが表示されます。

    例3-2 MyFirstSampleプログラムの出力

    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:us.oracle.com,machine:tpfaeffl-lap7,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:us.oracle.com,machine:tpfaeffl-lap7,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:us.oracle.com,machine:tpfaeffl-lap7,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:us.oracle.com,machine:tpfaeffl-lap7,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 キャッシュから値を取得するプログラムの作成

putメソッドやgetメソッドを使用せずに、キャッシュから値を取得するJavaクラスを作成するには:

  1. mainメソッドを持つ、MyFirstSampleReaderという名前の別のJavaクラスを作成します。詳細は、「Javaクラスの作成」を参照してください。例3-3にサンプル・プログラムを示します。

    例3-3 キャッシュからの値の取得

    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. Eclipse IDEでMyFirstSampleReaderクラスを実行します。エディタでMyFirstSampleReader.javaクラスを右クリックし、「Run As」→「Run Configuration」を選択します。

    Run Configuration」ダイアログ・ボックスの「Name」フィールドにMyFirstSampleReaderと入力します。「Main」タブの「Project」フィールドにInsertValueが表示されていることと、「Coherence」タブでローカル記憶域が有効になっていることを確認します。「Apply」→「Run」をクリックします。

    例3-4にプログラムの出力を示します。NULL値が返されています。MyFirstSampleプログラムによってNamedCacheキャッシュが正常に作成され、値が移入されますが、このキャッシュはMyFirstSampleプロセス・メモリー内にのみ存在します。MyFirstSampleプログラムが終了すると、キャッシュも終了します。

    例3-4 MyFirstSampleReaderプログラムの出力

    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:us.oracle.com,machine:tpfaeffl-lap7,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:us.oracle.com,machine:tpfaeffl-lap7,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:us.oracle.com,machine:tpfaeffl-lap7,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:us.oracle.com,machine:tpfaeffl-lap7,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. Eclipse IDEでのキャッシュ・サーバーの起動」で作成したDefaultCacheServerを起動します。「Main」タブの「Project」フィールドにInsertValueが表示されていることと、「Coherence」タブでローカル記憶域が有効になっていることを確認します。「Apply」、「Run」の順にクリックします。

  4. MyFirstSampleを実行してNamedCacheに値を挿入し、MyFirstSampleReaderを実行してキャッシュから値を読み取ります。例3-5に示す出力を参照してください。MyFirstSampleによって格納されたGene Smithという値がMyFirstSampleReaderによって返されています。

    例3-5 キャッシュ・サーバーを実行している状態でのMyFirstSampleReaderプログラムの出力

    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:us.oracle.com,machine:tpfaeffl-lap7,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:us.oracle.com,machine:tpfaeffl-lap7,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:us.oracle.com,machine:tpfaeffl-lap7,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:us.oracle.com,machine:tpfaeffl-lap7,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:us.oracle.com,machine:tpfaeffl-lap7,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:us.oracle.com,machine:tpfaeffl-lap7,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
    

    これは、MyFirstSampleのように、1つの操作(値を入力して終了するなど)を実行するためにのみクラスタに参加するプロセスには該当しません。デフォルトでは、すべてのプロセスが、記憶域が有効な状態で起動します。このようなプロセスではクラスタの一部としてデータを格納できます。記憶域が無効になるようにプロセスを変更してください。

    1. MyFirstSampleクラスを右クリックし、「Run As」→「Run Configurations」を選択します。詳細は、「ランタイム構成の作成」を参照してください。

    2. 「Coherence」タブの「Local storage」で、「Disabled (cache client)」を選択します。

      これは、Javaパラメータ-Dtangosol.coherence.distributed.localstorage=falseを設定するのと同様です。

  5. 実行中のキャッシュ・サーバーをシャットダウンし、MyFirstSampleクラスを再実行します。

    例3-6のように、クラスタで記憶域が有効でないことを示すメッセージが表示されます。これは、このメンバーを記憶域が無効になるように設定したためです。

    例3-6 キャッシュ記憶域を無効にした場合のMyFirstSampleクラスの出力

    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:us.oracle.com,machine:tpfaeffl-lap7,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:us.oracle.com,machine:tpfaeffl-lap7,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:us.oracle.com,machine:tpfaeffl-lap7,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:us.oracle.com,machine:tpfaeffl-lap7,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. DefaultCacheServerキャッシュ・サーバーを再起動し、MyFirstSampleおよびMyFirstSampleReaderを再度実行します。2つのJavaサンプルを実行する間、データが保持されていることがわかります。

3.3 最初のCoherenceベースのJavaアプリケーションの作成

この演習では、Coherenceクラスタ・キャッシュの単純なタイプの情報にアクセスし、更新および削除を行うJavaコンソール・ベースの単純なアプリケーションを開発します。

この演習を実行するには、「Coherenceインストールのテスト」を完了している必要があります。

一般にクライアント・アプリケーションとサーバー・アプリケーションとの間で接続および切断が行われるクライアント/サーバー・アプリケーションとは異なり、Coherenceベースのクラスタ・アプリケーションはクラスタ内にあることを確認するのみで、その後もクラスタのサービスを使用できます。Coherenceベースのアプリケーションは通常、アプリケーションのクラスタに接続するのではなく、クラスタの一部になります。

  1. コンソール・アプリケーションの作成

  2. コンソール・アプリケーションの実行

3.3.1 コンソール・アプリケーションの作成

Coherenceクラスタ・キャッシュの単純なタイプの情報にアクセスし、それらの情報を更新および削除するJavaコンソール・ベースのアプリケーションを作成するには:

  1. C:\oracle\product\coherence\docフォルダにあるCoherence Javaドキュメント(Javadoc)を使用して、CacheFactoryクラスのメソッドを調べます。

  2. CacheFactoryクラスを使用してクラスタに参加し(ensureClusterメソッドを使用)、その後、クラスタから離脱する(shutdownメソッドを使用)、YourFirstCoherenceApplicationという単純なJavaコンソール・アプリケーション(Javaクラス)を記述します。Javaクラスの作成の詳細は、「Javaクラスの作成」を参照してください。

    1. Javadocを使用して、NamedCacheインタフェースで使用可能なメソッドを調べます。

    2. CacheFactoryメソッドgetCacheを使用して、mycacheというキャッシュ(「Coherenceインストールのテスト」の演習で使用したものと同じキャッシュ名)のNamedCacheを取得するようにアプリケーションを拡張します。

    3. NamedCacheインスタンスで、getメソッドを使用してmessageキー(「Coherenceインストールのテスト」の演習で使用したものと同じキー)の値を取得します。

    4. System.out.println(….)メソッドを使用して、値を標準出力に書き込みます。

      例3-7に、CoherenceベースのサンプルJavaアプリケーションを示します。

      例3-7 CoherenceベースのJavaアプリケーション

      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 コンソール・アプリケーションの実行

Coherenceアプリケーションを実行するには:

  1. Eclipse IDEでのキャッシュ・サーバーの起動」で作成したDefaultCacheServerキャッシュ・サーバーを起動します。

  2. Coherence問合せ言語およびQueryHelper APIを使用するキャッシュ・クライアントの実行構成を作成します。プロジェクトを右クリックし、「Run As」→「Run Configurations」を選択します。


    注意:

    Coherence問合せ言語の詳細は、『Oracle Coherence開発者ガイド』のCoherence問合せ言語の使用に関する項を参照してください。

    1. Run Configurations」ダイアログ・ボックスで、「Oracle Coherence」→「New launch configuration」アイコンをクリックします。構成の名前としてQueryPlusを入力します。

    2. Main」タブの「Project」で、「Browse」ボタンをクリックし、「InsertValue」プロジェクトを選択します。「Main class」で、「Include system libraries when searching for a main class」を選択し、「Search」ボタンをクリックします。「Select Main Type」ダイアログ・ボックスで、QueryPlusと入力し、「QueryPlus - com.tangosol.coherence.dslquery」を選択します。「OK」をクリックします。「Main」タブは図3-1のようになります。

      図3-1 問合せクライアント構成の「Main」タブ

      問合せクライアント構成の「Main」タブ
      「図3-1 問合せクライアント構成の「Main」タブ」の説明

    3. Coherence」タブの「Local storage」で、「Disabled (cache client)」を選択します。「Cluster port」に一意の値を入力します(この値は、前の項で定義したキャッシュ・サーバーに定義された値と同じである必要があります)。

    4. Arguments」タブで、「VM arguments」フィールドに-showversionと入力します。

    5. Common」タブで、「Shared file」を選択し、「Browse」ボタンをクリックして\InsertValueプロジェクト名に移動します。「Allocate console」チェック・ボックスが選択されていることを確認します。「Apply」をクリックします。

  3. Run」をクリックして、QueryPlusクライアントを起動します。例3-8のような出力がEclipseコンソールに表示されます。

    例3-8 QueryPlusキャッシュ・クライアントの出力

    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. CohQL>プロンプトで、次のコマンドを入力して、mycacheという名前のキャッシュを作成し、そのキャッシュに接続します。

    CohQL> create cache "mycache"
    
  5. YourFirstCoherenceApplicationの実行構成を作成します。「Run Configurations」ダイアログ・ボックスで、「Name」フィールドにYourFirstCoherenceApplicationと入力します。「Main」タブで、「Project」フィールドにInsertValueと入力し、「メイン・クラス」にcom.oracle.handson.YourFirstCoherenceApplicationと入力します。

    「Coherence」タブの「Local storage」で「Disabled (cache client)」を選択し、「Cluster port」に一意の値を入力します(これは、キャッシュ・サーバーおよびキャッシュ・クライアントに使用したものと同じ値である必要があります)。

  6. Eclipse IDEからYourFirstCoherenceApplicationを実行し、結果を確認します。

    例3-9に、YourFirstCoherenceApplicationの出力を示します。この出力は、messageキーのデータがキャッシュにないことを示しています。

    例3-9 YourFirstCoherenceApplicationクラスの出力

    ...
    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. Eclipseコンソールで実行中のQueryPlusキャッシュ・クライアントを使用して、messageキーを変更します。たとえば、CohQL>プロンプトで次のコマンドを入力します。

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

    Eclipse IDEからYourFirstCoherenceApplicationを再実行して、変更された値を確認します。例3-10は、messageキーの値helloがキャッシュに保持されていることを示しています。

    例3-10 新しいキー値を含むYourFirstCoherenceApplicationクラスの出力

    ...
    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. YourFirstCoherenceApplicationの実行構成で、「Local storage」の値を「Enabled」から「Disabled」に変更します。出力は前回の実行と同じです。

  9. キャッシュ・サーバーおよびキャッシュ・クライアントのインスタンスをシャットダウンします。キャッシュ・サーバーを再起動し、(「Local storage」に新しい値を指定して)YourFirstCoherenceApplicationを再実行します。今回は出力がNULLになります。

  10. アプリケーションで(putメソッドを使用して)messageキーの値を変更した場合、新しい値がキャッシュ・クライアントで使用可能であることを確認します。

    1. たとえば、getメソッドをコメント・アウトし、putメソッドを追加します。

      //String message = (String)myCache.get("message");
        String message = (String)myCache.put("message", "bye");
      
    2. YourFirstCoherenceApplicationを実行します。

    3. QueryPlusキャッシュ・クライアントでgetコマンドを実行します。

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

      出力としてbyeが表示されます。