Class AbstractBasicCacheFactoryBean

java.lang.Object
org.springframework.data.gemfire.support.AbstractFactoryBeanSupport<org.apache.geode.cache.GemFireCache>
org.springframework.data.gemfire.AbstractBasicCacheFactoryBean
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.FactoryBean<org.apache.geode.cache.GemFireCache>, org.springframework.beans.factory.InitializingBean, org.springframework.context.Phased, org.springframework.dao.support.PersistenceExceptionTranslator
Direct Known Subclasses:
AbstractConfigurableCacheFactoryBean

public abstract class AbstractBasicCacheFactoryBean extends AbstractFactoryBeanSupport<org.apache.geode.cache.GemFireCache> implements org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, org.springframework.dao.support.PersistenceExceptionTranslator, org.springframework.context.Phased
Abstract base class for CacheFactoryBean and ClientCacheFactoryBean classes, used to create Apache Geode peer Cache and ClientCache instances, respectively. This class implements Spring's PersistenceExceptionTranslator interface and is auto-detected by Spring's PersistenceExceptionTranslationPostProcessor to enable AOP-based translation of native Apache Geode RuntimeExceptions to Spring's DataAccessException hierarchy. Therefore, the presence of this class automatically enables Spring's PersistenceExceptionTranslationPostProcessor to translate native Apache Geode thrown GemFireException and GemFireCheckedException types as Spring DataAccessExceptions. In addition, this class also assumes the responsibility of positioning the creation of the cache in the appropriate phase of the Spring container's lifecycle, providing default callbacks for both initialization and destruction. More importantly, this abstract class encapsulates configuration applicable to tuning Apache Geode in order to efficiently use JVM Heap memory. Since Apache Geode stores data in-memory, on the JVM Heap, it is important that Apache Geode be tuned to monitor the JVM Heap and respond to memory pressure accordingly, by evicting data and issuing warnings when the JVM Heap reaches critical mass. This abstract class is also concerned with the configuration of PDX and transaction event handling along with whether the contents (entries) of the cache should be made effectively immutable on reads (i.e. get(key)). In summary, this abstract class primarily handles and encapsulates the configuration of the following concerns:
  • copy-on-read semantics
  • JVM Heap memory management
  • PDX serialization
  • Transaction event processing
