この演習では、Coherenceクラスタ・キャッシュの単純なタイプの情報にアクセスし、それらの情報を更新および削除するJavaコンソール・ベースの単純なアプリケーションを開発します。また、Coherence Java APIについても学習します。Eclipse IDEを使用して、次の作業を行います。
新規プロジェクトの作成
新しい名前付きキャッシュ(NamedCache
オブジェクト)の作成
キャッシュへの情報の挿入と取得
キャッシュに関する情報の取得
この章では次の項について説明します。
Coherenceのキャッシュはすべて名前が付いており、それらのキャッシュが存在するクラスタ・インスタンスによって存続期間が調べられ、com.tangosol.net.NamedCache
インタフェースを実装します。NamedCache
インタフェースはjava.util.Map
インタフェースを拡張したもので、クラスタ・メンバー間で共有しているデータおよびリソースを保持します。各NamedCache
オブジェクトは、キーと値のペアとしてデータを保持します。キーと値のオブジェクト・タイプは単純でも複合でもかまいません。NamedCache
インタフェースは、ロックおよび同期、記憶域統合、問合せ、イベント集計、トランザクションなどの拡張機能をMap
インタフェースに提供します。表3-1に、NamedCache
インタフェースでよく使用されるメソッドをいくつか示します。
表3-1 NamedCacheインタフェースのメソッド
メソッド名 | 説明 |
---|---|
|
|
|
|
|
この値を含むエントリが |
|
|
|
オブジェクトをキャッシュに挿入し、前の値を返します(ある場合)。 |
|
このマップからこの |
|
一連のキーと値のペアを返します。 |
|
すべての値をコレクションとして取得しなおします。 |
|
この |
com.tangosol.net.CacheFactory
クラスは通常、NamedCache
オブジェクトのインスタンスを取得するために使用します。表3-2に、CacheFactory
クラスでよく使用されるメソッドをいくつか示します。
表3-2 CacheFactoryクラスのメソッド
メソッド名 | 説明 |
---|---|
|
Coherenceのサービスを実行しているクラスタ・オブジェクトを取得します。 |
|
すべてのクラスタ・サービスをシャットダウンします。 |
|
キャッシュのインスタンスを返します。クラスタ内の既存のキャッシュに参加するか、これが最初のメンバーである場合はキャッシュを作成します。 |
NamedCache
インタフェースおよびCacheFactory
クラスのメソッドの詳細な一覧は、C:\oracle\product\coherence\doc
にあるJavadocを参照してください。
ここでは、Coherenceクラスタ・キャッシュの単純なタイプの情報にアクセスし、それらの情報を更新および削除するJavaプログラムの作成方法について説明します。
CoherenceのJavaベース・アプリケーションを作成するには:
Eclipseで新しいアプリケーション・クライアント・プロジェクトを作成します。プロジェクトにInsertValue
という名前を付けます。フォルダがC:\home\oracle\workspace\InsertValue
であることを確認してください。
「New Application Client Project」ダイアログ・ボックスの「Configuration」セクションで、「Modify」をクリックします。「Project Facets」ダイアログ・ボックスで、「Configuration」ドロップダウン・リストから「CoherenceConfig」を選択します。
詳細な手順は、「Eclipse IDEでの新規プロジェクトの作成」を参照してください。
最初のCoherence Javaプログラムを作成します。「New Java Class」ダイアログ・ボックスで、クラスにMyFirstSample
という名前を付け、「public static void main(String[] args)」チェック・ボックスを選択します。
詳細は、「Javaクラスの作成」を参照してください。
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")); } }
実行中のキャッシュ・サーバーがあれば停止します。詳細は、「キャッシュ・サーバーの停止」を参照してください。
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
put
メソッドやget
メソッドを使用せずに、キャッシュから値を取得するJavaクラスを作成するには:
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")); } }
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
「Eclipse IDEでのキャッシュ・サーバーの起動」で作成したDefaultCacheServer
を起動します。「Main」タブの「Project」フィールドにInsertValue
が表示されていることと、「Coherence」タブでローカル記憶域が有効になっていることを確認します。「Apply」、「Run」の順にクリックします。
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つの操作(値を入力して終了するなど)を実行するためにのみクラスタに参加するプロセスには該当しません。デフォルトでは、すべてのプロセスが、記憶域が有効な状態で起動します。このようなプロセスではクラスタの一部としてデータを格納できます。記憶域が無効になるようにプロセスを変更してください。
MyFirstSample
クラスを右クリックし、「Run As」→「Run Configurations」を選択します。詳細は、「ランタイム構成の作成」を参照してください。
「Coherence」タブの「Local storage」で、「Disabled (cache client)」を選択します。
これは、Javaパラメータ-Dtangosol.coherence.distributed.localstorage=false
を設定するのと同様です。
実行中のキャッシュ・サーバーをシャットダウンし、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
DefaultCacheServer
キャッシュ・サーバーを再起動し、MyFirstSample
およびMyFirstSampleReader
を再度実行します。2つのJavaサンプルを実行する間、データが保持されていることがわかります。
この演習では、Coherenceクラスタ・キャッシュの単純なタイプの情報にアクセスし、更新および削除を行うJavaコンソール・ベースの単純なアプリケーションを開発します。
この演習を実行するには、「Coherenceインストールのテスト」を完了している必要があります。
一般にクライアント・アプリケーションとサーバー・アプリケーションとの間で接続および切断が行われるクライアント/サーバー・アプリケーションとは異なり、Coherenceベースのクラスタ・アプリケーションはクラスタ内にあることを確認するのみで、その後もクラスタのサービスを使用できます。Coherenceベースのアプリケーションは通常、アプリケーションのクラスタに接続するのではなく、クラスタの一部になります。
Coherenceクラスタ・キャッシュの単純なタイプの情報にアクセスし、それらの情報を更新および削除するJavaコンソール・ベースのアプリケーションを作成するには:
C:\oracle\product\coherence\doc
フォルダにあるCoherence Javaドキュメント(Javadoc)を使用して、CacheFactory
クラスのメソッドを調べます。
CacheFactory
クラスを使用してクラスタに参加し(ensureCluster
メソッドを使用)、その後、クラスタから離脱する(shutdown
メソッドを使用)、YourFirstCoherenceApplication
という単純なJavaコンソール・アプリケーション(Javaクラス)を記述します。Javaクラスの作成の詳細は、「Javaクラスの作成」を参照してください。
Javadocを使用して、NamedCache
インタフェースで使用可能なメソッドを調べます。
CacheFactory
メソッドgetCache
を使用して、mycache
というキャッシュ(「Coherenceインストールのテスト」の演習で使用したものと同じキャッシュ名)のNamedCache
を取得するようにアプリケーションを拡張します。
NamedCache
インスタンスで、get
メソッドを使用してmessage
キー(「Coherenceインストールのテスト」の演習で使用したものと同じキー)の値を取得します。
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(); } }
Coherenceアプリケーションを実行するには:
「Eclipse IDEでのキャッシュ・サーバーの起動」で作成したDefaultCacheServer
キャッシュ・サーバーを起動します。
Coherence問合せ言語およびQueryHelper
APIを使用するキャッシュ・クライアントの実行構成を作成します。プロジェクトを右クリックし、「Run As」→「Run Configurations」を選択します。
注意: Coherence問合せ言語の詳細は、『Oracle Coherence開発者ガイド』のCoherence問合せ言語の使用に関する項を参照してください。 |
「Run Configurations」ダイアログ・ボックスで、「Oracle Coherence」→「New launch configuration」アイコンをクリックします。構成の名前としてQueryPlus
を入力します。
「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のようになります。
「Coherence」タブの「Local storage」で、「Disabled (cache client)」を選択します。「Cluster port」に一意の値を入力します(この値は、前の項で定義したキャッシュ・サーバーに定義された値と同じである必要があります)。
「Arguments」タブで、「VM arguments」フィールドに-showversion
と入力します。
「Common」タブで、「Shared file」を選択し、「Browse」ボタンをクリックして\InsertValueプロジェクト名に移動します。「Allocate console」チェック・ボックスが選択されていることを確認します。「Apply」をクリックします。
「Run」をクリックして、QueryPlus
クライアントを起動します。例3-8のような出力がEclipseコンソールに表示されます。
CohQL>
プロンプトで、次のコマンドを入力して、mycache
という名前のキャッシュを作成し、そのキャッシュに接続します。
CohQL> create cache "mycache"
YourFirstCoherenceApplication
の実行構成を作成します。「Run Configurations」ダイアログ・ボックスで、「Name」フィールドにYourFirstCoherenceApplication
と入力します。「Main」タブで、「Project」フィールドにInsertValue
と入力し、「メイン・クラス」にcom.oracle.handson.YourFirstCoherenceApplication
と入力します。
「Coherence」タブの「Local storage」で「Disabled (cache client)」を選択し、「Cluster port」に一意の値を入力します(これは、キャッシュ・サーバーおよびキャッシュ・クライアントに使用したものと同じ値である必要があります)。
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
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
YourFirstCoherenceApplication
の実行構成で、「Local storage」の値を「Enabled」から「Disabled」に変更します。出力は前回の実行と同じです。
キャッシュ・サーバーおよびキャッシュ・クライアントのインスタンスをシャットダウンします。キャッシュ・サーバーを再起動し、(「Local storage」に新しい値を指定して)YourFirstCoherenceApplication
を再実行します。今回は出力がNULL
になります。
アプリケーションで(put
メソッドを使用して)message
キーの値を変更した場合、新しい値がキャッシュ・クライアントで使用可能であることを確認します。
たとえば、get
メソッドをコメント・アウトし、put
メソッドを追加します。
//String message = (String)myCache.get("message"); String message = (String)myCache.put("message", "bye");
YourFirstCoherenceApplication
を実行します。
QueryPlus
キャッシュ・クライアントでget
コマンドを実行します。
select key(), value() from "mycache" where key() is "message"
出力としてbye
が表示されます。