Class ClientCacheFactory

  • java.lang.Object
    • org.apache.geode.cache.client.ClientCacheFactory

  • public class ClientCacheFactory
    extends java.lang.Object
    Factory class used to create the singleton client cache and connect to one or more GemFire Cache Servers. If the application wants to connect to GemFire as a peer it should use CacheFactory instead.

    Once the factory has been configured using its set* methods you produce a ClientCache by calling the create() method. The ConfigurationProperties.CACHE_XML_FILE property can be used to specify a cache.xml file to initialize the cache with. The contents of this file must comply with the "doc-files/cache8_0.dtd" file and the top level element must be a client-cache element.

    Client connections are managed through connection pools. ClientCacheFactory creates a single pool to use by default on the cache it creates. ClientCacheFactory can also be used to configure the default connection pool using its setPool* and addPool* methods. In most cases, the defaults used by this implementation will suffice. For the default pool attributes see PoolFactory. If no pool is configured and a pool was not declared in cache.xml or created using PoolManager then a default one will be created that connects to a server on the default cache server port and local host. If multiple pools are declared in cache.xml or created by the PoolFactory then no default pool will exist and ClientRegionFactory.setPoolName will need to be called on each region created.

    To get the existing unclosed singleton client cache instance call getAnyInstance().

    The following examples illustrate bootstrapping the client cache using region shortcuts:

    Example 1: Connect to a CacheServer on the default host and port and access a region "customers"

     ClientCache c = new ClientCacheFactory().create();
     Region r = c.createClientRegionFactory(PROXY).create("customers");
     // The PROXY shortcut tells GemFire to route all requests to the servers
     // . i.e. there is no local caching
     
    Example 2: Connect using the GemFire locator and create a local LRU cache
     ClientCache c = new ClientCacheFactory().addPoolLocator(host, port).create();
     Region r = c.createClientRegionFactory(CACHING_PROXY_HEAP_LRU).create("customers");
     // The local LRU "customers" data region will automatically start evicting, by default, at 80%
     // heap utilization threshold
     
    Example 3: Access the query service
     QueryService qs = new ClientCacheFactory().create().getQueryService();
     
    Example 4: Construct the client cache region declaratively in cache.xml
     <!DOCTYPE client-cache PUBLIC
     "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
     "http://www.gemstone.com/dtd/cache8_0.dtd">
     <client-cache>
     <pool name="myPool">
     <locator host="hostName" port="10334"/>
     </pool>
     <region name="myRegion" refid="PROXY"/>
     <!-- you can override or add to the PROXY attributes by adding
     a region-attributes sub element here -->
     </client-cache>
     
    Now, create the cache telling it to read your cache.xml file:
     ClientCache c = new ClientCacheFactory().set("cache-xml-file", "myCache.xml").create();
     Region r = c.getRegion("myRegion");
     

    For a complete list of all client region shortcuts see ClientRegionShortcut. Applications that need to explicitly control the individual region attributes can do this declaratively in XML or using API.

    Example 5: Define custom region attributes for persistence in XML and create region using API. Define new region attributes with ID "MYAPP_CACHING_PROXY_MEM_LRU" that overrides the "CACHING_PROXY" shortcut

     <!DOCTYPE client-cache PUBLIC
     "-//GemStone Systems, Inc.//GemFire Declarative Caching 8.0//EN"
     "http://www.gemstone.com/dtd/cache8_0.dtd">
     <client-cache>
     <!-- now create a named region attributes that uses the CACHING_PROXY shortcut
     and adds a memory LRU limited to 900 megabytes -->
     <region-attributes id="MYAPP_CACHING_PROXY_MEM_LRU" refid="CACHING_PROXY" >
     <lru-memory-size maximum="900"/>
     </region-attributes>
     </client-cache>
     
    Now, create the data region in the client cache using this new attributes ID.
     ClientCache c = new ClientCacheFactory().set("cache-xml-file", "myCache.xml")
         .addPoolLocator(host, port).create();
     Region r = c.createClientRegionFactory("MYAPP_CACHING_PROXY_MEM_LRU").create("customers");
     
    Since:
    6.5
    • Constructor Detail

      • ClientCacheFactory

        public ClientCacheFactory()
        Creates a new client cache factory.
      • ClientCacheFactory

        public ClientCacheFactory​(java.util.Properties props)
        Create a new client cache factory given the initial gemfire properties.
        Parameters:
        props - The initial gemfire properties to be used. These properties can be overridden using the set(java.lang.String, java.lang.String) method For a full list of valid gemfire properties see ConfigurationProperties.
    • Method Detail

      • set

        public ClientCacheFactory set​(java.lang.String name,
                                      java.lang.String value)
        Sets a gemfire property that will be used when creating the ClientCache. For a full list of valid gemfire properties see ConfigurationProperties.
        Parameters:
        name - the name of the gemfire property
        value - the value of the gemfire property
        Returns:
        a reference to this ClientCacheFactory object
      • create

        public ClientCache create()
        Create a singleton client cache. If a client cache already exists in this vm that is not compatible with this factory's configuration then create will fail.

        While creating the cache instance any declarative cache configuration (cache.xml) is processed and used to initialize the created cache.

        Note that the cache that is produced is a singleton. Before a different instance can be produced the old one must be closed.

        Returns:
        the singleton client cache
        Throws:
        CacheXmlException - If a problem occurs while parsing the declarative caching XML file.
        TimeoutException - If a Region.put(Object, Object) times out while initializing the cache.
        CacheWriterException - If a CacheWriterException is thrown while initializing the cache.
        RegionExistsException - If the declarative caching XML file describes a region that already exists (including the root region).
        java.lang.IllegalStateException - if a client cache already exists and it is not compatible with this factory's configuration.
        java.lang.IllegalStateException - if mcast-port or locator is set on client cache.
        AuthenticationFailedException - if authentication fails.
        AuthenticationRequiredException - if server is in secure mode and client cache is not configured with security credentials.
      • setPoolSocketConnectTimeout

        public ClientCacheFactory setPoolSocketConnectTimeout​(int socketConnectTimeout)
        Sets the socket connect timeout for this pool. The number of milli seconds specified as socket timeout when the client connects to the servers/locators. A timeout of zero is interpreted as an infinite timeout. The connection will then block until established or an error occurs.
        Parameters:
        socketConnectTimeout - timeout in milliseconds when the client connects to the servers
        Returns:
        a reference to this
        Throws:
        java.lang.IllegalArgumentException - if socketConnectTimeout is less than or equal to -1.
      • setPoolFreeConnectionTimeout

        public ClientCacheFactory setPoolFreeConnectionTimeout​(int connectionTimeout)
        Sets the free connection timeout for this pool. If the pool has a max connections setting, operations will block if all of the connections are in use. The free connection timeout specifies how long those operations will block waiting for a free connection before receiving an AllConnectionsInUseException. If max connections is not set this setting has no effect.
        Parameters:
        connectionTimeout - the connection timeout in milliseconds
        Returns:
        a reference to this
        Throws:
        java.lang.IllegalArgumentException - if connectionTimeout is less than or equal to 0.
        See Also:
        setPoolMaxConnections(int)
      • setPoolServerConnectionTimeout

        public ClientCacheFactory setPoolServerConnectionTimeout​(int connectionTimeout)
        Sets the server connection timeout for this pool. If the pool has a max connections setting, operations will block if there is no free connections toward designated server. The server connection timeout specifies how long those operations will block waiting for a connection toward server before receiving an AllConnectionsInUseException. If max connections is not set this setting has no effect.
        Parameters:
        connectionTimeout - the connection timeout in milliseconds
        Returns:
        a reference to this
        Throws:
        java.lang.IllegalArgumentException - if connectionTimeout is less than 0.
        See Also:
        setPoolMaxConnections(int)
      • setPoolLoadConditioningInterval

        public ClientCacheFactory setPoolLoadConditioningInterval​(int loadConditioningInterval)
        Sets the load conditioning interval for this pool. This interval controls how frequently the pool will check to see if a connection to a given server should be moved to a different server to improve the load balance.

        A value of -1 disables load conditioning

        Parameters:
        loadConditioningInterval - the connection lifetime in milliseconds
        Returns:
        a reference to this
        Throws:
        java.lang.IllegalArgumentException - if connectionLifetime is less than -1.
      • setPoolSocketBufferSize

        public ClientCacheFactory setPoolSocketBufferSize​(int bufferSize)
        Sets the socket buffer size for each connection made in this pool. Large messages can be received and sent faster when this buffer is larger. Larger buffers also optimize the rate at which servers can send events for client subscriptions.
        Parameters:
        bufferSize - the size of the socket buffers used for reading and writing on each connection in this pool.
        Returns:
        a reference to this
        Throws:
        java.lang.IllegalArgumentException - if bufferSize is less than or equal to 0.
      • setPoolThreadLocalConnections

        @Deprecated
        public ClientCacheFactory setPoolThreadLocalConnections​(boolean threadLocalConnections)
        Deprecated.
        Since Geode 1.10.0. Thread local connections are ignored. Will be removed in future major release.
        Sets the thread local connections policy for this pool. If true then any time a thread goes to use a connection from this pool it will check a thread local cache and see if it already has a connection in it. If so it will use it. If not it will get one from this pool and cache it in the thread local. This gets rid of thread contention for the connections but increases the number of connections the servers see.

        If false then connections are returned to the pool as soon as the operation being done with the connection completes. This allows connections to be shared amonst multiple threads keeping the number of connections down.

        Parameters:
        threadLocalConnections - if true then enable thread local connections.
        Returns:
        a reference to this
      • setPoolReadTimeout

        public ClientCacheFactory setPoolReadTimeout​(int timeout)
        Sets the number of milliseconds to wait for a response from a server before timing out the operation and trying another server (if any are available).
        Parameters:
        timeout - number of milliseconds to wait for a response from a server
        Returns:
        a reference to this
        Throws:
        java.lang.IllegalArgumentException - if timeout is less than 0.
      • setPoolMinConnections

        public ClientCacheFactory setPoolMinConnections​(int minConnections)
        Set the minimum number of connections to keep available at all times. When the pool is created, it will create this many connections. If 0 then connections will not be made until an actual operation is done that requires client-to-server communication.
        Parameters:
        minConnections - the initial number of connections this pool will create.
        Returns:
        a reference to this
        Throws:
        java.lang.IllegalArgumentException - if minConnections is less than 0, or larger than maxConnections or min/maxConnectionsPerServer is set to non-defaults
        See Also:
        setPoolMaxConnectionsPerServer(int), setPoolMinConnectionsPerServer(int)
      • setPoolMinConnectionsPerServer

        public ClientCacheFactory setPoolMinConnectionsPerServer​(int minConnections)
        Set the minimum number of connections to keep available to one server at all times. When the pool is created it will create this many connections to each server. If 0 then connections to a server will not be made until an operation is done that requires a connection to a server.
        Parameters:
        minConnections - the initial number of connections this pool will create per server.
        Returns:
        a reference to this
        Throws:
        java.lang.IllegalArgumentException - if minConnections is less than 0. or larger than maxConnectionsPerServer or min/maxConnections is set to non-defaults
        Since:
        10.1
        See Also:
        setPoolMaxConnections(int), setPoolMinConnections(int)
      • setPoolMaxConnections

        public ClientCacheFactory setPoolMaxConnections​(int maxConnections)
        Set the max number of client to server connections that the pool will create. If all of the connections are in use, an operation requiring a client to server connection will block until a connection is available.
        Parameters:
        maxConnections - the maximum number of connections in the pool. this pool will create. -1 indicates that there is no maximum number of connections
        Returns:
        a reference to this
        Throws:
        java.lang.IllegalArgumentException - if maxConnections is less than -1 or equal to 0 or less than minConnections or min/maxConnectionsPerServer is set to non-defaults
        See Also:
        setPoolMaxConnectionsPerServer(int), setPoolMinConnectionsPerServer(int), setPoolFreeConnectionTimeout(int), setPoolServerConnectionTimeout(int)
      • setPoolMaxConnectionsPerServer

        public ClientCacheFactory setPoolMaxConnectionsPerServer​(int maxConnections)
        Set the maximum number of client connections to one server connections that the pool will create. If the maximum number of connections exist, and they are all in use, an operation requiring a client to server connection will block until a connection is available.
        Parameters:
        maxConnections - the maximum number of connections to a server in the pool. this pool will create. -1 indicates that there is no maximum number of connections
        Returns:
        a reference to this
        Throws:
        java.lang.IllegalArgumentException - if maxConnections is less than -1 or equal to 0 or less than minConnectionsPerServer or min/maxConnections is set to non-defaults
        Since:
        10.1
        See Also:
        setPoolMaxConnections(int), setPoolMinConnections(int)
      • setPoolIdleTimeout

        public ClientCacheFactory setPoolIdleTimeout​(long idleTimeout)
        Set the amount of time a connection can be idle before expiring the connection. If the pool size is greater than the minimum specified by setPoolMinConnections(int), connections which have been idle for longer than the idleTimeout will be closed.
        Parameters:
        idleTimeout - The amount of time in milliseconds that an idle connection should live before expiring. -1 indicates that connections should never expire.
        Returns:
        a reference to this
        Throws:
        java.lang.IllegalArgumentException - if idleTimout is less than -1.
      • setPoolRetryAttempts

        public ClientCacheFactory setPoolRetryAttempts​(int retryAttempts)
        Set the number of times to retry a request after timeout/exception.
        Parameters:
        retryAttempts - The number of times to retry a request after timeout/exception. -1 indicates that a request should be tried against every available server before failing
        Returns:
        a reference to this
        Throws:
        java.lang.IllegalArgumentException - if idleTimout is less than -1.
      • setPoolPingInterval

        public ClientCacheFactory setPoolPingInterval​(long pingInterval)
        How often to ping servers to verify that they are still alive. Each server will be sent a ping every pingInterval if there has not been any other communication with the server.

        These pings are used by the server to monitor the health of the client. Make sure that the pingInterval is less than the maximum time between pings allowed by the cache server.

        Parameters:
        pingInterval - The amount of time in milliseconds between pings.
        Returns:
        a reference to this
        Throws:
        java.lang.IllegalArgumentException - if pingInterval is less than or equal to 0.
        See Also:
        CacheServer.setMaximumTimeBetweenPings(int)
      • setPoolStatisticInterval

        public ClientCacheFactory setPoolStatisticInterval​(int statisticInterval)
        How often to send client statistics to the server. Doing this allows gfmon to monitor clients.

        A value of -1 disables the sending of client statistics to the server.

        Parameters:
        statisticInterval - The amount of time in milliseconds between sends of client statistics to the server.
        Returns:
        a reference to this
        Throws:
        java.lang.IllegalArgumentException - if statisticInterval is less than -1.
      • setPoolServerGroup

        public ClientCacheFactory setPoolServerGroup​(java.lang.String group)
        Configures the group that all servers this pool connects to must belong to.
        Parameters:
        group - the server group that this pool will connect to. If null or "" then all servers will be connected to.
        Returns:
        a reference to this
      • addPoolLocator

        public ClientCacheFactory addPoolLocator​(java.lang.String host,
                                                 int port)
        Add a locator, given its host and port, to this factory. The locator must be a server locator and will be used to discover other running cache servers and locators. Note that if the host is unknown at the time of this call the locator will still be added. When the pool is used for an operation if the host is still unknown an exception will be thrown.
        Parameters:
        host - the host name or ip address that the locator is listening on.
        port - the port that the locator is listening on
        Returns:
        a reference to this
        Throws:
        java.lang.IllegalArgumentException - if port is outside the valid range of [0..65535] inclusive.
        java.lang.IllegalStateException - if a server has already been added to this factory.
      • addPoolServer

        public ClientCacheFactory addPoolServer​(java.lang.String host,
                                                int port)
        Add a server, given its host and port, to this factory. The server must be a cache server and this client will directly connect to without consulting a server locator. Note that if the host is unknown at the time of this call the server will still be added. When the pool is used for an operation if the host is still unknown an exception will be thrown.
        Parameters:
        host - the host name or ip address that the server is listening on.
        port - the port that the server is listening on
        Returns:
        a reference to this
        Throws:
        java.lang.IllegalArgumentException - if port is outside the valid range of [0..65535] inclusive.
        java.lang.IllegalStateException - if a locator has already been added to this factory.
      • setPoolSubscriptionEnabled

        public ClientCacheFactory setPoolSubscriptionEnabled​(boolean enabled)
        If set to true then the created pool will have server-to-client subscriptions enabled. If set to false then all Subscription* attributes are ignored at create time.
        Parameters:
        enabled - whether the created pool will have server-to-client subscriptions enabled
        Returns:
        a reference to this
      • setPoolSubscriptionRedundancy

        public ClientCacheFactory setPoolSubscriptionRedundancy​(int redundancy)
        Sets the redundancy level for this pools server-to-client subscriptions. If 0 then no redundant copies will be kept on the servers. Otherwise an effort will be made to maintain the requested number of copies of the server-to-client subscriptions. At most one copy per server will be made up to the requested level.
        Parameters:
        redundancy - the number of redundant servers for this client's subscriptions.
        Returns:
        a reference to this
        Throws:
        java.lang.IllegalArgumentException - if redundancyLevel is less than -1.
      • setPoolSubscriptionTimeoutMultiplier

        public ClientCacheFactory setPoolSubscriptionTimeoutMultiplier​(int multiplier)
        A server has an inactivity monitor that ensures a message is sent to a client at least once a minute (60,000 milliseconds). If a subscription timeout multipler is set in the client it enables timing out of the subscription feed with failover to another server.

        The client will time out it's subscription connection after a number of seconds equal to this multiplier times the server's subscription-timeout.

        Set this to 2 or more to make sure the client will receive pings from the server before the timeout.

        A value of zero (the default) disables timeouts

        The resulting timeout will be multiplied by 1.25 in order to avoid race conditions with the server sending its "ping" message.

        Parameters:
        multiplier - the subscription timeout multiplier to set
        Returns:
        a reference to this
      • setPoolSubscriptionMessageTrackingTimeout

        public ClientCacheFactory setPoolSubscriptionMessageTrackingTimeout​(int messageTrackingTimeout)
        Sets the messageTrackingTimeout attribute which is the time-to-live period, in milliseconds, for subscription events the client has received from the server. It's used to minimize duplicate events. Entries that have not been modified for this amount of time are expired from the list
        Parameters:
        messageTrackingTimeout - number of milliseconds to set the timeout to.
        Returns:
        a reference to this
        Throws:
        java.lang.IllegalArgumentException - if messageTrackingTimeout is less than or equal to 0.
      • setPoolSocketFactory

        public ClientCacheFactory setPoolSocketFactory​(SocketFactory socketFactory)
        Set the socket factory used by this pool to create connections to both locators (if configured using addPoolLocator(String, int) (String, int)}) and servers. Sockets returned by this factory will have the rest of the configuration options specified on this pool and on the ClientCache applied to them. In particular, sockets returned by this factory will be wrapped with SSLSockets if ssl is enabled for this client cache. This factory can be used for configuring a proxy, or overriding various socket settings. For modifying SSL settings, see SSLParameterExtension See ProxySocketFactories
        Parameters:
        socketFactory - The SocketFactory to use
        Returns:
        a reference to this
        Since:
        Geode 1.13
        See Also:
        PoolFactory.setSocketFactory(SocketFactory)
      • setPoolSubscriptionAckInterval

        public ClientCacheFactory setPoolSubscriptionAckInterval​(int ackInterval)
        Sets the interval in milliseconds to wait before sending acknowledgements to the cache server for events received from the server subscriptions.
        Parameters:
        ackInterval - number of milliseconds to wait before sending event acknowledgements.
        Returns:
        a reference to this
        Throws:
        java.lang.IllegalArgumentException - if ackInterval is less than or equal to 0.
      • setPoolPRSingleHopEnabled

        public ClientCacheFactory setPoolPRSingleHopEnabled​(boolean enabled)
        By default setPRSingleHopEnabled is true in which case the client is aware of the location of partitions on servers hosting regions with DataPolicy.PARTITION. Using this information, the client routes the client cache operations directly to the server which is hosting the required partition for the cache operation using a single network hop. This mode works best when setPoolMaxConnections(int) is set to -1 which is the default. This mode causes the client to have more connections to the servers.

        If setPRSingleHopEnabled is false the client may need to do an extra network hop on servers to go to the required partition for that cache operation. The client will use fewer network connections to the servers.

        Caution: for partition regions with local-max-memory equal to zero, no cache operations mentioned above will be routed to those servers as they do not host any partitions.

        Parameters:
        enabled - whether Partition Region single hop is enabled
        Returns:
        the newly created pool.
      • setPoolMultiuserAuthentication

        public ClientCacheFactory setPoolMultiuserAuthentication​(boolean enabled)
        If set to true then the created pool can be used by multiple users.

        Note: If set to true, all the client side regions must be proxies. No client side storage is allowed.
        Parameters:
        enabled - whether the created pool can be used by multiple users
        Returns:
        a reference to this
      • getVersion

        public static java.lang.String getVersion()
        Returns the version of the cache implementation.
        Returns:
        the version of the cache implementation as a String
      • getAnyInstance

        public static ClientCache getAnyInstance()
        Gets an arbitrary open instance of ClientCache produced by an earlier call to create().
        Returns:
        an arbitrary open instance of ClientCache produced by an earlier call to create()
        Throws:
        CacheClosedException - if a cache has not been created or the only created one is closed
        java.lang.IllegalStateException - if the cache was created by CacheFactory instead of ClientCacheFactory
      • setPdxReadSerialized

        public ClientCacheFactory setPdxReadSerialized​(boolean pdxReadSerialized)
        Sets the object preference to PdxInstance type. When a cached object that was serialized as a PDX is read from the cache a PdxInstance will be returned instead of the actual domain class. The PdxInstance is an interface that provides run time access to the fields of a PDX without deserializing the entire PDX. The PdxInstance implementation is a light weight wrapper that simply refers to the raw bytes of the PDX that are kept in the cache. Using this method applications can choose to access PdxInstance instead of Java object.

        Note that a PdxInstance is only returned if a serialized PDX is found in the cache. If the cache contains a deserialized PDX, then a domain class instance is returned instead of a PdxInstance.

        Parameters:
        pdxReadSerialized - true to prefer PdxInstance
        Returns:
        this ClientCacheFactory
        Since:
        GemFire 6.6
        See Also:
        PdxInstance
      • setPdxSerializer

        public ClientCacheFactory setPdxSerializer​(PdxSerializer serializer)
        Set the PDX serializer for the cache. If this serializer is set, it will be consulted to see if it can serialize any domain classes which are added to the cache in portable data exchange format.
        Parameters:
        serializer - the serializer to use
        Returns:
        this ClientCacheFactory
        Since:
        GemFire 6.6
        See Also:
        PdxSerializer
      • setPdxDiskStore

        @Deprecated
        public ClientCacheFactory setPdxDiskStore​(java.lang.String diskStoreName)
        Deprecated.
        Pdx Persistence is not supported on client side. Even when set, it's internally ignored.
        Set the disk store that is used for PDX meta data. When serializing objects in the PDX format, the type definitions are persisted to disk. This setting controls which disk store is used for that persistence.

        If not set, the metadata will go in the default disk store.

        Parameters:
        diskStoreName - the name of the disk store to use for the PDX metadata.
        Returns:
        this ClientCacheFactory
        Since:
        GemFire 6.6
      • setPdxPersistent

        @Deprecated
        public ClientCacheFactory setPdxPersistent​(boolean isPersistent)
        Deprecated.
        Pdx Persistence is not supported on client side. Even when set, it's internally ignored.
        Control whether the type metadata for PDX objects is persisted to disk. The default for this setting is false. If you are using persistent regions with PDX then you must set this to true. If you are using a WAN gateway with PDX then you should set this to true.
        Parameters:
        isPersistent - true if the metadata should be persistent
        Returns:
        this ClientCacheFactory
        Since:
        GemFire 6.6
      • setPdxIgnoreUnreadFields

        public ClientCacheFactory setPdxIgnoreUnreadFields​(boolean ignore)
        Control whether pdx ignores fields that were unread during deserialization. The default is to preserve unread fields be including their data during serialization. But if you configure the cache to ignore unread fields then their data will be lost during serialization.

        You should only set this attribute to true if you know this member will only be reading cache data. In this use case you do not need to pay the cost of preserving the unread fields since you will never be reserializing pdx data.

        Parameters:
        ignore - true if fields not read during pdx deserialization should be ignored; false, the default, if they should be preserved.
        Returns:
        this ClientCacheFactory
        Since:
        GemFire 6.6