All of these concerns are applicable to both Apache Geode ClientCache and peer Cache instances.
Since:
2.5.0
See Also:
  • Constructor Details

    • AbstractBasicCacheFactoryBean

      public AbstractBasicCacheFactoryBean()
  • Method Details

    • setCache

      protected void setCache(@Nullable org.apache.geode.cache.GemFireCache cache)
      Sets a reference to the constructed, configured an initialized GemFireCache instance created by this cache FactoryBean.
      Parameters:
      cache - GemFireCache created by this cache FactoryBean.
      See Also:
      • GemFireCache
    • getCache

      @Nullable public <T extends org.apache.geode.cache.GemFireCache> T getCache()
      Returns a reference to the constructed, configured an initialized GemFireCache instance created by this cache FactoryBean.
      Type Parameters:
      T - parameterized Class type extending GemFireCache.
      Returns:
      a reference to the GemFireCache created by this cache FactoryBean.
      See Also:
      • GemFireCache
    • getOptionalCache

      public <T extends org.apache.geode.cache.GemFireCache> Optional<T> getOptionalCache()
      Returns an Optional reference to the constructed, configured and initialized GemFireCache instance created by this cache FactoryBean.
      Type Parameters:
      T - parameterized Class type extending GemFireCache.
      Returns:
      an Optional reference to the GemFireCache created by this {cache @link FactoryBean}.
      See Also:
    • setCacheFactoryInitializer

      public void setCacheFactoryInitializer(@Nullable AbstractBasicCacheFactoryBean.CacheFactoryInitializer cacheFactoryInitializer)
      Set the AbstractBasicCacheFactoryBean.CacheFactoryInitializer called by this FactoryBean to initialize the Apache Geode cache factory used to create the cache constructed by this FactoryBean.
      Parameters:
      cacheFactoryInitializer - AbstractBasicCacheFactoryBean.CacheFactoryInitializer called to initialize the cache factory.
      See Also:
    • getCacheFactoryInitializer

      @Nullable public AbstractBasicCacheFactoryBean.CacheFactoryInitializer getCacheFactoryInitializer()
      Return the AbstractBasicCacheFactoryBean.CacheFactoryInitializer called by this FactoryBean to initialize the Apache Geode cache factory used to create the cache constructed by this FactoryBean.
      Returns:
      the AbstractBasicCacheFactoryBean.CacheFactoryInitializer called to initialize the cache factory.
      See Also:
    • setClose

      public void setClose(boolean close)
      Sets a boolean value used to determine whether the cache should be closed on shutdown of the Spring container.
      Parameters:
      close - boolean value used to determine whether the cache will be closed on shutdown of the Spring container.
    • isClose

      public boolean isClose()
      Returns a boolean value used to determine whether the cache will be closed on shutdown of the Spring container. Defaults to true.
      Returns:
      a boolean value used to determine whether the cache will be closed on shutdown of the Spring container.
    • setCopyOnRead

      public void setCopyOnRead(@Nullable Boolean copyOnRead)
      Sets the GemFireCache.getCopyOnRead() property of the GemFireCache.
      Parameters:
      copyOnRead - a Boolean value to indicate whether objects stored in the GemFireCache are copied on read (i.e. Region.get(Object).
    • getCopyOnRead

      @Nullable public Boolean getCopyOnRead()
      Returns the configuration of the GemFireCache.getCopyOnRead() property set on the GemFireCache.
      Returns:
      a Boolean value to indicate whether objects stored in the GemFireCache are copied on read (i.e. Region.get(Object).
    • isCopyOnRead

      public boolean isCopyOnRead()
      Determines whether objects stored in the GemFireCache are copied when read (i.e. Region.get(Object). Defaults to false.
      Returns:
      a boolean value indicating whether objects stored in the GemFireCache are copied when read (i.e. Region.get(Object).
      See Also:
    • setCriticalHeapPercentage

      public void setCriticalHeapPercentage(@Nullable Float criticalHeapPercentage)
      Set the GemFireCache critical heap percentage property.
      Parameters:
      criticalHeapPercentage - Float value specifying the configuration for the GemFireCache critical heap percentage.
    • getCriticalHeapPercentage

      public Float getCriticalHeapPercentage()
      Gets the configuration of the GemFireCache critical heap percentage property.
      Returns:
      a Float value specifying the configuration for the GemFireCache critical heap percentage.
    • setCriticalOffHeapPercentage

      public void setCriticalOffHeapPercentage(@Nullable Float criticalOffHeapPercentage)
      Set the GemFireCache critical off-heap percentage property.
      Parameters:
      criticalOffHeapPercentage - Float value specifying the configuration for the GemFireCache critical off-heap percentage.
    • getCriticalOffHeapPercentage

      public Float getCriticalOffHeapPercentage()
      Gets the configuration of the GemFireCache critical off-heap percentage property.
      Returns:
      a Float value specifying the configuration for the GemFireCache critical off-heap percentage.
    • setEvictionHeapPercentage

      public void setEvictionHeapPercentage(Float evictionHeapPercentage)
      Set the GemFireCache eviction heap percentage property.
      Parameters:
      evictionHeapPercentage - Float value specifying the configuration for the GemFireCache eviction heap percentage.
    • getEvictionHeapPercentage

      public Float getEvictionHeapPercentage()
      Gets the configuration of the GemFireCache eviction heap percentage property.
      Returns:
      a Float value specifying the configuration for the GemFireCache eviction heap percentage.
    • setEvictionOffHeapPercentage

      public void setEvictionOffHeapPercentage(Float evictionOffHeapPercentage)
      Set the GemFireCache eviction off-heap percentage property.
      Parameters:
      evictionOffHeapPercentage - Float value specifying the configuration for the GemFireCache eviction off-heap percentage.
    • getEvictionOffHeapPercentage

      public Float getEvictionOffHeapPercentage()
      Gets the configuration of the GemFireCache eviction off-heap percentage property.
      Returns:
      a Float value specifying the configuration for the GemFireCache eviction off-heap percentage.
    • getObject

      public org.apache.geode.cache.GemFireCache getObject() throws Exception
      Returns the cache object reference created by this cache FactoryBean.
      Specified by:
      getObject in interface org.springframework.beans.factory.FactoryBean<org.apache.geode.cache.GemFireCache>
      Returns:
      the cache object reference created by this cache FactoryBean.
      Throws:
      Exception
      See Also:
    • doGetObject

      protected abstract org.apache.geode.cache.GemFireCache doGetObject()
      Called if getCache() returns a null GemFireCache reference from getObject().
      Returns:
      a new constructed, configured and initialized GemFireCache instance.
      See Also:
    • getObjectType

      public Class<? extends org.apache.geode.cache.GemFireCache> getObjectType()
      Returns the type of GemFireCache created by this cache FactoryBean.
      Specified by:
      getObjectType in interface org.springframework.beans.factory.FactoryBean<org.apache.geode.cache.GemFireCache>
      Returns:
      the type of GemFireCache created by this cache FactoryBean.
      See Also:
    • doGetObjectType

      protected Class<? extends org.apache.geode.cache.GemFireCache> doGetObjectType()
      By default, returns GemFireCache Class.
      Returns:
      GemFireCache Class by default.
      See Also:
    • setPdxDiskStoreName

      public void setPdxDiskStoreName(@Nullable String pdxDiskStoreName)
      Sets the name of the Apache Geode DiskStore used to store PDX metadata.
      Parameters:
      pdxDiskStoreName - name for the PDX DiskStore.
      See Also:
      • CacheFactory.setPdxDiskStore(String)
      • DiskStore.getName()
    • getPdxDiskStoreName

      @Nullable public String getPdxDiskStoreName()
      Gets the name of the Apache Geode DiskStore used to store PDX metadata.
      Returns:
      the name of the PDX DiskStore.
      See Also:
      • GemFireCache.getPdxDiskStore()
      • DiskStore.getName()
    • setPdxIgnoreUnreadFields

      public void setPdxIgnoreUnreadFields(@Nullable Boolean pdxIgnoreUnreadFields)
      Configures whether PDX will ignore unread fields when deserializing PDX bytes back to an Object. Defaults to false.
      Parameters:
      pdxIgnoreUnreadFields - Boolean value controlling ignoring unread fields.
      See Also:
      • CacheFactory.setPdxIgnoreUnreadFields(boolean)
    • getPdxIgnoreUnreadFields

      @Nullable public Boolean getPdxIgnoreUnreadFields()
      Gets the configuration determining whether PDX will ignore unread fields when deserializing PDX bytes back to an Object. Defaults to false.
      Returns:
      a Boolean value controlling ignoring unread fields.
      See Also:
      • GemFireCache.getPdxIgnoreUnreadFields()
    • setPdxPersistent

      public void setPdxPersistent(@Nullable Boolean pdxPersistent)
      Configures whether type metadata for objects serialized to PDX will be persisted to disk.
      Parameters:
      pdxPersistent - Boolean value controlling whether PDX type metadata will be persisted to disk.
      See Also:
      • CacheFactory.setPdxPersistent(boolean)
    • getPdxPersistent

      @Nullable public Boolean getPdxPersistent()
      Gets the configuration determining whether type metadata for objects serialized to PDX will be persisted to disk.
      Returns:
      a Boolean value controlling whether PDX type metadata will be persisted to disk.
      See Also:
      • GemFireCache.getPdxPersistent()
    • setPdxReadSerialized

      public void setPdxReadSerialized(@Nullable Boolean pdxReadSerialized)
      Configures whether objects stored in the Apache Geode cache as PDX will be read back as PDX bytes or (deserialized) as an Object when Region.get(Object) is called.
      Parameters:
      pdxReadSerialized - Boolean value controlling the PDX read serialized function.
      See Also:
      • CacheFactory.setPdxReadSerialized(boolean)
    • getPdxReadSerialized

      @Nullable public Boolean getPdxReadSerialized()
      Gets the configuration determining whether objects stored in the Apache Geode cache as PDX will be read back as PDX bytes or (deserialized) as an Object when Region.get(Object) is called.
      Returns:
      a Boolean value controlling the PDX read serialized function.
      See Also:
      • GemFireCache.getPdxReadSerialized()
    • setPdxSerializer

      public void setPdxSerializer(@Nullable org.apache.geode.pdx.PdxSerializer serializer)
      Configures a reference to PdxSerializer used by this cache to de/serialize objects stored in the cache and distributed/transferred across the distributed system as PDX bytes.
      Parameters:
      serializer - PdxSerializer used by this cache to de/serialize objects as PDX.
      See Also:
      • CacheFactory.setPdxSerializer(PdxSerializer)
      • PdxSerializer
    • getPdxSerializer

      @Nullable public org.apache.geode.pdx.PdxSerializer getPdxSerializer()
      Get a reference to the configured PdxSerializer used by this cache to de/serialize objects stored in the cache and distributed/transferred across the distributed system as PDX bytes.
      Returns:
      a reference to the configured PdxSerializer.
      See Also:
      • GemFireCache.getPdxSerializer()
      • PdxSerializer
    • setPhase

      protected void setPhase(int phase)
      Set the lifecycle phase for this cache bean in the Spring container.
      Parameters:
      phase - Integer.TYPE value specifying the lifecycle phase for this cache bean in the Spring container.
      See Also:
      • Phased.getPhase()
    • getPhase

      public int getPhase()
      Returns the configured lifecycle phase for this cache bean in the Spring container.
      Specified by:
      getPhase in interface org.springframework.context.Phased
      Returns:
      an Integer.TYPE used specifying the lifecycle phase for this cache bean in the Spring container.
      See Also:
      • Phased.getPhase()
    • setTransactionListeners

      public void setTransactionListeners(List<org.apache.geode.cache.TransactionListener> transactionListeners)
      Configures the cache (transaction manager) with a List of TransactionListeners implemented by applications to listen for and receive transaction events after a transaction is processed (i.e. committed or rolled back).
      Parameters:
      transactionListeners - List of application-defined TransactionListeners registered with the cache to listen for and receive transaction events.
      See Also:
      • TransactionListener
    • getTransactionListeners

      public List<org.apache.geode.cache.TransactionListener> getTransactionListeners()
      Returns the List of configured, application-defined TransactionListeners registered with the cache (transaction manager) to enable applications to receive transaction events after a transaction is processed (i.e. committed or rolled back).
      Returns:
      a List of application-defined TransactionListeners registered with the cache (transaction manager) to listen for and receive transaction events.
      See Also:
      • TransactionListener
    • setTransactionWriter

      public void setTransactionWriter(@Nullable org.apache.geode.cache.TransactionWriter transactionWriter)
      Configures a TransactionWriter implemented by the application to receive transaction events and perform a action, like a veto.
      Parameters:
      transactionWriter - TransactionWriter receiving transaction events.
      See Also:
      • TransactionWriter
    • getTransactionWriter

      @Nullable public org.apache.geode.cache.TransactionWriter getTransactionWriter()
      Return the configured TransactionWriter used to process and handle transaction events.
      Returns:
      the configured TransactionWriter.
      See Also:
      • TransactionWriter
    • afterPropertiesSet

      public void afterPropertiesSet() throws Exception
      Initializes this cache FactoryBean after all properties for this cache bean have been set by the Spring container.
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
      Throws:
      Exception - if initialization fails.
      See Also:
    • applyCacheConfigurers

      protected abstract void applyCacheConfigurers()
      Applies any user-defined cache configurers (e.g. ClientCacheConfigurer or PeerCacheConfigurer) to this cache FactoryBean before cache construction, configuration and initialization.
    • close

      protected void close(@Nullable org.apache.geode.cache.GemFireCache cache)
      Null-safe method used to close the GemFireCache by calling RegionService.close() iff the cache is not already closed.
      Parameters:
      cache - GemFireCache to close.
      See Also:
    • isNotClosed

      protected boolean isNotClosed(@Nullable org.apache.geode.cache.GemFireCache cache)
      Determines if the GemFireCache has not been closed yet.
      Parameters:
      cache - GemFireCache to evaluate.
      Returns:
      a boolean value indicating if the GemFireCache is not yet closed.
      See Also:
      • GemFireCache
    • destroy

      public void destroy()
      Destroys the cache bean on Spring container shutdown.
      Specified by:
      destroy in interface org.springframework.beans.factory.DisposableBean
      See Also:
    • configureHeapPercentages

      @NonNull protected org.apache.geode.cache.GemFireCache configureHeapPercentages(@NonNull org.apache.geode.cache.GemFireCache cache)
      Configures the GemFireCache critical and eviction heap thresholds as percentages.
      Parameters:
      cache - GemFireCache to configure the critical and eviction heap thresholds; must not be null.
      Returns:
      the given GemFireCache.
      Throws:
      IllegalArgumentException - if the critical or eviction heap thresholds are not valid percentages.
      See Also:
      • ResourceManager.setCriticalHeapPercentage(float)
      • ResourceManager.setEvictionHeapPercentage(float)
      • ResourceManager
      • GemFireCache.getResourceManager()
      • GemFireCache
    • configureOffHeapPercentages

      @NonNull protected org.apache.geode.cache.GemFireCache configureOffHeapPercentages(@NonNull org.apache.geode.cache.GemFireCache cache)
      Configures the GemFireCache critical and eviction off-heap thresholds as percentages.
      Parameters:
      cache - GemFireCache to configure the critical and eviction off-heap thresholds; must not be null.
      Returns:
      the given GemFireCache.
      Throws:
      IllegalArgumentException - if the critical or eviction off-heap thresholds are not valid percentages.
      See Also:
      • ResourceManager.setCriticalOffHeapPercentage(float)
      • ResourceManager.setEvictionOffHeapPercentage(float)
      • ResourceManager
      • GemFireCache.getResourceManager()
      • GemFireCache
    • configurePdx

      protected <T> T configurePdx(AbstractBasicCacheFactoryBean.PdxConfigurer<T> pdxConfigurer)
      Configures the cache to use PDX serialization.
      Parameters:
      pdxConfigurer - AbstractBasicCacheFactoryBean.PdxConfigurer used to configure the cache with PDX serialization.
      Returns:
      the AbstractBasicCacheFactoryBean.PdxConfigurer.getTarget().
    • fetchCache

      protected <T extends org.apache.geode.cache.GemFireCache> T fetchCache()
      Fetches an existing cache instance from the Apache Geode cache factory.
      Type Parameters:
      T - parameterized Class type extending GemFireCache.
      Returns:
      an existing cache instance if available.
      Throws:
      org.apache.geode.cache.CacheClosedException - if an existing cache instance does not exist.
      See Also:
    • doFetchCache

      protected abstract <T extends org.apache.geode.cache.GemFireCache> T doFetchCache()
      Called by fetchCache() if the GemFireCache reference returned by getCache() is null. This method is typically implemented by calling CacheFactory.getAnyInstance() or ClientCacheFactory.getAnyInstance() depending on the GemFireCache type declared and used in the Spring application.
      Type Parameters:
      T - parameterized Class type extending GemFireCache.
      Returns:
      a (existing) reference to a GemFireCache instance.
      Throws:
      org.apache.geode.cache.CacheClosedException - if a GemFireCache reference does not exist.
      See Also:
    • initializeFactory

      @Nullable protected Object initializeFactory(Object factory)
      Initializes the given CacheFactory or ClientCacheFactory with the configured AbstractBasicCacheFactoryBean.CacheFactoryInitializer.
      Parameters:
      factory - CacheFactory or ClientCacheFactory to initialize.
      Returns:
      the initialized CacheFactory or ClientCacheFactory.
      See Also:
    • registerTransactionListeners

      @NonNull protected org.apache.geode.cache.GemFireCache registerTransactionListeners(@NonNull org.apache.geode.cache.GemFireCache cache)
      Registers configured, application-defined TransactionListeners with the cache (transaction manager) to listen for and receive transaction events when a (cache) transaction is processed (e.g. committed or rolled back).
      Parameters:
      cache - GemFireCache used to register the configured, application-defined TransactionListeners; must not be null.
      Returns:
      the given GemFireCache.
      See Also:
      • GemFireCache.getCacheTransactionManager()
      • CacheTransactionManager.addListener(TransactionListener)
      • CacheTransactionManager
      • TransactionListener
      • GemFireCache
    • registerTransactionWriter

      @NonNull protected org.apache.geode.cache.GemFireCache registerTransactionWriter(@NonNull org.apache.geode.cache.GemFireCache cache)
      Registers the configured, application-defined TransactionWriter with the cache (transaction manager) to receive transaction events with the intent to alter the transaction outcome (e.g. veto).
      Parameters:
      cache - GemFireCache used to register the configured, application-defined TransactionWriter, must not be null.
      Returns:
      the given GemFireCache.
      See Also:
      • GemFireCache.getCacheTransactionManager()
      • CacheTransactionManager.setWriter(TransactionWriter)
      • CacheTransactionManager
      • TransactionWriter
      • GemFireCache
    • translateExceptionIfPossible

      @Nullable public org.springframework.dao.DataAccessException translateExceptionIfPossible(@Nullable RuntimeException exception)
      Translates the thrown Apache Geode RuntimeException into a corresponding Exception from Spring's generic DataAccessException hierarchy if possible.
      Specified by:
      translateExceptionIfPossible in interface org.springframework.dao.support.PersistenceExceptionTranslator
      Parameters:
      exception - the Apache Geode RuntimeException to translate.
      Returns:
      the translated Spring DataAccessException or null if the Apache Geode RuntimeException could not be translated.
      See Also:
      • PersistenceExceptionTranslator.translateExceptionIfPossible(RuntimeException)
      • DataAccessException