Terracotta 10.3 | Ehcache API Developer Guide | Management and Monitoring with Ehcache | Managing multiple cache managers
 
Managing multiple cache managers
The default ManagementRegistry instance that is created when none are manually registered only manages a single cache manager by default, but sometimes you may want one ManagementRegistry to manage multiple cache managers.
ManagementRegistry instances are thread-safe, so one instance can be shared amongst multiple cache managers:

CacheConfiguration<Long, String> cacheConfiguration =
CacheConfigurationBuilder.newCacheConfigurationBuilder(
Long.class, String.class, ResourcePoolsBuilder.heap(10))
.build();

CacheManager cacheManager1 = null;
CacheManager cacheManager2 = null;
try {
SharedManagementService sharedManagementService =
new DefaultSharedManagementService(); // 1
cacheManager1 = CacheManagerBuilder.newCacheManagerBuilder()
.withCache("aCache", cacheConfiguration)
.using(new DefaultManagementRegistryConfiguration()
.setCacheManagerAlias("myCacheManager-1"))
.using(sharedManagementService) // 2
.build(true);

cacheManager2 = CacheManagerBuilder.newCacheManagerBuilder()
.withCache("aCache", cacheConfiguration)
.using(new DefaultManagementRegistryConfiguration()
.setCacheManagerAlias("myCacheManager-2"))
.using(sharedManagementService) // 3
.build(true);

Context context1 = Context.empty()
.with("cacheManagerName", "myCacheManager-1")
.with("cacheName", "aCache");

Context context2 = Context.empty()
.with("cacheManagerName", "myCacheManager-2")
.with("cacheName", "aCache");

Cache<Long, String> cache =
cacheManager1.getCache("aCache", Long.class, String.class);
cache.get(1L);//cache miss
cache.get(2L);//cache miss

StatisticQuery query = sharedManagementService
.withCapability("StatisticsCapability")
.queryStatistic("Cache:MissCount")
.on(context1)
.on(context2)
.build();

long val = 0;
// it could be several seconds before the sampled stats
// could become available
// let's try until we find the correct value : 2
do {
ResultSet<ContextualStatistics> counters = query.execute();

ContextualStatistics statisticsContext1 =
counters.getResult(context1);

Number counterContext1 = statisticsContext1.
getStatistic("Cache:MissCount");

// miss count is a sampled stat,
//for example its values could be [0,1,2].
// In the present case, only the last value is important to us,
// the cache was eventually missed 2 times
val = counterContext1.longValue();
} while(val != 2);
}
finally {
if(cacheManager2 != null) cacheManager2.close();
if(cacheManager1 != null) cacheManager1.close();
}
1
Create an instance of org.ehcache.management.SharedManagementService
2
Pass it as a service to the first cache manager
3
Pass it as a service to the second cache manager
This way, all managed objects get registered into a common ManagementRegistry instance.

Copyright © 2010-2019 | Software AG, Darmstadt, Germany and/or Software AG USA, Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.