ヘッダーをスキップ
Oracle® Fusion Middleware Oracle Coherenceチュートリアル
12c (12.1.3)
E56204-01
  ドキュメント・ライブラリへ移動
ライブラリ
製品リストへ移動
製品
目次へ移動
目次

前
 
次
 

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

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

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プログラムの出力

    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 キャッシュから値を取得するプログラムの作成

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が、「Main class」フィールドにcom.oracle.handson.MyFirstSampleReaderが表示されていることを確認します。Coherenceを自分のホストに制限するために、「Coherence」タブで、ローカル記憶域が有効で「Cluster port」が3155に設定されていることを確認します。「Apply」「Run」の順にクリックします。

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

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

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

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

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

    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
    

    これは、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クラスの出力

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    例3-9 YourFirstCoherenceApplicationクラスの出力

    ...
    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. 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{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. 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が表示されます。