Terracotta 10.7 | Ehcache API Developer Guide | Management and Monitoring with Ehcache | Capabilities and contexts
 
Capabilities and contexts
Capabilities are metadata of what the managed objects are capable of: a collection of statistic that can be queried and/or remote actions that can be called. Each capability requires a context to run in. For instance, cache-specific statistics require a cache manager name and a cache name to uniquely identify the cache on which you want to query stats or call an action.

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

CacheManager cacheManager = null;
try {
ManagementRegistryService managementRegistry =
new DefaultManagementRegistryService();
cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.withCache("aCache", cacheConfiguration)
.using(managementRegistry)
.build(true);


Collection<? extends Capability> capabilities =
managementRegistry.getCapabilities(); // 1
Assert.assertThat(capabilities.isEmpty(), Matchers.is(false));
Capability capability = capabilities.iterator().next();
String capabilityName = capability.getName(); // 2
Collection<? extends Descriptor> capabilityDescriptions =
capability.getDescriptors(); // 3
Assert.assertThat(capabilityDescriptions.isEmpty(),
Matchers.is(false));
CapabilityContext capabilityContext =
capability.getCapabilityContext();
Collection<CapabilityContext.Attribute> attributes =
capabilityContext.getAttributes(); // 4
Assert.assertThat(attributes.size(), Matchers.is(2));
Iterator<CapabilityContext.Attribute> iterator =
attributes.iterator();
CapabilityContext.Attribute attribute1 = iterator.next();
Assert.assertThat(attribute1.getName(),
Matchers.equalTo("cacheManagerName")); // 5
Assert.assertThat(attribute1.isRequired(), Matchers.is(true));
CapabilityContext.Attribute attribute2 = iterator.next();
Assert.assertThat(attribute2.getName(),
Matchers.equalTo("cacheName")); // 6
Assert.assertThat(attribute2.isRequired(), Matchers.is(true));

ContextContainer contextContainer =
managementRegistry.getContextContainer(); // 7
Assert.assertThat(contextContainer.getName(),
Matchers.equalTo("cacheManagerName")); // 8
Assert.assertThat(contextContainer.getValue(),
Matchers.startsWith("cache-manager-"));
Collection<ContextContainer> subContexts =
contextContainer.getSubContexts();
Assert.assertThat(subContexts.size(), Matchers.is(1));
ContextContainer subContextContainer =
subContexts.iterator().next();
Assert.assertThat(subContextContainer.getName(),
Matchers.equalTo("cacheName")); // 9
Assert.assertThat(subContextContainer.getValue(),
Matchers.equalTo("aCache"));
}
finally {
if(cacheManager != null) cacheManager.close();
}
1
Query the ManagementRegistry for the registered managed objects' capabilities.
2
Each capability has a unique name you will need to refer to it.
3
Each capability has a collection of Descriptors that contains the metadata of each statistic or action.
4
Each capability requires a context to which it needs to refer to.
5
The first attribute of this context is the cache manager name.
6
The second attribute of this context is the cache name. With both attributes, the capability can uniquely refer to a unique managed object.
7
Query the ManagementRegistry for the all the registered managed objects' contexts.
8
There is only one context here, and its name is the cache manager's name.
9
The above context has a subcontext: the cache's name.
The context containers give you all the attributes of all existing contexts. You can match the values returned by a context container to a capability's context by matching their respective names.