此内容没有您所选择的语言版本。

29.2. Deploying Your Own JBoss Cache Instance


It's quite common for users to deploy their own instances of JBoss Cache inside JBoss Enterprise Application Platform for custom use by their applications. In this section we describe the various ways caches can be deployed.

29.2.1. Deployment Via the CacheManager Service

The standard JBoss clustered services that use JBoss Cache obtain a reference to their cache from the Enterprise Application Platform's CacheManager service (see Section 21.2.1, “The JBoss Enterprise Application Platform CacheManager Service”). End user applications can do the same thing; here's how.
Section 29.1.1, “Editing the CacheManager Configuration” shows the configuration of the CacheManager's "CacheConfigurationRegistry" bean. To add a new configuration, you would add an additional element inside that bean's 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>    
       .....
Copy to Clipboard Toggle word wrap

29.2.1.1. Accessing the CacheManager

Once you've added your cache configuration to the CacheManager, the next step is to provide a reference to the CacheManager to your application. There are three ways to do this:
  • Dependency Injection
    If your application uses the JBoss Microcontainer for configuration, the simplest mechanism is to have it inject the CacheManager into your service.
    <bean name="MyService" class="com.example.MyService">
       <property name="cacheManager"><inject bean="CacheManager"/></property>
    </bean>
    Copy to Clipboard Toggle word wrap
  • JNDI Lookup
    Alternatively, you can find look up the CacheManger is JNDI. It is bound under 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");
       }
    }
    Copy to Clipboard Toggle word wrap
  • CacheManagerLocator
    JBoss Enterprise Application Platform also provides a service locator object that can be used to access the 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 is not necessary inside the Enterprise Application Platform
           cacheManager = locator.getCacheManager(null);
       }
    }
    Copy to Clipboard Toggle word wrap
Once a reference to the CacheManager is obtained; usage is simple. Access a cache by passing in the name of the desired configuration. The CacheManager will not start the cache; this is the responsibility of the application. The cache may, however, have been started by another application running in the cache server; the cache may be shared. When the application is done using the cache, it should not stop. Just inform the CacheManager that the cache is no longer being used; the manager will stop the cache when all callers that have asked for the cache have released it.
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 does not exist yet
       cache = cacheManager.getCache("my-cache-config", true);
       
       cache.start();
   }
   
   public void stop() throws Exception {
       cacheManager.releaseCache("my-cache-config");
   }
}
Copy to Clipboard Toggle word wrap
The CacheManager can also be used to access instances of POJO Cache.
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 does not exist yet
       pojoCache = cacheManager.getPojoCache("my-cache-config", true);
       
       pojoCache.start();
   }
   
   public void stop() throws Exception {
       cacheManager.releaseCache("my-cache-config");
   }
}
Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat