Skip to content

Commit 692ac67

Browse files
committed
Apply additional polish to the cache FactoryBean class hierarchy.
Edit Javadoc. Resolves spring-projectsgh-493.
1 parent 3ef8f81 commit 692ac67

File tree

5 files changed

+124
-61
lines changed

5 files changed

+124
-61
lines changed

spring-data-geode/src/main/java/org/springframework/data/gemfire/AbstractBasicCacheFactoryBean.java

+98-39
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@
5858
* {@link PersistenceExceptionTranslationPostProcessor} to enable AOP-based translation of native Apache Geode
5959
* {@link RuntimeException RuntimeExceptions} to Spring's {@link DataAccessException} hierarchy. Therefore,
6060
* the presence of this class automatically enables Spring's {@link PersistenceExceptionTranslationPostProcessor}
61-
* to translate Apache Geode thrown {@link GemFireException} and {@link GemFireCheckedException} types
61+
* to translate native Apache Geode thrown {@link GemFireException} and {@link GemFireCheckedException} types
6262
* as Spring {@link DataAccessException DataAccessExceptions}.
6363
*
64+
* In addition, this class also assumes the responsibility of positioning the creation of the cache in the appropriate
65+
* phase of the Spring container's lifecycle, providing default callbacks for both initialization and destruction.
66+
*
6467
* More importantly, this abstract class encapsulates configuration applicable to tuning Apache Geode in order to
6568
* efficiently use JVM Heap memory. Since Apache Geode stores data in-memory, on the JVM Heap, it is important that
6669
* Apache Geode be tuned to monitor the JVM Heap and respond to memory pressure accordingly, by evicting data
@@ -69,6 +72,17 @@
6972
* This abstract class is also concerned with the configuration of PDX and transaction event handling along with
7073
* whether the contents (entries) of the cache should be made effectively immutable on reads (i.e. get(key)).
7174
*
75+
* In summary, this abstract class primarily handles and encapsulates the configuration of the following concerns:
76+
*
77+
* <ul>
78+
* <li>copy-on-read semantics</li>
79+
* <li>JVM Heap memory management</li>
80+
* <li>PDX serialization</li>
81+
* <li>Transaction event processing</li>
82+
* </ul>
83+
*
84+
* All of these concerns are applicable to both Apache Geode {@link ClientCache} and peer {@link Cache} instances.
85+
*
7286
* @author John Blum
7387
* @see org.apache.geode.GemFireCheckedException
7488
* @see org.apache.geode.GemFireException
@@ -114,7 +128,7 @@ public abstract class AbstractBasicCacheFactoryBean extends AbstractFactoryBeanS
114128
private Float evictionHeapPercentage;
115129
private Float evictionOffHeapPercentage;
116130

117-
private GemFireCache cache;
131+
private volatile GemFireCache cache;
118132

119133
private List<TransactionListener> transactionListeners;
120134

