Class RemoteCacheManager

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, RemoteCacheManagerMXBean, RemoteCacheContainer, BasicCacheContainer, Lifecycle

    public class RemoteCacheManager
    extends java.lang.Object
    implements RemoteCacheContainer, java.io.Closeable, RemoteCacheManagerMXBean

    Factory for RemoteCaches.

    In order to be able to use a RemoteCache, the RemoteCacheManager must be started first: this instantiates connections to Hot Rod server(s). Starting the RemoteCacheManager can be done either at creation by passing start==true to the constructor or by using a constructor that does that for you; or after construction by calling start().

    NOTE: this is an "expensive" object, as it manages a set of persistent TCP connections to the Hot Rod servers. It is recommended to only have one instance of this per JVM, and to cache it between calls to the server (i.e. remoteCache operations)

    stop() needs to be called explicitly in order to release all the resources (e.g. threads, TCP connections).

    Since:
    4.1
    Author:
    Mircea.Markus@jboss.com
    • Field Detail

      • DEFAULT_CACHE_NAME

        public static final java.lang.String DEFAULT_CACHE_NAME
        See Also:
        Constant Field Values
      • HOTROD_CLIENT_PROPERTIES

        public static final java.lang.String HOTROD_CLIENT_PROPERTIES
        See Also:
        Constant Field Values
      • JSON_STRING_ARRAY_ELEMENT_REGEX

        public static final java.lang.String JSON_STRING_ARRAY_ELEMENT_REGEX
        See Also:
        Constant Field Values
      • channelFactory

        protected org.infinispan.client.hotrod.impl.transport.netty.ChannelFactory channelFactory
      • listenerNotifier

        protected org.infinispan.client.hotrod.event.impl.ClientListenerNotifier listenerNotifier
    • Constructor Detail

      • RemoteCacheManager

        public RemoteCacheManager​(Configuration configuration)
        Create a new RemoteCacheManager using the supplied Configuration. The RemoteCacheManager will be started automatically
        Parameters:
        configuration - the configuration to use for this RemoteCacheManager
        Since:
        5.3
      • RemoteCacheManager

        public RemoteCacheManager​(Configuration configuration,
                                  boolean start)
        Create a new RemoteCacheManager using the supplied Configuration. The RemoteCacheManager will be started automatically only if the start parameter is true
        Parameters:
        configuration - the configuration to use for this RemoteCacheManager
        start - whether or not to start the manager on return from the constructor.
        Since:
        5.3
      • RemoteCacheManager

        public RemoteCacheManager​(boolean start)

        Similar to RemoteCacheManager(Configuration, boolean), but it will try to lookup the config properties in the classpath, in a file named hotrod-client.properties. If no properties can be found in the classpath, defaults will be used, attempting to connect to 127.0.0.1:11222

        Refer to ConfigurationBuilder for a detailed list of available properties.

        Parameters:
        start - whether or not to start the RemoteCacheManager
        Throws:
        HotRodClientException - if such a file cannot be found in the classpath
    • Method Detail

      • getConfiguration

        public Configuration getConfiguration()
        Description copied from interface: RemoteCacheContainer
        Retrieves the configuration currently in use. The configuration object is immutable. If you wish to change configuration, you should use the following pattern:
        
         ConfigurationBuilder builder = new ConfigurationBuilder();
         builder.read(remoteCacheManager.getConfiguration());
         // modify builder
         remoteCacheManager.stop();
         remoteCacheManager = new RemoteCacheManager(builder.build());
         
        Specified by:
        getConfiguration in interface RemoteCacheContainer
        Returns:
        The configuration of this RemoteCacheManager
        Since:
        5.3
      • getCache

        public <K,​V> RemoteCache<K,​V> getCache​(java.lang.String cacheName)
        Retrieves a named cache from the remote server if the cache has been defined, otherwise if the cache name is undefined, it will return null.
        Specified by:
        getCache in interface BasicCacheContainer
        Parameters:
        cacheName - name of cache to retrieve
        Returns:
        a cache instance identified by cacheName or null if the cache name has not been defined
      • getCacheNames

        public java.util.Set<java.lang.String> getCacheNames()
        Description copied from interface: BasicCacheContainer
        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. If no named caches are registered or no caches have been created, this method returns an empty set. The list of caches does not include internal-only caches InternalCacheRegistry
        Specified by:
        getCacheNames in interface BasicCacheContainer
        Returns:
        an immutable set of caches registered or created with this cache manager.
      • getCache

        public <K,​V> RemoteCache<K,​V> getCache()
        Retrieves the default cache from the remote server.
        Specified by:
        getCache in interface BasicCacheContainer
        Returns:
        a remote cache instance that can be used to send requests to the default cache in the server
      • getCache

        public <K,​V> RemoteCache<K,​V> getCache​(java.lang.String cacheName,
                                                           TransactionMode transactionMode,
                                                           javax.transaction.TransactionManager transactionManager)
        Specified by:
        getCache in interface RemoteCacheContainer
        Parameters:
        cacheName - The cache's name.
        transactionMode - The TransactionMode to override. If null, it uses the configured value.
        transactionManager - The TransactionManager to override. If null, it uses the configured value.
        Returns:
        the RemoteCache implementation.
      • getCache

        public <K,​V> RemoteCache<K,​V> getCache​(java.lang.String cacheName,
                                                           boolean forceReturnValue,
                                                           TransactionMode transactionMode,
                                                           javax.transaction.TransactionManager transactionManager)
        Specified by:
        getCache in interface RemoteCacheContainer
        Parameters:
        cacheName - The cache's name.
        forceReturnValue - true to force a return value when it is not needed.
        transactionMode - The TransactionMode to override. If null, it uses the configured value.
        transactionManager - The TransactionManager to override. If null, it uses the configured value.
        Returns:
        the RemoteCache implementation.
      • startAsync

        public java.util.concurrent.CompletableFuture<java.lang.Void> startAsync()
      • stopAsync

        public java.util.concurrent.CompletableFuture<java.lang.Void> stopAsync()
      • start

        public void start()
        Description copied from interface: Lifecycle
        Invoked on component start
        Specified by:
        start in interface Lifecycle
      • getMarshallerRegistry

        public org.infinispan.client.hotrod.impl.MarshallerRegistry getMarshallerRegistry()
      • stop

        public void stop()
        Stop the remote cache manager, disconnecting all existing connections. As part of the disconnection, all registered client cache listeners will be removed since client no longer can receive callbacks.
        Specified by:
        stop in interface Lifecycle
      • switchToCluster

        public boolean switchToCluster​(java.lang.String clusterName)
        Description copied from interface: RemoteCacheContainer
        Switch remote cache manager to a different cluster, previously declared via configuration. If the switch was completed successfully, this method returns true, otherwise it returns false.
        Specified by:
        switchToCluster in interface RemoteCacheContainer
        Specified by:
        switchToCluster in interface RemoteCacheManagerMXBean
        Parameters:
        clusterName - name of the cluster to which to switch to
        Returns:
        true if the cluster was switched, false otherwise
      • cacheNameBytes

        public static byte[] cacheNameBytes​(java.lang.String cacheName)
      • cacheNameBytes

        public static byte[] cacheNameBytes()
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • getCodec

        public org.infinispan.client.hotrod.impl.protocol.Codec getCodec()
        This method is not a part of the public API. It is exposed for internal purposes only.
      • getChannelFactory

        public org.infinispan.client.hotrod.impl.transport.netty.ChannelFactory getChannelFactory()
        This method is not a part of the public API. It is exposed for internal purposes only.