13.9. Hot Rod Java クライアント
Hot Rod はバイナリーの言語非依存プロトコルです。Java クライアントは、Hot Rod Java Client API を使用して Hot Rod プロトコルを介してサーバーと対話できます。
13.9.1. Hot Rod Java クライアントのダウンロード
JBoss Data Grid Hot Rod Java クライアントをダウンロードするには、次の手順に従ってください。
手順13.2 Hot Rod Java クライアントのダウンロード
- カスタマーポータル (https://access.redhat.com) にログインします。
- ページ最上部にある ダウンロード ボタンをクリックします。
- 製品のダウンロード ページで Red Hat JBoss Data Grid をクリックします。
- バージョン: ドロップダウンメニューから適切な JBoss Data Grid バージョンを選択します。
- Red Hat JBoss Data Grid ${VERSION} Hot Rod Java Client エントリーを探し、対応する ダウンロード リンクをクリックします。
13.9.2. Hot Rod Java クライアントの設定
Hot Rod Java クライアントは、プログラムを使用したり、設定ファイルまたはプロパティーファイルを外部的に使用したりして設定されます。次の例は、利用可能な Java 対応 API を使用したクライアントインスタンスの作成を示しています。
例13.1 クライアントインスタンスの作成
org.infinispan.client.hotrod.configuration.ConfigurationBuilder cb = new org.infinispan.client.hotrod.configuration.ConfigurationBuilder(); cb.tcpNoDelay(true) .connectionPool() .numTestsPerEvictionRun(3) .testOnBorrow(false) .testOnReturn(false) .testWhileIdle(true) .addServer() .host("localhost") .port(11222); RemoteCacheManager rmc = new RemoteCacheManager(cb.build());
プロパティーファイルを使用した Hot Rod Java クライアントの設定
Hot Rod Java クライアントを設定するには、クラスパス上の hotrod-client.properties
ファイルを編集します。
次の例は、
hotrod-client.properties
ファイルの内容を示しています。
例13.2 設定
infinispan.client.hotrod.transport_factory = org.infinispan.client.hotrod.impl.transport.tcp.TcpTransportFactory infinispan.client.hotrod.server_list = 127.0.0.1:11222 infinispan.client.hotrod.marshaller = org.infinispan.commons.marshall.jboss.GenericJBossMarshaller infinispan.client.hotrod.async_executor_factory = org.infinispan.client.hotrod.impl.async.DefaultAsyncExecutorFactory infinispan.client.hotrod.default_executor_factory.pool_size = 1 infinispan.client.hotrod.default_executor_factory.queue_size = 10000 infinispan.client.hotrod.hash_function_impl.1 = org.infinispan.client.hotrod.impl.consistenthash.ConsistentHashV1 infinispan.client.hotrod.tcp_no_delay = true infinispan.client.hotrod.ping_on_startup = true infinispan.client.hotrod.request_balancing_strategy = org.infinispan.client.hotrod.impl.transport.tcp.RoundRobinBalancingStrategy infinispan.client.hotrod.key_size_estimate = 64 infinispan.client.hotrod.value_size_estimate = 512 infinispan.client.hotrod.force_return_values = false infinispan.client.hotrod.tcp_keep_alive = true ## below is connection pooling config maxActive=-1 maxTotal = -1 maxIdle = -1 whenExhaustedAction = 1 timeBetweenEvictionRunsMillis=120000 minEvictableIdleTimeMillis=300000 testWhileIdle = true minIdle = 1
注記
TCP
KEEPALIVE
設定は、 例で示された設定プロパティー (infinispan.client.hotrod.tcp_keep_alive = true/false
) または org.infinispan.client.hotrod.ConfigurationBuilder.tcpKeepAlive()
メソッドを使用したプログラムによって Hot Rod Java クライアントで有効/無効になります。
Red Hat JBoss Data Grid でプロパティーファイルを使用するには、次の 2 つのコンストラクターのいずれかを使用する必要があります。
new RemoteCacheManager(boolean start)
new RemoteCacheManager()
13.9.3. Hot Rod Java クライアント基本 API
以下のコードは、クライアント API を使用して Hot Rod Java クライアントで Hot Rod サーバーから情報を保存または取得する方法を示しています。この例では、Hot Rod サーバーがデフォルトの場所
localhost:11222
にバインドするよう起動されていることを前提とします。
例13.3 基本 API
//API entry point, by default it connects to localhost:11222 BasicCacheContainer cacheContainer = new RemoteCacheManager(); //obtain a handle to the remote default cache BasicCache<String, String> cache = cacheContainer.getCache(); //now add something to the cache and ensure it is there cache.put("car", "ferrari"); assert cache.get("car").equals("ferrari"); //remove the data cache.remove("car"); assert !cache.containsKey("car") : "Value must have been removed!";
RemoteCacheManager
は、DefaultCacheManager
に対応し、両方とも BasicCacheContainer
を実装します。
この API は、ローカルコールから Hot Rod を介したリモートコールへの移行を実現します。これは、
DefaultCacheManager
と RemoteCacheManager
を切り替えることによって行うことができ、共通の BasicCacheContainer
インターフェースによって単純化されます。
すべてのキーは、
keySet()
メソッドを使用してリモートキャッシュから取得できます。リモートキャッシュが分散キャッシュである場合は、サーバーによりマップ/削減ジョブが開始され、クラスタノードからすべてのキーが取得され、すべてのキーがクライアントに返されます。
キーの数が多い場合は、このメソッドを注意して使用してください。
Set keys = remoteCache.keySet();
13.9.4. Hot Rod Java クライアントバージョン API
データの整合性を確保するために、Hot Rod は各変更を一意に識別するバージョン番号を保存します。
getVersioned
を使用して、クライアントはキーと現在のバージョンに関連付けられた値を取得できます。
Hot Rod Java クライアントを使用する場合、
RemoteCacheManager
は、リモートクラスタ上の名前付きまたはデフォルトのキャッシュにアクセスする RemoteCache
インターフェースのインスタンスを提供します。これにより、バージョン API を含む、新しいメソッドを追加する Cache
インターフェースが拡張されます。
例13.4 バージョンメソッドの使用
// To use the versioned API, remote classes are specifically needed RemoteCacheManager remoteCacheManager = new RemoteCacheManager(); RemoteCache<String, String> remoteCache = remoteCacheManager.getCache(); remoteCache.put("car", "ferrari"); VersionedValue valueBinary = remoteCache.getVersioned("car"); // removal only takes place only if the version has not been changed // in between. (a new version is associated with 'car' key on each change) assert remoteCache.removeWithVersion("car", valueBinary.getVersion()); assert !remoteCache.containsKey("car");
例13.5 置換の使用
remoteCache.put("car", "ferrari"); VersionedValue valueBinary = remoteCache.getVersioned("car"); assert remoteCache.replaceWithVersion("car", "lamborghini", valueBinary.getVersion());