이 콘텐츠는 선택한 언어로 제공되지 않습니다.

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

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat