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 |
DEFAULT_CACHE_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)
Constructs and starts a new instance of the CacheManager, using the default configuration passed in.
|
DefaultCacheManager(Configuration defaultConfiguration,
boolean start)
Constructs a new instance of the CacheManager, using the default configuration passed in.
|
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)
Constructs and starts a new instance of the CacheManager, using the global and default configurations passed in.
|
DefaultCacheManager(GlobalConfiguration globalConfiguration,
Configuration defaultConfiguration,
boolean start)
Constructs a new instance of the CacheManager, using the global and default configurations passed in.
|
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(String globalConfigurationFile,
String defaultConfigurationFile,
String namedCacheFile,
boolean start)
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
void |
addCacheDependency(String from,
String to)
Add a dependency between two caches.
|
void |
addListener(Object listener)
Adds a listener to the component.
|
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
CacheContainer.getCache() methods and has not yet been removed via
EmbeddedCacheManager.removeCache(String) . |
<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)
Defines a named cache's configuration by using the provided configuration
Unlike previous versions of Infinispan, this method does not build on an existing configuration (default or named).
|
Configuration |
defineConfiguration(String name,
String template,
Configuration configurationOverride)
Defines a named cache's configuration using by first reading the template configuration and then applying
the override afterwards to generate a configuration.
|
ClusterExecutor |
executor()
Providess the cache manager based executor.
|
Address |
getAddress() |
<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)
Retrieves a named cache from the system in the same way that
CacheContainer.getCache(String) does except that if offers the possibility for the
named cache not to be retrieved if it has not yet been started, or if
it's been removed after being started. |
<K,V> Cache<K,V> |
getCache(String cacheName,
String configurationName)
Retrieves a named cache from the system in the same way that
CacheContainer.getCache(String) does except that if offers the possibility to specify
a specific configuration template. |
<K,V> Cache<K,V> |
getCache(String cacheName,
String configurationTemplate,
boolean createIfAbsent)
Retrieves a named cache from the system in the same way that
CacheContainer.getCache(String) does except that if offers the possibility to specify
a specific configuration template. |
Configuration |
getCacheConfiguration(String name)
Returns the configuration for the given cache.
|
Set<String> |
getCacheConfigurationNames()
This method returns a collection of cache configuration names which contains the
cache configurations that have been defined via XML or programmatically, and the
cache configurations that have been defined at runtime via this cache manager
instance.
|
GlobalConfiguration |
getCacheManagerConfiguration()
Returns global configuration for this CacheManager
|
String |
getCacheManagerStatus() |
Set<String> |
getCacheNames()
This method returns a collection of caches names which contains the
caches that have been defined via XML or programmatically, and the
caches that have been created at runtime via this cache manager
instance.
|
ClassWhiteList |
getClassWhiteList() |
String |
getClusterMembers() |
String |
getClusterName() |
int |
getClusterSize() |
Address |
getCoordinator() |
String |
getCoordinatorAddress() |
String |
getCreatedCacheCount() |
Configuration |
getDefaultCacheConfiguration()
Returns default configuration for this CacheManager
|
String |
getDefinedCacheConfigurationNames() |
String |
getDefinedCacheCount() |
String |
getDefinedCacheNames() |
GlobalComponentRegistry |
getGlobalComponentRegistry() |
Properties |
getGlobalConfigurationAsProperties() |
Health |
getHealth()
Returns an entry point for a Health Check API.
|
Set<Object> |
getListeners() |
List<Address> |
getMembers() |
String |
getName() |
String |
getNodeAddress() |
String |
getPhysicalAddresses() |
String |
getRunningCacheCount() |
CacheContainerStats |
getStats()
Returns statistics for this cache manager
since 7.1
|
ComponentStatus |
getStatus() |
Transport |
getTransport() |
String |
getVersion() |
<K,V> Cache<K,V> |
internalGetCache(String cacheName,
String configurationName) |
boolean |
isCoordinator() |
boolean |
isDefaultRunning()
Tests whether the default cache is running.
|
boolean |
isRunning(String cacheName)
Tests whether a named cache is running.
|
void |
removeCache(String cacheName)
Removes a cache with the given name from the system.
|
void |
removeListener(Object listener)
Removes a listener from the component.
|
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.
|
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 startedpublic DefaultCacheManager(Configuration defaultConfiguration)
Configuration
and GlobalConfiguration
for details of these defaults.defaultConfiguration
- configuration to use as a template for all caches createdpublic DefaultCacheManager(Configuration defaultConfiguration, boolean start)
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.public DefaultCacheManager(GlobalConfiguration globalConfiguration, Configuration defaultConfiguration)
globalConfiguration
- global configuration to use. If null, a default instance is created.defaultConfiguration
- default configuration to use. If null, a default instance is created.public DefaultCacheManager(GlobalConfiguration globalConfiguration, Configuration defaultConfiguration, boolean start)
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(ConfigurationBuilderHolder holder, boolean start)
holder
- holder containing configuration settings, to use as a template for all caches createdstart
- if true, the cache manager is started@Deprecated public DefaultCacheManager(String globalConfigurationFile, String defaultConfigurationFile, String namedCacheFile, boolean start) throws IOException
start
- if true, the cache manager is startedglobalConfigurationFile
- name of file that contains the global configurationdefaultConfigurationFile
- name of file that contains the default configurationnamedCacheFile
- name of file that contains the named cache configurationIOException
- if there is a problem with the configuration file.public Configuration defineConfiguration(String name, Configuration configuration)
EmbeddedCacheManager
ConfigurationBuilder.read(org.infinispan.configuration.cache.Configuration)
.
The other way to define named cache's configuration is declaratively, in the XML file passed in to the cache
manager.
If this cache was already configured either declaritively or programmatically this method will throw a
CacheConfigurationException
.defineConfiguration
in interface EmbeddedCacheManager
name
- name of cache whose configuration is being definedconfiguration
- configuration overrides to usepublic Configuration defineConfiguration(String name, String template, Configuration configurationOverride)
EmbeddedCacheManager
EmbeddedCacheManager.defineConfiguration(String, Configuration)
.
If this cache was already configured either declaratively or programmatically this method will throw a
CacheConfigurationException
.defineConfiguration
in interface EmbeddedCacheManager
name
- name of cache whose configuration is being definedtemplate
- name of cache to use as a template before overrides are applied to itconfigurationOverride
- configuration overrides to usepublic void undefineConfiguration(String configurationName)
EmbeddedCacheManager
IllegalStateException
is thrown. If the named configuration does not exist, nothing
happensundefineConfiguration
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)
(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
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
cacheName
- name of cache to retrievepublic <K,V> Cache<K,V> getCache(String cacheName, String configurationName)
EmbeddedCacheManager
CacheContainer.getCache(String)
does except that if offers the possibility to specify
a specific configuration template. Multiple caches can be created using
the same configuration.getCache
in interface EmbeddedCacheManager
cacheName
- name of cache to retrieveconfigurationName
- name of the configuration template to usepublic <K,V> Cache<K,V> internalGetCache(String cacheName, String configurationName)
public boolean cacheExists(String cacheName)
EmbeddedCacheManager
CacheContainer.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
public <K,V> Cache<K,V> getCache(String cacheName, boolean createIfAbsent)
EmbeddedCacheManager
CacheContainer.getCache(String)
does except that if offers the possibility for the
named cache not to be retrieved if it has not yet been started, or if
it's been removed after being started. If a non-template configuration
exists with the same name, it will be used to configure the cache.getCache
in interface EmbeddedCacheManager
cacheName
- name of cache to retrievecreateIfAbsent
- if false, the named cache will not be
retrieved if it hasn't been retrieved previously or if it's been
removed. If true, this methods works just like CacheContainer.getCache(String)
public <K,V> Cache<K,V> getCache(String cacheName, String configurationTemplate, boolean createIfAbsent)
EmbeddedCacheManager
CacheContainer.getCache(String)
does except that if offers the possibility to specify
a specific configuration template. Multiple caches can be created using
the same configuration. Tihs method also offers the possibility for the
named cache not to be retrieved if it has not yet been started, or if
it's been removed after being started.getCache
in interface EmbeddedCacheManager
cacheName
- name of cache to retrieveconfigurationTemplate
- name of the configuration template to usecreateIfAbsent
- if false, the named cache will not be
retrieved if it hasn't been retrieved previously or if it's been
removed. If true, this methods works just like EmbeddedCacheManager.getCache(String, String)
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
public Address getAddress()
getAddress
in interface EmbeddedCacheManager
public Address getCoordinator()
getCoordinator
in interface EmbeddedCacheManager
public String getCoordinatorAddress()
public boolean isCoordinator()
isCoordinator
in interface EmbeddedCacheManager
public void start()
Lifecycle
public void stop()
Lifecycle
public void addListener(Object listener)
Listenable
Listener
and
further to that, contain methods annotated appropriately, otherwise the listener will not be registered.
See the Listener
annotation for more information.
addListener
in interface Listenable
listener
- must not be null.public void removeListener(Object listener)
Listenable
removeListener
in interface Listenable
listener
- listener to remove. Must not be null.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()
EmbeddedCacheManager
getDefaultCacheConfiguration
in interface EmbeddedCacheManager
public Configuration getCacheConfiguration(String name)
EmbeddedCacheManager
getCacheConfiguration
in interface EmbeddedCacheManager
public Set<String> getCacheNames()
BasicCacheContainer
InternalCacheRegistry
getCacheNames
in interface BasicCacheContainer
public Set<String> getCacheConfigurationNames()
EmbeddedCacheManager
InternalCacheRegistry
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
public String getCacheManagerStatus()
public String getDefinedCacheNames()
public String getDefinedCacheConfigurationNames()
public String getDefinedCacheCount()
public String getCreatedCacheCount()
public String getRunningCacheCount()
public String getVersion()
public String getName()
public void startCache()
public void startCache(String cacheName)
public String getNodeAddress()
public String getPhysicalAddresses()
public String getClusterMembers()
public int getClusterSize()
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 namepublic Properties getGlobalConfigurationAsProperties()
public CacheContainerStats getStats()
EmbeddedCacheManager
getStats
in interface EmbeddedCacheManager
public Health getHealth()
EmbeddedCacheManager
getHealth
in interface EmbeddedCacheManager
EmbeddedCacheManager
.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 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 ClassWhiteList getClassWhiteList()
getClassWhiteList
in interface EmbeddedCacheManager
Copyright © 2018 JBoss, a division of Red Hat. All rights reserved.