@@ -128,7 +142,7 @@ public abstract class AbstractBasicCacheFactoryBean extends AbstractFactoryBeanS
128142
* Sets a reference to the constructed, configured an initialized {@link GemFireCache} instance created by
129143
* this cache {@link FactoryBean}.
130144
*
131-
* @param cache {@link GemFireCache} created by this {@link FactoryBean}.
145+
* @param cache {@link GemFireCache} created by this cache {@link FactoryBean}.
132146
* @see org.apache.geode.cache.GemFireCache
133147
*/
134148
protected void setCache(@Nullable GemFireCache cache) {
@@ -139,7 +153,8 @@ protected void setCache(@Nullable GemFireCache cache) {
139153
* Returns a reference to the constructed, configured an initialized {@link GemFireCache} instance created by
140154
* this cache {@link FactoryBean}.
141155
*
142-
* @return a reference to the {@link GemFireCache} created by this {@link FactoryBean}.
156+
* @param <T> parameterized {@link Class} type extending {@link GemFireCache}.
157+
* @return a reference to the {@link GemFireCache} created by this cache {@link FactoryBean}.
143158
* @see org.apache.geode.cache.GemFireCache
144159
*/
145160
@SuppressWarnings("unchecked")
@@ -152,7 +167,7 @@ protected void setCache(@Nullable GemFireCache cache) {
152167
* instance created by this cache {@link FactoryBean}.
153168
*
154169
* @param <T> parameterized {@link Class} type extending {@link GemFireCache}.
155-
* @return an {@link Optional} reference to the {@link GemFireCache} created by this {@link FactoryBean}.
170+
* @return an {@link Optional} reference to the {@link GemFireCache} created by this {cache @link FactoryBean}.
156171
* @see org.apache.geode.cache.GemFireCache
157172
* @see java.util.Optional
158173
* @see #getCache()
@@ -186,140 +201,169 @@ public void setCacheFactoryInitializer(@Nullable CacheFactoryInitializer cacheFa
186201
}
187202

188203
/**
189-
* Sets a boolean value used to determine whether the cache should be closed on shutdown of the Spring application.
204+
* Sets a boolean value used to determine whether the cache should be closed on shutdown of the Spring container.
190205
*
191-
* @param close boolean value used to determine whether the cache will be closed on shutdown
192-
* of the Spring application.
206+
* @param close boolean value used to determine whether the cache will be closed on shutdown of the Spring container.
193207
*/
194208
public void setClose(boolean close) {
195209
this.close = close;
196210
}
197211

198212
/**
199-
* Returns a boolean value used to determine whether the cache will be closed on shutdown of the Spring application.
213+
* Returns a boolean value used to determine whether the cache will be closed on shutdown of the Spring container.
214+
*
215+
* Defaults to {@literal true}.
200216
*
201-
* @return a boolean value used to determine whether the cache will be closed on shutdown of the Spring application.
217+
* @return a boolean value used to determine whether the cache will be closed on shutdown of the Spring container.
202218
*/
203219
public boolean isClose() {
204220
return this.close;
205221
}
206222

207223
/**
208-
* Sets the {@link GemFireCache#getCopyOnRead()} property of the {@link GemFireCache cache}.
224+
* Sets the {@link GemFireCache#getCopyOnRead()} property of the {@link GemFireCache}.
209225
*
210-
* @param copyOnRead a {@link Boolean value} indicating whether {@link Object objects}
211-
* stored in the {@link GemFireCache cache} are copied on read (i.e. {@link Region#get(Object)}.
226+
* @param copyOnRead a {@link Boolean} value to indicate whether {@link Object objects}
227+
* stored in the {@link GemFireCache} are copied on read (i.e. {@link Region#get(Object)}.
212228
*/
213229
public void setCopyOnRead(@Nullable Boolean copyOnRead) {
214230
this.copyOnRead = copyOnRead;
215231
}
216232

217233
/**
218-
* Returns the configuration of the {@link GemFireCache#getCopyOnRead()} property on the {@link GemFireCache cache}.
234+
* Returns the configuration of the {@link GemFireCache#getCopyOnRead()} property set on the {@link GemFireCache}.
219235
*
220-
* @return a {@link Boolean value} indicating whether {@link Object objects}
221-
* stored in the {@link GemFireCache cache} are copied on read (i.e. {@link Region#get(Object)}.
236+
* @return a {@link Boolean} value to indicate whether {@link Object objects}
237+
* stored in the {@link GemFireCache} are copied on read (i.e. {@link Region#get(Object)}.
222238
*/
223239
public @Nullable Boolean getCopyOnRead() {
224240
return this.copyOnRead;
225241
}
226242

227243
/**
228-
* Determines whether {@link Object objects} stored in the {@link GemFireCache cache} are copied
229-
* when read (i.e. {@link Region#get(Object)}.
244+
* Determines whether {@link Object objects} stored in the {@link GemFireCache} are copied when read
245+
* (i.e. {@link Region#get(Object)}.
230246
*
231-
* @return a boolean value indicating whether {@link Object objects} stored in the {@link GemFireCache cache}
232-
* are copied on read (i.e. {@link Region#get(Object)}.
247+
* Defaults to {@literal false}.
248+
*
249+
* @return a boolean value indicating whether {@link Object objects} stored in the {@link GemFireCache}
250+
* are copied when read (i.e. {@link Region#get(Object)}.
251+
* @see #getCopyOnRead()
233252
*/
234253
public boolean isCopyOnRead() {
235-
return Boolean.TRUE.equals(this.copyOnRead);
254+
return Boolean.TRUE.equals(getCopyOnRead());
236255
}
237256

238257
/**
239-
* Set the Cache's critical heap percentage attribute.
258+
* Set the {@link GemFireCache} critical heap percentage property.
240259
*
241-
* @param criticalHeapPercentage floating point value indicating the critical heap percentage.
260+
* @param criticalHeapPercentage {@link Float} value specifying the configuration for the {@link GemFireCache}
261+
* critical heap percentage.
242262
*/
243263
public void setCriticalHeapPercentage(@Nullable Float criticalHeapPercentage) {
244264
this.criticalHeapPercentage = criticalHeapPercentage;
245265
}
246266

247267
/**
248-
* @return the criticalHeapPercentage
268+
* Gets the configuration of the {@link GemFireCache} critical heap percentage property.
269+
*
270+
* @return a {@link Float} value specifying the configuration for the {@link GemFireCache} critical heap percentage.
249271
*/
250272
public Float getCriticalHeapPercentage() {
251273
return this.criticalHeapPercentage;
252274
}
253275

254276
/**
255-
* Set the cache's critical off-heap percentage property.
277+
* Set the {@link GemFireCache} critical off-heap percentage property.
256278
*
257-
* @param criticalOffHeapPercentage floating point value indicating the critical off-heap percentage.
279+
* @param criticalOffHeapPercentage {@link Float} value specifying the configuration for the {@link GemFireCache}
280+
* critical off-heap percentage.
258281
*/
259282
public void setCriticalOffHeapPercentage(@Nullable Float criticalOffHeapPercentage) {
260283
this.criticalOffHeapPercentage = criticalOffHeapPercentage;
261284
}
262285

263286
/**
264-
* @return the criticalOffHeapPercentage
287+
* Gets the configuration of the {@link GemFireCache} critical off-heap percentage property.
288+
*
289+
* @return a {@link Float} value specifying the configuration for the {@link GemFireCache} critical off-heap
290+
* percentage.
265291
*/
266292
public Float getCriticalOffHeapPercentage() {
267293
return this.criticalOffHeapPercentage;
268294
}
269295

270296
/**
271-
* Set the Cache's eviction heap percentage attribute.
297+
* Set the {@link GemFireCache} eviction heap percentage property.
272298
*
273-
* @param evictionHeapPercentage float-point value indicating the Cache's heap use percentage to trigger eviction.
299+
* @param evictionHeapPercentage {@link Float} value specifying the configuration for the {@link GemFireCache}
300+
* eviction heap percentage.
274301
*/
275302
public void setEvictionHeapPercentage(Float evictionHeapPercentage) {
276303
this.evictionHeapPercentage = evictionHeapPercentage;
277304
}
278305

279306
/**
280-
* @return the evictionHeapPercentage
307+
* Gets the configuration of the {@link GemFireCache} eviction heap percentage property.
308+
*
309+
* @return a {@link Float} value specifying the configuration for the {@link GemFireCache} eviction heap percentage.
281310
*/
282311
public Float getEvictionHeapPercentage() {
283312
return this.evictionHeapPercentage;
284313
}
285314

286315
/**
287-
* Set the cache's eviction off-heap percentage property.
316+
* Set the {@link GemFireCache} eviction off-heap percentage property.
288317
*
289-
* @param evictionOffHeapPercentage float-point value indicating the percentage of off-heap use triggering eviction.
318+
* @param evictionOffHeapPercentage {@link Float} value specifying the configuration for the {@link GemFireCache}
319+
* eviction off-heap percentage.
290320
*/
291321
public void setEvictionOffHeapPercentage(Float evictionOffHeapPercentage) {
292322
this.evictionOffHeapPercentage = evictionOffHeapPercentage;
293323
}
294324

295325
/**
296-
* @return the evictionOffHeapPercentage
326+
* Gets the configuration of the {@link GemFireCache} eviction off-heap percentage property.
327+
*
328+
* @return a {@link Float} value specifying the configuration for the {@link GemFireCache} eviction off-heap
329+
* percentage.
297330
*/
298331
public Float getEvictionOffHeapPercentage() {
299332
return this.evictionOffHeapPercentage;
300333
}
301334

302335
/**
303-
* Returns the cache object reference created by this cache {@link FactoryBean}.
336+
* Returns the {@link GemFireCache cache object reference} created by this cache {@link FactoryBean}.
304337
*
305-
* @return the cache object reference created by this cache {@link FactoryBean}.
338+
* @return the {@link GemFireCache cache object reference} created by this cache {@link FactoryBean}.
306339
* @see org.springframework.beans.factory.FactoryBean#getObject()
307340
* @see org.apache.geode.cache.GemFireCache
308341
* @see #doGetObject()
309342
* @see #getCache()
310343
*/
311344
@Override
312345
public GemFireCache getObject() throws Exception {
313-
return Optional.<GemFireCache>ofNullable(getCache()).orElseGet(this::doGetObject);
346+
347+
GemFireCache cache = getCache();
348+
349+
return cache != null ? cache : doGetObject();
314350
}
315351

352+
/**
353+
* Called if {@link #getCache()} returns a {@literal null} {@link GemFireCache} reference from {@link #getObject()}.
354+
*
355+
* @return a new constructed, configured and initialized {@link GemFireCache} instance.
356+
* @see org.apache.geode.cache.GemFireCache
357+
* @see #getObject()
358+
*/
316359
protected abstract GemFireCache doGetObject();
317360

318361
/**
319362
* Returns the {@link Class type} of {@link GemFireCache} created by this cache {@link FactoryBean}.
320363
*
321-
* @return the {@link Class type} type of {@link GemFireCache} created by this cache {@link FactoryBean}.
364+
* @return the {@link Class type} of {@link GemFireCache} created by this cache {@link FactoryBean}.
322365
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
366+
* @see #doGetObjectType()
323367
*/
324368
@Override
325369
public Class<? extends GemFireCache> getObjectType() {
@@ -334,6 +378,7 @@ public Class<? extends GemFireCache> getObjectType() {
334378
*
335379
* @return {@link GemFireCache} {@link Class} by default.
336380
* @see org.apache.geode.cache.GemFireCache#getClass()
381+
* @see #getObjectType()
337382
* @see java.lang.Class
338383
*/
339384
protected Class<? extends GemFireCache> doGetObjectType() {
@@ -461,7 +506,8 @@ public void setPdxSerializer(@Nullable PdxSerializer serializer) {
461506
/**
462507
* Set the lifecycle phase for this cache bean in the Spring container.
463508
*
464-
* @param phase {@link Integer#TYPE} value used as the lifecycle phase for this cache bean in the Spring container.
509+
* @param phase {@link Integer#TYPE} value specifying the lifecycle phase for this cache bean
510+
* in the Spring container.
465511
* @see org.springframework.context.Phased#getPhase()
466512
*/
467513
protected void setPhase(int phase) {
@@ -471,7 +517,7 @@ protected void setPhase(int phase) {
471517
/**
472518
* Returns the configured lifecycle phase for this cache bean in the Spring container.
473519
*
474-
* @return an {@link Integer#TYPE} used as the lifecycle phase for this cache bean in the Spring container.
520+
* @return an {@link Integer#TYPE} used specifying the lifecycle phase for this cache bean in the Spring container.
475521
* @see org.springframework.context.Phased#getPhase()
476522
*/
477523
@Override
@@ -696,6 +742,19 @@ protected <T extends GemFireCache> T fetchCache() {
696742
return cache != null ? cache : doFetchCache();
697743
}
698744

745+
/**
746+
* Called by {@link #fetchCache()} if the {@link GemFireCache} reference returned by {@link #getCache()}
747+
* is {@literal null}.
748+
*
749+
* This method is typically implemented by calling {@link CacheFactory#getAnyInstance()}
750+
* or {@link ClientCacheFactory#getAnyInstance()} depending on the {@link GemFireCache} type declared
751+
* and used in the Spring application.
752+
*
753+
* @param <T> parameterized {@link Class} type extending {@link GemFireCache}.
754+
* @return a (existing) reference to a {@link GemFireCache} instance.
755+
* @throws org.apache.geode.cache.CacheClosedException if a {@link GemFireCache} reference does not exist.
756+
* @see #fetchCache()
757+
*/
699758
protected abstract <T extends GemFireCache> T doFetchCache();
700759

701760
/**

spring-data-geode/src/main/java/org/springframework/data/gemfire/AbstractConfigurableCacheFactoryBean.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* @see org.apache.geode.cache.client.ClientCache
4747
* @see org.springframework.beans.factory.BeanFactory
4848
* @see org.springframework.core.io.Resource
49+
* @see org.springframework.data.gemfire.AbstractBasicCacheFactoryBean
4950
* @see org.springframework.data.gemfire.support.GemfireBeanFactoryLocator
5051
* @since 2.5.0
5152
*/
@@ -218,8 +219,8 @@ public boolean isUseBeanFactoryLocator() {
218219
* and an existing {@link #getBeanFactoryLocator() GemfireBeanFactoryLocator} is not already present.
219220
*
220221
* @see org.springframework.data.gemfire.support.GemfireBeanFactoryLocator#newBeanFactoryLocator(BeanFactory, String)
222+
* @see #getOptionalBeanFactoryLocator()
221223
* @see #isUseBeanFactoryLocator()
222-
* @see #getBeanFactoryLocator()
223224
* @see #getBeanFactory()
224225
* @see #getBeanName()
225226
*/
@@ -234,13 +235,14 @@ public void afterPropertiesSet() throws Exception {
234235
}
235236

236237
private boolean useBeanFactoryLocator() {
237-
return isUseBeanFactoryLocator() && getBeanFactoryLocator() == null;
238+
return isUseBeanFactoryLocator() && !getOptionalBeanFactoryLocator().isPresent();
238239
}
239240

240241
/**
241242
* Destroys and releases resources used by the {@link GemfireBeanFactoryLocator}, if present.
242243
*
243244
* @see org.springframework.data.gemfire.support.GemfireBeanFactoryLocator#destroy()
245+
* @see #getOptionalBeanFactoryLocator()
244246
*/
245247
@Override
246248
public void destroy() {
@@ -262,6 +264,7 @@ public void destroy() {
262264
* into the {@link GemFireCache}.
263265
* @see org.apache.geode.cache.GemFireCache#loadCacheXml(InputStream)
264266
* @see org.apache.geode.cache.GemFireCache
267+
* @see #getOptionalCacheXml()
265268
*/
266269
protected @NonNull <T extends GemFireCache> T loadCacheXml(@NonNull T cache) {
267270

spring-data-geode/src/main/java/org/springframework/data/gemfire/AbstractResolvableCacheFactoryBean.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.springframework.lang.NonNull;
3030

3131
/**
32-
* Abstract base class encapsulating logic to resolve or create a {@link GemFireCache cache} instance.
32+
* Abstract base class encapsulating logic to resolve or create a {@link GemFireCache} instance.
3333
*
3434
* @author John Blum
3535
* @see java.util.Properties

0 commit comments

Comments
 (0)