@MBean(objectName="CacheManager", description="Component that acts as a manager, factory and container for caches in the system.") public class DefaultCacheManager extends Object implements EmbeddedCacheManager
Cache
instance, and is often used as a
starting point to using the Cache
.
CacheManagers are heavyweight objects, and we foresee no more than one CacheManager being used per
JVM (unless specific configuration requirements require more than one; but either way, this would be a minimal and
finite number of instances).
Constructing a CacheManager is done via one of its constructors, which optionally take in a Configuration
or a path or URL to a configuration XML file.
Lifecycle - CacheManagers have a lifecycle (it implements Lifecycle
) and the default constructors
also call start()
. Overloaded versions of the constructors are available, that do not start the
CacheManager, although it must be kept in mind that CacheManagers need to be started before they
can be used to create Cache instances.
Once constructed, CacheManagers should be made available to any component that requires a Cache,
via JNDI or via some other mechanism such as an IoC container.
You obtain Cache instances from the CacheManager by using one of the overloaded
getCache(), methods. Note that with getCache(), there is no guarantee that the instance you get is
brand-new and empty, since caches are named and shared. Because of this, the CacheManager also acts as a
repository of Caches, and is an effective mechanism of looking up or creating Caches on demand.
When the system shuts down, it should call stop()
on the CacheManager. This will ensure all caches
within its scope are properly stopped as well.
Sample usage:
CacheManager manager = CacheManager.getInstance("my-config-file.xml");
Cache<String, Person> entityCache = manager.getCache("myEntityCache");
entityCache.put("aPerson", new Person());
ConfigurationBuilder confBuilder = new ConfigurationBuilder();
confBuilder.clustering().cacheMode(CacheMode.REPL_SYNC);
manager.createCache("myReplicatedCache", confBuilder.build());
Cache<String, String> replicatedCache = manager.getCache("myReplicatedCache");
Modifier and Type | Field and Description |
---|---|
static String |
OBJECT_NAME |
Constructor and Description |
---|
DefaultCacheManager()
Constructs and starts a default instance of the CacheManager, using configuration defaults.
|
DefaultCacheManager(boolean start)
Constructs a default instance of the CacheManager, using configuration defaults.
|
DefaultCacheManager(Configuration defaultConfiguration)
Deprecated.
Since 11.0, please use
DefaultCacheManager(ConfigurationBuilderHolder, boolean) instead. |
DefaultCacheManager(Configuration defaultConfiguration,
boolean start)
Deprecated.
Since 11.0, please use
DefaultCacheManager(ConfigurationBuilderHolder, boolean) instead. |
DefaultCacheManager(ConfigurationBuilderHolder holder,
boolean start)
Constructs a new instance of the CacheManager, using the holder passed in to read configuration settings.
|
DefaultCacheManager(GlobalConfiguration globalConfiguration)
Constructs and starts a new instance of the CacheManager, using the global configuration passed in, and system
defaults for the default named cache configuration.
|
DefaultCacheManager(GlobalConfiguration globalConfiguration,
boolean start)
Constructs a new instance of the CacheManager, using the global configuration passed in, and system defaults for
the default named cache configuration.
|
DefaultCacheManager(GlobalConfiguration globalConfiguration,
Configuration defaultConfiguration)
Deprecated.
Since 11.0, please use
DefaultCacheManager(ConfigurationBuilderHolder, boolean) instead. |
DefaultCacheManager(GlobalConfiguration globalConfiguration,
Configuration defaultConfiguration,
boolean start)
Deprecated.
Since 11.0, please use
DefaultCacheManager(ConfigurationBuilderHolder, boolean) instead. |
DefaultCacheManager(InputStream configurationStream)
Constructs and starts a new instance of the CacheManager, using the input stream passed in to read configuration
file contents.
|
DefaultCacheManager(InputStream configurationStream,
boolean start)
Constructs a new instance of the CacheManager, using the input stream passed in to read configuration file
contents.
|
DefaultCacheManager(String configurationFile)
Constructs and starts a new instance of the CacheManager, using the configuration file name passed in.
|
DefaultCacheManager(String configurationFile,
boolean start)
Constructs a new instance of the CacheManager, using the configuration file name passed in.
|
DefaultCacheManager(URL configurationURL,
boolean start)
Constructs a new instance of the CacheManager, using the input stream passed in to read configuration file
contents.
|
Modifier and Type | Method and Description |
---|---|
void |
addCacheDependency(String from,
String to)
Add a dependency between two caches.
|
CompletionStage<Void> |
addListenerAsync(Object listener)
Asynchronous version of
Listenable.addListener(Object) |
EmbeddedCacheManagerAdmin |
administration()
Provides an
EmbeddedCacheManagerAdmin whose methods affect the entire cluster as opposed to a single node. |
boolean |
cacheExists(String cacheName)
A cache is considered to exist if it has been created and started via
one of the
EmbeddedCacheManager.getCache() methods and has not yet been removed via
EmbeddedCacheManager.removeCache(String) . |
void |
close() |
<K,V> Cache<K,V> |
createCache(String name,
Configuration configuration)
Creates a cache on the local node using the supplied configuration.
|
Configuration |
defineConfiguration(String name,
Configuration configuration)
Register a cache configuration in the cache manager.
|
Configuration |
defineConfiguration(String name,
String template,
Configuration configurationOverride)
Defines a cache configuration by first reading the template configuration and then applying the override.
|
ClusterExecutor |
executor()
Providess the cache manager based executor.
|
Address |
getAddress()
Warning: the address may be
null before the first clustered cache starts
and after all the clustered caches have been stopped. |
<K,V> Cache<K,V> |
getCache()
Retrieves the default cache associated with this cache manager.
|
<K,V> Cache<K,V> |
getCache(String cacheName)
Retrieves a named cache from the system.
|
<K,V> Cache<K,V> |
getCache(String cacheName,
boolean createIfAbsent)
Similar to
EmbeddedCacheManager.getCache(String) , except if has the option
to not create the cache if it is not already running. |
Configuration |
getCacheConfiguration(String name)
Returns the configuration for the given cache.
|
Set<String> |
getCacheConfigurationNames()
This method returns a collection of all cache configuration names.
|
GlobalConfiguration |
getCacheManagerConfiguration()
Returns global configuration for this CacheManager
|
CacheManagerInfo |
getCacheManagerInfo() |
String |
getCacheManagerStatus() |
Set<String> |
getCacheNames()
This method returns a collection of all cache names.
|
ClassWhiteList |
getClassWhiteList() |
String |
getClusterMembers() |
String |
getClusterMembersPhysicalAddresses() |
String |
getClusterName() |
int |
getClusterSize() |
Address |
getCoordinator() |
String |
getCoordinatorAddress() |
String |
getCreatedCacheCount() |
Configuration |
getDefaultCacheConfiguration() |
String |
getDefinedCacheConfigurationNames() |
String |
getDefinedCacheCount() |
String |
getDefinedCacheNames() |
GlobalComponentRegistry |
getGlobalComponentRegistry() |
Properties |
getGlobalConfigurationAsProperties() |
Health |
getHealth()
Returns an entry point for a Health Check API.
|
Set<Object> |
getListeners()
Deprecated.
|
List<Address> |
getMembers() |
String |
getName() |
String |
getNodeAddress() |
int |
getNumberOfCacheConfigurations() |
long |
getNumberOfCreatedCaches() |
long |
getNumberOfRunningCaches() |
String |
getPhysicalAddresses() |
String |
getRunningCacheCount() |
CacheContainerStats |
getStats()
Returns statistics for this cache manager
|
ComponentStatus |
getStatus() |
Subject |
getSubject() |
Transport |
getTransport() |
String |
getVersion() |
boolean |
isCoordinator() |
boolean |
isDefaultRunning()
Tests whether the default cache is running.
|
boolean |
isRunning(String cacheName)
Tests whether a cache is running.
|
void |
removeCache(String cacheName)
Removes a cache with the given name from the system.
|
CompletionStage<Void> |
removeListenerAsync(Object listener)
Asynchronous version of
Listenable.removeListener(Object) |
void |
start()
Invoked on component start
|
void |
startCache() |
void |
startCache(String cacheName) |
EmbeddedCacheManager |
startCaches(String... cacheNames)
Starts a set of caches in parallel.
|
void |
stop()
Invoked on component stop
|
String |
toString() |
void |
undefineConfiguration(String configurationName)
Removes a configuration from the set of defined configurations.
|
EmbeddedCacheManager |
withSubject(Subject subject) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
addListener, removeListener
public static final String OBJECT_NAME
public DefaultCacheManager()
Configuration
and GlobalConfiguration
for details of these defaults.public DefaultCacheManager(boolean start)
Configuration
and GlobalConfiguration
for details of these defaults.start
- if true, the cache manager is started@Deprecated public DefaultCacheManager(Configuration defaultConfiguration)
DefaultCacheManager(ConfigurationBuilderHolder, boolean)
instead.Configuration
and GlobalConfiguration
for details of these defaults.defaultConfiguration
- configuration to use as a template for all caches created@Deprecated public DefaultCacheManager(Configuration defaultConfiguration, boolean start)
DefaultCacheManager(ConfigurationBuilderHolder, boolean)
instead.GlobalConfiguration
for details of these defaults.defaultConfiguration
- configuration file to use as a template for all caches createdstart
- if true, the cache manager is startedpublic DefaultCacheManager(GlobalConfiguration globalConfiguration)
Configuration
for details of these defaults.globalConfiguration
- GlobalConfiguration to use for all caches createdpublic DefaultCacheManager(GlobalConfiguration globalConfiguration, boolean start)
Configuration
for details of these defaults.globalConfiguration
- GlobalConfiguration to use for all caches createdstart
- if true, the cache manager is started.@Deprecated public DefaultCacheManager(GlobalConfiguration globalConfiguration, Configuration defaultConfiguration)
DefaultCacheManager(ConfigurationBuilderHolder, boolean)
instead.globalConfiguration
- global configuration to use. If null, a default instance is created.defaultConfiguration
- default configuration to use. If null, a default instance is created.@Deprecated public DefaultCacheManager(GlobalConfiguration globalConfiguration, Configuration defaultConfiguration, boolean start)
DefaultCacheManager(ConfigurationBuilderHolder, boolean)
instead.globalConfiguration
- global configuration to use. If null, a default instance is created.defaultConfiguration
- default configuration to use. If null, a default instance is created.start
- if true, the cache manager is startedpublic DefaultCacheManager(String configurationFile) throws IOException
configurationFile
- name of configuration file to use as a template for all caches createdIOException
- if there is a problem with the configuration file.public DefaultCacheManager(String configurationFile, boolean start) throws IOException
configurationFile
- name of configuration file to use as a template for all caches createdstart
- if true, the cache manager is startedIOException
- if there is a problem with the configuration file.public DefaultCacheManager(InputStream configurationStream) throws IOException
configurationStream
- stream containing configuration file contents, to use as a template for all caches
createdIOException
- if there is a problem with the configuration stream.public DefaultCacheManager(InputStream configurationStream, boolean start) throws IOException
configurationStream
- stream containing configuration file contents, to use as a template for all caches
createdstart
- if true, the cache manager is startedIOException
- if there is a problem reading the configuration streampublic DefaultCacheManager(URL configurationURL, boolean start) throws IOException
configurationURL
- stream containing configuration file contents, to use as a template for all caches
createdstart
- if true, the cache manager is startedIOException
- if there is a problem reading the configuration streampublic DefaultCacheManager(ConfigurationBuilderHolder holder, boolean start)
holder
- holder containing configuration settings, to use as a template for all caches createdstart
- if true, the cache manager is startedpublic Configuration defineConfiguration(String name, Configuration configuration)
EmbeddedCacheManager
ConfigurationBuilder.read(org.infinispan.configuration.cache.Configuration)
.
The other way to define a cache configuration is declaratively, in the XML file passed in to the cache
manager.defineConfiguration
in interface EmbeddedCacheManager
name
- name of the cache configurationconfiguration
- the cache configurationpublic Configuration defineConfiguration(String name, String template, Configuration configurationOverride)
EmbeddedCacheManager
EmbeddedCacheManager.defineConfiguration(String, Configuration)
.defineConfiguration
in interface EmbeddedCacheManager
name
- name of cache whose configuration is being definedtemplate
- configuration to use as a templateconfigurationOverride
- configuration overrides on top of the templatepublic void undefineConfiguration(String configurationName)
EmbeddedCacheManager
undefineConfiguration
in interface EmbeddedCacheManager
configurationName
- the named configurationpublic <K,V> Cache<K,V> createCache(String name, Configuration configuration)
EmbeddedCacheManager
EmbeddedCacheManager.defineConfiguration(String, Configuration)
and
EmbeddedCacheManager.getCache(String, boolean)
) needs to be invoked on all nodes.createCache
in interface EmbeddedCacheManager
K
- the generic type of the keyV
- the generic type of the valuename
- the name of the cacheconfiguration
- the configuration to use.public <K,V> Cache<K,V> getCache()
createCache(String)
(String)} since it is automatically created lazily
when first used.
As such, this method is always guaranteed to return the default cache.getCache
in interface BasicCacheContainer
getCache
in interface CacheContainer
getCache
in interface EmbeddedCacheManager
public <K,V> Cache<K,V> getCache(String cacheName)
defineConfiguration(String, Configuration)
or defineConfiguration(String, String, Configuration)
methods, or declared in the configuration file.getCache
in interface BasicCacheContainer
getCache
in interface CacheContainer
getCache
in interface EmbeddedCacheManager
cacheName
- name of cache to retrievepublic boolean cacheExists(String cacheName)
EmbeddedCacheManager
EmbeddedCacheManager.getCache()
methods and has not yet been removed via
EmbeddedCacheManager.removeCache(String)
.
In environments when caches are continuously created and removed, this
method offers the possibility to find out whether a cache has either,
not been started, or if it was started, whether it's been removed already
or not.cacheExists
in interface EmbeddedCacheManager
cacheName
- cache to checkpublic <K,V> Cache<K,V> getCache(String cacheName, boolean createIfAbsent)
EmbeddedCacheManager
EmbeddedCacheManager.getCache(String)
, except if has the option
to not create the cache if it is not already running.getCache
in interface EmbeddedCacheManager
cacheName
- name of cache to retrievecreateIfAbsent
- If true, this methods works just like EmbeddedCacheManager.getCache(String)
.
If false, return the already running cache or null.public EmbeddedCacheManager startCaches(String... cacheNames)
EmbeddedCacheManager
startCaches
in interface EmbeddedCacheManager
cacheNames
- the names of the caches to startpublic void removeCache(String cacheName)
EmbeddedCacheManager
removeCache
in interface EmbeddedCacheManager
cacheName
- name of cache to removepublic List<Address> getMembers()
getMembers
in interface EmbeddedCacheManager
null
if not connectedpublic Address getAddress()
null
before the first clustered cache starts
and after all the clustered caches have been stopped.getAddress
in interface EmbeddedCacheManager
null
if not connectedpublic Address getCoordinator()
getCoordinator
in interface EmbeddedCacheManager
null
if not connected@ManagedAttribute(description="The logical address of the cluster\'s coordinator", displayName="Coordinator address") public String getCoordinatorAddress()
@ManagedAttribute(description="Indicates whether this node is coordinator", displayName="Is coordinator?") public boolean isCoordinator()
isCoordinator
in interface EmbeddedCacheManager
null
if not connectedpublic void start()
Lifecycle
public void stop()
Lifecycle
public CompletionStage<Void> addListenerAsync(Object listener)
Listenable
Listenable.addListener(Object)
addListenerAsync
in interface Listenable
listener
- listener to add, must not be nullpublic CompletionStage<Void> removeListenerAsync(Object listener)
Listenable
Listenable.removeListener(Object)
removeListenerAsync
in interface Listenable
listener
- listener to remove, must not be null@Deprecated public Set<Object> getListeners()
getListeners
in interface Listenable
public ComponentStatus getStatus()
getStatus
in interface EmbeddedCacheManager
public GlobalConfiguration getCacheManagerConfiguration()
EmbeddedCacheManager
getCacheManagerConfiguration
in interface EmbeddedCacheManager
public Configuration getDefaultCacheConfiguration()
getDefaultCacheConfiguration
in interface EmbeddedCacheManager
null
if there is no default cache.public Configuration getCacheConfiguration(String name)
EmbeddedCacheManager
getCacheConfiguration
in interface EmbeddedCacheManager
public Set<String> getCacheNames()
BasicCacheContainer
getCacheNames
in interface BasicCacheContainer
public Set<String> getCacheConfigurationNames()
EmbeddedCacheManager
ConfigurationBuilderHolder
,
or at runtime via EmbeddedCacheManager.defineConfiguration(String, Configuration)
.
Internal caches defined via InternalCacheRegistry
are not included.getCacheConfigurationNames
in interface EmbeddedCacheManager
public boolean isRunning(String cacheName)
EmbeddedCacheManager
isRunning
in interface EmbeddedCacheManager
cacheName
- name of cache to test.public boolean isDefaultRunning()
EmbeddedCacheManager
isDefaultRunning
in interface EmbeddedCacheManager
@ManagedAttribute(description="The status of the cache manager instance.", displayName="Cache manager status", dataType=TRAIT) public String getCacheManagerStatus()
@ManagedAttribute(description="The defined cache names and their statuses. The default cache is not included in this representation.", displayName="List of defined caches", dataType=TRAIT) public String getDefinedCacheNames()
@ManagedAttribute(description="The defined cache configuration names.", displayName="List of defined cache configurations", dataType=TRAIT) public String getDefinedCacheConfigurationNames()
@ManagedAttribute(description="The total number of defined cache configurations.", displayName="Number of caches defined") public String getDefinedCacheCount()
@ManagedAttribute(description="The total number of defined cache configurations.", displayName="Number of caches defined") public int getNumberOfCacheConfigurations()
@ManagedAttribute(description="The total number of created caches, including the default cache.", displayName="Number of caches created") public String getCreatedCacheCount()
@ManagedAttribute(description="The total number of created caches, including the default cache.", displayName="Number of caches created") public long getNumberOfCreatedCaches()
@ManagedAttribute(description="The total number of running caches, including the default cache.", displayName="Number of running caches") public String getRunningCacheCount()
@ManagedAttribute(description="The total number of running caches, including the default cache.", displayName="Number of running caches") public long getNumberOfRunningCaches()
@ManagedAttribute(description="Returns the version of Infinispan", displayName="Infinispan version", dataType=TRAIT) public String getVersion()
@ManagedAttribute(description="The name of this cache manager", displayName="Cache manager name", dataType=TRAIT) public String getName()
@ManagedOperation(description="Starts the default cache associated with this cache manager", displayName="Starts the default cache") public void startCache()
@ManagedOperation(description="Starts a named cache from this cache manager", name="startCache", displayName="Starts a cache with the given name") public void startCache(@Parameter(name="cacheName",description="Name of cache to start") String cacheName)
@ManagedAttribute(description="The network address associated with this instance", displayName="Network address", dataType=TRAIT) public String getNodeAddress()
@ManagedAttribute(description="The physical network addresses associated with this instance", displayName="Physical network addresses", dataType=TRAIT) public String getPhysicalAddresses()
@ManagedAttribute(description="List of members in the cluster", displayName="Cluster members", dataType=TRAIT) public String getClusterMembers()
@ManagedAttribute(description="List of members in the cluster", displayName="Cluster members", dataType=TRAIT) public String getClusterMembersPhysicalAddresses()
@ManagedAttribute(description="Size of the cluster in number of nodes", displayName="Cluster size") public int getClusterSize()
@ManagedAttribute(description="Cluster name", displayName="Cluster name", dataType=TRAIT) public String getClusterName()
getClusterName
in interface EmbeddedCacheManager
public Transport getTransport()
getTransport
in interface EmbeddedCacheManager
public GlobalComponentRegistry getGlobalComponentRegistry()
getGlobalComponentRegistry
in interface EmbeddedCacheManager
public void addCacheDependency(String from, String to)
EmbeddedCacheManager
addCacheDependency
in interface EmbeddedCacheManager
from
- cache nameto
- cache name@ManagedAttribute(description="Global configuration properties", displayName="Global configuration properties", dataType=TRAIT) public Properties getGlobalConfigurationAsProperties()
public CacheContainerStats getStats()
EmbeddedCacheManager
getStats
in interface EmbeddedCacheManager
public Health getHealth()
EmbeddedCacheManager
getHealth
in interface EmbeddedCacheManager
EmbeddedCacheManager
.public CacheManagerInfo getCacheManagerInfo()
getCacheManagerInfo
in interface EmbeddedCacheManager
CacheManagerInfo
used to get basic info about the cache manager.public ClusterExecutor executor()
EmbeddedCacheManager
Note that not all EmbeddedCacheManager
implementations may implement this. Those that don't will throw
a UnsupportedOperationException
upon invocation.
executor
in interface EmbeddedCacheManager
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
public ClassWhiteList getClassWhiteList()
getClassWhiteList
in interface EmbeddedCacheManager
public EmbeddedCacheManagerAdmin administration()
EmbeddedCacheManager
EmbeddedCacheManagerAdmin
whose methods affect the entire cluster as opposed to a single node.administration
in interface CacheContainer
administration
in interface EmbeddedCacheManager
EmbeddedCacheManagerAdmin
public Subject getSubject()
getSubject
in interface EmbeddedCacheManager
public EmbeddedCacheManager withSubject(Subject subject)
withSubject
in interface EmbeddedCacheManager
Copyright © 2021 JBoss by Red Hat. All rights reserved.