Class HazelcastCacheManager

  • All Implemented Interfaces:
    CacheManager, Destroyable, Initializable

    public class HazelcastCacheManager
    extends Object
    implements CacheManager, Initializable, Destroyable
    A CacheManager implementation backed by Hazelcast, "an open source clustering and highly scalable data distribution platform for Java"

    This implementation interacts with a HazelcastInstance to acquire named ConcurrentMap instances. Those clustered/distributed Map instances are then wrapped and made available to CacheManager callers as MapCache instances via getCache(String).

    Configuration

    This implementation's backing HazelcastInstance can be configured in one of three ways:
    1. Doing nothing and leveraging default Hazelcast configuration mechanisms
    2. Supplying an already-existing HazelcastInstance
    3. Supplying a Config instance and using that to create a new HazelcastInstance

    Default Configuration

    If you simply instantiate a HazelcastCacheManager and do nothing further, its backing HazelcastInstance instance will be created automatically by calling Hazelcast.newHazelcastInstance(null).

    The null argument instructs Hazelcast to use whatever default configuration mechanism it has at its disposal, usually a hazelcast.xml file at the root of the classpath, or if that is not present, the hazelcast-default.xml file contained in the Hazelcast .jar file itself.

    An existing HazelcastInstance

    If you have created a HazelcastInstance outside of Shiro's knowledge/control, you can simply configure it to be used by calling setHazelcastInstance.

    A Config instance

    If you do not want to use the above two options, you can have programmatic control over all of Hazelcast's configuration by creating and configuring a Config instance.

    Once constructed, you can set it via setConfig(config). This config instance will be used to acquire a new Hazelcast instance by calling Hazelcast.newHazelcastInstance(config)

    Since:
    1.3
    See Also:
    Hazelcast Configuration Documentation
    • Method Detail

      • getCache

        public <K,​V> Cache<K,​V> getCache​(String name)
                                              throws CacheException
        Returns a MapCache instance representing the named Hazelcast-managed IMap. The Hazelcast Map is obtained by calling hazelcastInstance.getMap(name).
        Specified by:
        getCache in interface CacheManager
        Type Parameters:
        K - the type of map key
        V - the type of map value
        Parameters:
        name - the name of the cache to acquire.
        Returns:
        a MapCache instance representing the named Hazelcast-managed IMap.
        Throws:
        CacheException
        See Also:
        HazelcastInstance.getMap(String), ensureHazelcastInstance()
      • createHazelcastInstance

        protected com.hazelcast.core.HazelcastInstance createHazelcastInstance()
        Implicitly creates and returns a new HazelcastInstance that will be used to back this implementation. This implementation calls:
         return Hazelcast.newHazelcastInstance(this.config);
         
        using any configured Config object. If no config object has been specified, this.config will be null, thereby using Hazelcast's default configuration mechanism.

        Can be overridden by subclasses for custom creation behavior.

        Returns:
        a new HazelcastInstance that will be used to back this implementation
        See Also:
        Hazelcast.newHazelcastInstance(com.hazelcast.config.Config), Config
      • isImplicitlyCreated

        protected final boolean isImplicitlyCreated()
        NOT PART OF SHIRO'S ACCESSIBLE API. DO NOT DEPEND ON THIS. This method was added for testing purposes only.

        Returns true if this HazelcastCacheManager instance implicitly created the backing HazelcastInstance, or false if one was externally provided via setHazelcastInstance.

        Returns:
        true if this HazelcastCacheManager instance implicitly created the backing HazelcastInstance, or false if one was externally provided via setHazelcastInstance.
      • getHazelcastInstance

        public com.hazelcast.core.HazelcastInstance getHazelcastInstance()
        Returns the HazelcastInstance from which named ConcurrentMap instances will be acquired to create MapCache instances.
        Returns:
        the HazelcastInstance from which named ConcurrentMap instances will be acquired to create MapCache instances.
      • setHazelcastInstance

        public void setHazelcastInstance​(com.hazelcast.core.HazelcastInstance hazelcastInstance)
        Sets the HazelcastInstance from which named ConcurrentMap instances will be acquired to create MapCache instances.
        Parameters:
        hazelcastInstance - the HazelcastInstance from which named ConcurrentMap instances will be acquired to create MapCache instances.
      • getConfig

        public com.hazelcast.config.Config getConfig()
        Returns the Hazelcast Config object to use to create a backing HazelcastInstance if one is not supplied, or null if the default Hazelcast configuration mechanisms will be used.
        Returns:
        the Hazelcast Config object to use to create a backing HazelcastInstance if one is not supplied, or null if the default Hazelcast configuration mechanisms will be used.
        See Also:
        Hazelcast.newHazelcastInstance(com.hazelcast.config.Config)