28.2. 独自の JBoss Cache インスタンスをデプロイ
アプリケーションがカスタム使用できるよう JBoss Enterprise Application Platform 内部にユーザー独自の JBoss Cache インスタンスをデプロイすることは比較的一般的です。 本項では、 キャッシュをデプロイする方法を複数取り上げます。
28.2.1. CacheManager サービスによるデプロイメント
JBoss Cache を使用する標準の JBoss クラスタ化サービスは、 Enterprise Application Platform の CacheManager サービスからキャッシュへの参照を取得します (「JBoss Enterprise Application Platform の CacheManager サービス」 参照)。 エンドユーザーアプリケーションも下記の方法で同様の動作をさせることができます。
「CacheManager 設定の編集」 は CacheManager の 「CacheConfigurationRegistry」 Bean の設定になります。 新しい設定を追加するには、 追加する要素を Bean の
newConfigurations <map> 内に追加します。
<bean name="CacheConfigurationRegistry"
class="org.jboss.ha.cachemanager.DependencyInjectedConfigurationRegistry">
.....
<property name="newConfigurations">
<map keyClass="java.lang.String" valueClass="org.jboss.cache.config.Configuration">
<entry><key>my-custom-cache</key>
<value>
<bean name="MyCustomCacheConfig" class="org.jboss.cache.config.Configuration">
.... details of the my-custom-cache configuration
</bean>
</value>
</entry>
.....
設定例は、 「CacheManager 設定の編集」 を参照してください。
28.2.1.1. CacheManager へのアクセス
CacheManager にキャッシュ設定を追加したら、 次に CacheManager への参照をアプリケーションに提供します。 これには 3 つの方法があります。
- 依存関係の挿入アプリケーションが設定に JBoss Microcontainer を使用する場合、 サービスに CacheManager を挿入するのが最も簡単な方法となります。
<bean name="MyService" class="com.example.MyService"> <property name="cacheManager"><inject bean="CacheManager"/></property> </bean>
- JNDI ルックアップCacheManager を JNDI でルックアップすることもできます。
java:CacheManager以下にバインドされます。import org.jboss.ha.cachemanager.CacheManager; public class MyService { private CacheManager cacheManager; public void start() throws Exception { Context ctx = new InitialContext(); cacheManager = (CacheManager) ctx.lookup("java:CacheManager"); } } - CacheManagerLocatorJBoss Enterprise Application Platform は、 CacheManager へのアクセスに使用されるサービスロケーターオブジェクトも提供します。
import org.jboss.ha.cachemanager.CacheManager; import org.jboss.ha.framework.server.CacheManagerLocator; public class MyService { private CacheManager cacheManager; public void start() throws Exception { CacheManagerLocator locator = CacheManagerLocator.getCacheManagerLocator(); // Locator accepts as param a set of JNDI properties to help in lookup; // this isn't necessary inside the Enterprise Application Platform cacheManager = locator.getCacheManager(null); } }
CacheManager への参照を取得すれば、使用方法は簡単です。 希望する設定の名前を渡してキャッシュにアクセスします。 CacheManager はキャッシュを起動しません。 キャッシュの起動はアプリケーションが行いますが、 キャッシュサーバーで実行されている他のアプリケーションによって既に起動されていることがあります。 キャッシュを共有することもできます。 アプリケーションはキャッシュの使用を終了しても、 停止しません。 キャッシュが使用されていないことを CacheManager に伝え、 キャッシュを要求したすべての呼び出し元がキャッシュを解放した後、 CacheManager がキャッシュを停止します。
import org.jboss.cache.Cache;
import org.jboss.ha.cachemanager.CacheManager;
import org.jboss.ha.framework.server.CacheManagerLocator;
public class MyService {
private CacheManager cacheManager;
private Cache cache;
public void start() throws Exception {
Context ctx = new InitialContext();
cacheManager = (CacheManager) ctx.lookup("java:CacheManager");
// "true" param tells the manager to instantiate the cache if
// it doesn't exist yet
cache = cacheManager.getCache("my-cache-config", true);
cache.start();
}
public void stop() throws Exception {
cacheManager.releaseCache("my-cache-config");
}
}
POJO キャッシュのインスタンスへアクセスするために CacheManager を使用することもできます。
import org.jboss.cache.pojo.PojoCache;
import org.jboss.ha.cachemanager.CacheManager;
import org.jboss.ha.framework.server.CacheManagerLocator;
public class MyService {
private CacheManager cacheManager;
private PojoCache pojoCache;
public void start() throws Exception {
Context ctx = new InitialContext();
cacheManager = (CacheManager) ctx.lookup("java:CacheManager");
// "true" param tells the manager to instantiate the cache if
// it doesn't exist yet
pojoCache = cacheManager.getPojoCache("my-cache-config", true);
pojoCache.start();
}
public void stop() throws Exception {
cacheManager.releaseCache("my-cache-config");
}
}