25
25
import java .util .Objects ;
26
26
import java .util .Optional ;
27
27
import java .util .Properties ;
28
+ import java .util .function .Function ;
28
29
29
30
import org .apache .geode .GemFireCheckedException ;
30
31
import org .apache .geode .GemFireException ;
57
58
import org .springframework .util .Assert ;
58
59
59
60
/**
60
- * Abstract base class for {@link CacheFactoryBean} and {@link ClientCacheFactoryBean} classes
61
+ * Abstract base class for {@link CacheFactoryBean} and {@link ClientCacheFactoryBean} classes,
61
62
* used to create Apache Geode peer {@link Cache} and {@link ClientCache} instances, respectively.
62
63
*
63
64
* This class implements Spring's {@link PersistenceExceptionTranslator} interface and is auto-detected by Spring's
66
67
* the presence of this class automatically enables a {@link PersistenceExceptionTranslationPostProcessor}
67
68
* to translate Apache Geode {@link RuntimeException RuntimeExceptions} appropriately.
68
69
*
69
- * Importantly, this class encapsulates configure applicable to tuning Apache Geode in response to JVM Heap memory.
70
- * Since Apache Geode stores data in-memory, on the JVM Heap, it is important that Aapche Geode be tuned to monitor
71
- * the JVM Heap and respond accordingly to memory pressure, by evicting data and issuing warnings when the JVM Heap
72
- * reaches critical mass.
70
+ * Importantly, this abstract class encapsulates configuration applicable to tuning Apache Geode to efficiently use JVM
71
+ * Heap memory. Since Apache Geode stores data in-memory, on the JVM Heap, it is important that Apache Geode be tuned
72
+ * to monitor the JVM Heap and respond accordingly to memory pressure, by evicting data and issuing warnings when the
73
+ * JVM Heap reaches critical mass.
73
74
*
74
75
* @author John Blum
75
76
* @see java.io.File
@@ -170,6 +171,20 @@ protected void setCache(@Nullable GemFireCache cache) {
170
171
return (T ) this .cache ;
171
172
}
172
173
174
+ /**
175
+ * Returns an {@link Optional} reference to the constructed, configured and initialized {@link GemFireCache}
176
+ * instance created by this cache {@link FactoryBean}.
177
+ *
178
+ * @param <T> parameterized {@link Class} type extending {@link GemFireCache}.
179
+ * @return an {@link Optional} reference to the {@link GemFireCache} created by this {@link FactoryBean}.
180
+ * @see org.apache.geode.cache.GemFireCache
181
+ * @see java.util.Optional
182
+ * @see #getCache()
183
+ */
184
+ public <T extends GemFireCache > Optional <T > getOptionalCache () {
185
+ return Optional .ofNullable (getCache ());
186
+ }
187
+
173
188
/**
174
189
* Set the {@link CacheFactoryInitializer} called by this {@link FactoryBean} to initialize the Apache Geode
175
190
* cache factory used to create the cache constructed by this {@link FactoryBean}.
@@ -592,7 +607,20 @@ private boolean isHeapPercentageValid(@NonNull Float heapPercentage) {
592
607
return heapPercentage >= 0.0f && heapPercentage <= 100.0f ;
593
608
}
594
609
595
- protected GemFireCache configureHeapPercentages (GemFireCache cache ) {
610
+ /**
611
+ * Configures the {@link GemFireCache} critical and eviction heap thresholds as percentages.
612
+ *
613
+ * @param cache {@link GemFireCache} to configure the critical and eviction heap thresholds;
614
+ * must not be {@literal null}.
615
+ * @return the given {@link GemFireCache}.
616
+ * @throws IllegalArgumentException if the critical or eviction heap thresholds are not valid percentages.
617
+ * @see org.apache.geode.cache.control.ResourceManager#setCriticalHeapPercentage(float)
618
+ * @see org.apache.geode.cache.control.ResourceManager#setEvictionHeapPercentage(float)
619
+ * @see org.apache.geode.cache.control.ResourceManager
620
+ * @see org.apache.geode.cache.GemFireCache#getResourceManager()
621
+ * @see org.apache.geode.cache.GemFireCache
622
+ */
623
+ protected @ NonNull GemFireCache configureHeapPercentages (@ NonNull GemFireCache cache ) {
596
624
597
625
Optional .ofNullable (getCriticalHeapPercentage ()).ifPresent (criticalHeapPercentage -> {
598
626
@@ -615,7 +643,20 @@ protected GemFireCache configureHeapPercentages(GemFireCache cache) {
615
643
return cache ;
616
644
}
617
645
618
- protected GemFireCache configureOffHeapPercentages (GemFireCache cache ) {
646
+ /**
647
+ * Configures the {@link GemFireCache} critical and eviction off-heap thresholds as percentages.
648
+ *
649
+ * @param cache {@link GemFireCache} to configure the critical and eviction off-heap thresholds;
650
+ * must not be {@literal null}.
651
+ * @return the given {@link GemFireCache}.
652
+ * @throws IllegalArgumentException if the critical or eviction off-heap thresholds are not valid percentages.
653
+ * @see org.apache.geode.cache.control.ResourceManager#setCriticalOffHeapPercentage(float)
654
+ * @see org.apache.geode.cache.control.ResourceManager#setEvictionOffHeapPercentage(float)
655
+ * @see org.apache.geode.cache.control.ResourceManager
656
+ * @see org.apache.geode.cache.GemFireCache#getResourceManager()
657
+ * @see org.apache.geode.cache.GemFireCache
658
+ */
659
+ protected @ NonNull GemFireCache configureOffHeapPercentages (@ NonNull GemFireCache cache ) {
619
660
620
661
Optional .ofNullable (getCriticalOffHeapPercentage ()).ifPresent (criticalOffHeapPercentage -> {
621
662
@@ -697,15 +738,17 @@ protected Object initializeFactory(Object factory) {
697
738
}
698
739
699
740
/**
700
- * Loads the configured {@literal cache.xml} to initialize the cache .
741
+ * Loads the configured {@literal cache.xml} to initialize the {@link GemFireCache} .
701
742
*
702
743
* @param <T> parameterized {@link Class} type extending {@link GemFireCache}.
703
- * @param cache cache instance to initialized with {@literal cache.xml}.
704
- * @return the given cache instance.
705
- * @throws RuntimeException if the configured {@literal cache.xml} file could not be loaded.
744
+ * @param cache {@link GemFireCache} instance to initialize with {@literal cache.xml}; must not be {@literal null}.
745
+ * @return the given {@link GemFireCache}.
746
+ * @throws RuntimeException if the configured {@literal cache.xml} file could not be loaded
747
+ * into the {@link GemFireCache}.
706
748
* @see org.apache.geode.cache.GemFireCache#loadCacheXml(InputStream)
749
+ * @see org.apache.geode.cache.GemFireCache
707
750
*/
708
- protected <T extends GemFireCache > T loadCacheXml (T cache ) {
751
+ protected @ NonNull <T extends GemFireCache > T loadCacheXml (@ NonNull T cache ) {
709
752
710
753
// Load the cache.xml file (Resource) and initialize the cache
711
754
Optional .ofNullable (getCacheXml ()).ifPresent (cacheXml -> {
@@ -721,7 +764,21 @@ protected <T extends GemFireCache> T loadCacheXml(T cache) {
721
764
return cache ;
722
765
}
723
766
724
- protected GemFireCache registerTransactionListeners (GemFireCache cache ) {
767
+ /**
768
+ * Registers configured, application-defined {@link TransactionListener TransactionListeners} with the cache
769
+ * (transaction manager) to listen for and receive transaction events when a (cache) transaction is processed
770
+ * (e.g. committed or rolled back).
771
+ *
772
+ * @param cache {@link GemFireCache} used to register the configured, application-defined
773
+ * {@link TransactionListener TransactionListeners}; must not be {@literal null}.
774
+ * @return the given {@link GemFireCache}.
775
+ * @see org.apache.geode.cache.GemFireCache#getCacheTransactionManager()
776
+ * @see org.apache.geode.cache.CacheTransactionManager#addListener(TransactionListener)
777
+ * @see org.apache.geode.cache.CacheTransactionManager
778
+ * @see org.apache.geode.cache.TransactionListener
779
+ * @see org.apache.geode.cache.GemFireCache
780
+ */
781
+ protected @ NonNull GemFireCache registerTransactionListeners (@ NonNull GemFireCache cache ) {
725
782
726
783
CollectionUtils .nullSafeCollection (getTransactionListeners ()).stream ()
727
784
.filter (Objects ::nonNull )
@@ -730,7 +787,20 @@ protected GemFireCache registerTransactionListeners(GemFireCache cache) {
730
787
return cache ;
731
788
}
732
789
733
- protected GemFireCache registerTransactionWriter (GemFireCache cache ) {
790
+ /**
791
+ * Registers the configured, application-defined {@link TransactionWriter} with the cache (transaction manager)
792
+ * to receive transaction events with the intent to alter the transaction outcome (e.g. veto).
793
+ *
794
+ * @param cache {@link GemFireCache} used to register the configured, application-defined {@link TransactionWriter},
795
+ * must not be {@literal null}.
796
+ * @return the given {@link GemFireCache}.
797
+ * @see org.apache.geode.cache.GemFireCache#getCacheTransactionManager()
798
+ * @see org.apache.geode.cache.CacheTransactionManager#setWriter(TransactionWriter)
799
+ * @see org.apache.geode.cache.CacheTransactionManager
800
+ * @see org.apache.geode.cache.TransactionWriter
801
+ * @see org.apache.geode.cache.GemFireCache
802
+ */
803
+ protected @ NonNull GemFireCache registerTransactionWriter (@ NonNull GemFireCache cache ) {
734
804
735
805
Optional .ofNullable (getTransactionWriter ())
736
806
.ifPresent (transactionWriter -> cache .getCacheTransactionManager ().setWriter (transactionWriter ));
@@ -739,20 +809,21 @@ protected GemFireCache registerTransactionWriter(GemFireCache cache) {
739
809
}
740
810
741
811
/**
742
- * Resolves the Apache Geode {@link Properties} used to configure the {@link Cache }.
812
+ * Resolves the Apache Geode {@link Properties} used to configure the {@link GemFireCache }.
743
813
*
744
- * @return the resolved Apache Geode {@link Properties} used to configure the {@link Cache }.
814
+ * @return the resolved Apache Geode {@link Properties} used to configure the {@link GemFireCache }.
745
815
* @see #setAndGetProperties(Properties)
746
816
* @see #getProperties()
817
+ * @see java.util.Properties
747
818
*/
748
- protected Properties resolveProperties () {
819
+ protected @ NonNull Properties resolveProperties () {
749
820
750
821
return Optional .ofNullable (getProperties ())
751
822
.orElseGet (() -> setAndGetProperties (new Properties ()));
752
823
}
753
824
754
825
/**
755
- * Translates the thrown Apache Geode {@link RuntimeException} to a corresponding {@link Exception} from Spring's
826
+ * Translates the thrown Apache Geode {@link RuntimeException} into a corresponding {@link Exception} from Spring's
756
827
* generic {@link DataAccessException} hierarchy if possible.
757
828
*
758
829
* @param exception the Apache Geode {@link RuntimeException} to translate.
@@ -762,7 +833,7 @@ protected Properties resolveProperties() {
762
833
* @see org.springframework.dao.DataAccessException
763
834
*/
764
835
@ Override
765
- public DataAccessException translateExceptionIfPossible (@ Nullable RuntimeException exception ) {
836
+ public @ Nullable DataAccessException translateExceptionIfPossible (@ Nullable RuntimeException exception ) {
766
837
767
838
if (exception instanceof IllegalArgumentException ) {
768
839
@@ -790,22 +861,35 @@ public DataAccessException translateExceptionIfPossible(@Nullable RuntimeExcepti
790
861
}
791
862
792
863
/**
793
- * Callback interface for initializing either a {@link CacheFactory} or a {@link ClientCacheFactory} instance,
864
+ * Callback interface for initializing a {@link CacheFactory} or a {@link ClientCacheFactory} instance,
794
865
* which is used to create an instance of {@link GemFireCache}.
795
866
*
796
- * @see org.apache.geode.cache.CacheFactory
797
867
* @see org.apache.geode.cache.client.ClientCacheFactory
868
+ * @see org.apache.geode.cache.CacheFactory
869
+ * @see java.util.function.Function
798
870
*/
799
871
@ FunctionalInterface
800
- public interface CacheFactoryInitializer <T > {
872
+ public interface CacheFactoryInitializer <T > extends Function <T , T > {
873
+
874
+ /**
875
+ * Alias for {@link #initialize(Object)}.
876
+ *
877
+ * @param t cache factory to initialize.
878
+ * @return the initialized cache factory.
879
+ * @see #initialize(Object)
880
+ */
881
+ @ Override
882
+ default T apply (T t ) {
883
+ return initialize (t );
884
+ }
801
885
802
886
/**
803
887
* Initialize the given cache factory.
804
888
*
805
889
* @param cacheFactory cache factory to initialize.
806
890
* @return the given cache factory.
807
- * @see org.apache.geode.cache.CacheFactory
808
891
* @see org.apache.geode.cache.client.ClientCacheFactory
892
+ * @see org.apache.geode.cache.CacheFactory
809
893
*/
810
894
T initialize (T cacheFactory );
811
895
0 commit comments