nimbus (1.2.4) | 2018-01-25 20:02 |
nimbus-sample (1.2.4) | 2018-01-26 17:06 |
アプリケーション開発において、処理中のデータを格納し、必要な時に照会するデータの格納場所が必要とされる場合があります。
データを格納及び照会する機能を抽象化したのが、Contextです。
Contextに格納したデータを永続化する機能を抽象化したのがContextStoreです。
データを複数のサーバを跨いで共有する機能を抽象化したのが、SharedContextです。
単一のサーバでは保持できない大量のデータを複数のサーバを跨いで分散共有する機能を抽象化したのが、DistributedSharedContextです。
関連するパッケージは、以下です。
アプリケーション向けインタフェースContextを使った簡単なアプリケーションのサンプルを示します。
- import jp.ossc.nimbus.core.ServiceManagerFactory;
- import jp.ossc.nimbus.service.context.Context;
- // Contextを取得
- Context context = (Context)ServiceManagerFactory.getServiceObject("Context");
- // データを格納する
- context.put("hoge", new Integer(100));
- // データを取得する
- System.out.println(context.get("hoge"));
- // データを削除する
- context.remove("hoge");
実装サービスの一覧は以下のとおりです。
実装サービス | 実装概要 |
jp.ossc.nimbus.service.context.DefaultContextService | デフォルト実装。静的にデータを格納する。 |
jp.ossc.nimbus.service.context.ThreadContextService | 処理スレッド毎にデータを格納する。 |
jp.ossc.nimbus.service.context.ServerInfoService | サーバの静的な情報を照会する。 |
jp.ossc.nimbus.service.context.GroupContextService | 複数のコンテキストをグル-ピングして照会する。 |
Contextインタフェース向けインタフェースContextStoreは、Contextインタフェース実装サービスに設定する事で、格納したデータを永続化したり、永続化されたデータをコンテキスト上に読み込ませたりする事ができます。
このインタフェースの実装サービスは、下位サービスで、以下の上位サービスから使用します。
上位サービスインタフェース | 用途 |
jp.ossc.nimbus.service.context.Context | コンテキスト情報を永続化するために使用する |
実装サービスの一覧は以下のとおりです。
実装サービス | 実装概要 |
jp.ossc.nimbus.service.context.DatabaseContextStoreService | データベースに永続化する。 |
Contextインタフェースを継承しているため、サーバを跨って共有されている事を意識せずにデータの格納及び参照を行う事ができます。
拡張されている機能は、以下です。
アプリケーション向けインタフェースSharedContextを使った簡単なアプリケーションのサンプルを示します。
- import java.util.*;
- import jp.ossc.nimbus.core.ServiceManagerFactory;
- import jp.ossc.nimbus.service.context.SharedContext;
- import jp.ossc.nimbus.service.context.SharedContextRecord;
- // SharedContextを取得
- SharedContext context = (SharedContext)ServiceManagerFactory.getServiceObject("Context");
- // データを格納する
- context.put("hoge", new Integer(100));
- // データを取得する
- System.out.println(context.get("hoge"));
- // データを削除する
- context.remove("hoge");
- // 構造化データを格納する
- SharedContextRecord record = new SharedContextRecord(
- ":name,java.lang.String\n"
- ":age,int"
- );
- record.setProperty("name", "Yamada hanako");
- record.setProperty("age", 10);
- context.put("001", record);
- try{
- // キー指定で更新をロックする
- context.lock("001");
- // データを取得する
- record = (SharedContextRecord)context.get("001");
- // 更新差分を取得する
- SharedContextValueDifference diff = record.updateProperty("name", "Tanaka hanako", null);
- diff = record.updateProperty("age", 25, diff);
- // 差分更新する
- context.update("001", diff);
- }finally{
- // 更新ロックを解除する
- context.unlock("001");
- }
- // インデックスを設定する
- // 基本的には、サービス定義で予め設定しておく
- context.setIndex("AGE_INDEX", new String[]{"age"});
- // インデックス情報を再解析する
- context.analyzeIndex("AGE_INDEX");
- // 検索キーを生成する
- SharedContextRecord searchKey = new SharedContextRecord(
- ":name,java.lang.String\n"
- ":age,int"
- );
- searchKey.setProperty("age", 10);
- // インデックスを使って検索し、キー集合を取得する
- Set keySet = context.createView().searchBy(searchKey, "AGE_INDEX", null).getResultSet();
- // 検索条件マップを生成する
- Map variables = new HashMap();
- variables.put("age", new Integer(10));
- // インタプリタ実行する
- Integer count = (Integer)context.executeInterpretQuery(
- "import java.util.*;"
- + "import jp.ossc.nimbus.service.context.SharedContextRecord;"
- + "int count = 0;"
- + "Iterator vals = context.values().iterator();"
- + "while(vals.hasNext()){"
- + "SharedContextRecord rec = (SharedContextRecord)itr.next();"
- + "if(rec.getProperty(\"age\") != null && rec.getProperty(\"age\").compareTo(age) >= 0){"
- + "count++;"
- + "}"
- + "}"
- + "return count;",
- variables
- );
実装サービスの一覧は以下のとおりです。
実装サービス | 実装概要 |
jp.ossc.nimbus.service.context.SharedContextService | デフォルト実装。 |
SharedContextインタフェースを継承しているため、サーバを跨って分散共有されている事を意識せずにデータの格納及び参照を行う事ができます。
アプリケーション向けインタフェースDistributedSharedContextを使ったサンプルは、SharedContextを参照。
実装サービスの一覧は以下のとおりです。
実装サービス | 実装概要 |
jp.ossc.nimbus.service.context.DistributedSharedContextService | デフォルト実装。 |
サンプルは、以下。