58
58
* {@link PersistenceExceptionTranslationPostProcessor} to enable AOP-based translation of native Apache Geode
59
59
* {@link RuntimeException RuntimeExceptions} to Spring's {@link DataAccessException} hierarchy. Therefore,
60
60
* 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
62
62
* as Spring {@link DataAccessException DataAccessExceptions}.
63
63
*
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
+ *
64
67
* More importantly, this abstract class encapsulates configuration applicable to tuning Apache Geode in order to
65
68
* efficiently use JVM Heap memory. Since Apache Geode stores data in-memory, on the JVM Heap, it is important that
66
69
* Apache Geode be tuned to monitor the JVM Heap and respond to memory pressure accordingly, by evicting data
69
72
* This abstract class is also concerned with the configuration of PDX and transaction event handling along with
70
73
* whether the contents (entries) of the cache should be made effectively immutable on reads (i.e. get(key)).
71
74
*
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
+ *
72
86
* @author John Blum
73
87
* @see org.apache.geode.GemFireCheckedException
74
88
* @see org.apache.geode.GemFireException
@@ -114,7 +128,7 @@ public abstract class AbstractBasicCacheFactoryBean extends AbstractFactoryBeanS
114
128
private Float evictionHeapPercentage ;
115
129
private Float evictionOffHeapPercentage ;
116
130
117
- private GemFireCache cache ;
131
+ private volatile GemFireCache cache ;
118
132
119
133
private List <TransactionListener > transactionListeners ;
120
134
@@ -128,7 +142,7 @@ public abstract class AbstractBasicCacheFactoryBean extends AbstractFactoryBeanS
128
142
* Sets a reference to the constructed, configured an initialized {@link GemFireCache} instance created by
129
143
* this cache {@link FactoryBean}.
130
144
*
131
- * @param cache {@link GemFireCache} created by this {@link FactoryBean}.
145
+ * @param cache {@link GemFireCache} created by this cache {@link FactoryBean}.
132
146
* @see org.apache.geode.cache.GemFireCache
133
147
*/
134
148
protected void setCache (@ Nullable GemFireCache cache ) {
@@ -139,7 +153,8 @@ protected void setCache(@Nullable GemFireCache cache) {
139
153
* Returns a reference to the constructed, configured an initialized {@link GemFireCache} instance created by
140
154
* this cache {@link FactoryBean}.
141
155
*
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}.
143
158
* @see org.apache.geode.cache.GemFireCache
144
159
*/
145
160
@ SuppressWarnings ("unchecked" )
@@ -152,7 +167,7 @@ protected void setCache(@Nullable GemFireCache cache) {
152
167
* instance created by this cache {@link FactoryBean}.
153
168
*
154
169
* @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}.
156
171
* @see org.apache.geode.cache.GemFireCache
157
172
* @see java.util.Optional
158
173
* @see #getCache()
@@ -186,140 +201,169 @@ public void setCacheFactoryInitializer(@Nullable CacheFactoryInitializer cacheFa
186
201
}
187
202
188
203
/**
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 .
190
205
*
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.
193
207
*/
194
208
public void setClose (boolean close ) {
195
209
this .close = close ;
196
210
}
197
211
198
212
/**
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}.
200
216
*
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 .
202
218
*/
203
219
public boolean isClose () {
204
220
return this .close ;
205
221
}
206
222
207
223
/**
208
- * Sets the {@link GemFireCache#getCopyOnRead()} property of the {@link GemFireCache cache }.
224
+ * Sets the {@link GemFireCache#getCopyOnRead()} property of the {@link GemFireCache}.
209
225
*
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)}.
212
228
*/
213
229
public void setCopyOnRead (@ Nullable Boolean copyOnRead ) {
214
230
this .copyOnRead = copyOnRead ;
215
231
}
216
232
217
233
/**
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}.
219
235
*
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)}.
222
238
*/
223
239
public @ Nullable Boolean getCopyOnRead () {
224
240
return this .copyOnRead ;
225
241
}
226
242
227
243
/**
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)}.
230
246
*
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()
233
252
*/
234
253
public boolean isCopyOnRead () {
235
- return Boolean .TRUE .equals (this . copyOnRead );
254
+ return Boolean .TRUE .equals (getCopyOnRead () );
236
255
}
237
256
238
257
/**
239
- * Set the Cache's critical heap percentage attribute .
258
+ * Set the {@link GemFireCache} critical heap percentage property .
240
259
*
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.
242
262
*/
243
263
public void setCriticalHeapPercentage (@ Nullable Float criticalHeapPercentage ) {
244
264
this .criticalHeapPercentage = criticalHeapPercentage ;
245
265
}
246
266
247
267
/**
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.
249
271
*/
250
272
public Float getCriticalHeapPercentage () {
251
273
return this .criticalHeapPercentage ;
252
274
}
253
275
254
276
/**
255
- * Set the cache's critical off-heap percentage property.
277
+ * Set the {@link GemFireCache} critical off-heap percentage property.
256
278
*
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.
258
281
*/
259
282
public void setCriticalOffHeapPercentage (@ Nullable Float criticalOffHeapPercentage ) {
260
283
this .criticalOffHeapPercentage = criticalOffHeapPercentage ;
261
284
}
262
285
263
286
/**
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.
265
291
*/
266
292
public Float getCriticalOffHeapPercentage () {
267
293
return this .criticalOffHeapPercentage ;
268
294
}
269
295
270
296
/**
271
- * Set the Cache's eviction heap percentage attribute .
297
+ * Set the {@link GemFireCache} eviction heap percentage property .
272
298
*
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.
274
301
*/
275
302
public void setEvictionHeapPercentage (Float evictionHeapPercentage ) {
276
303
this .evictionHeapPercentage = evictionHeapPercentage ;
277
304
}
278
305
279
306
/**
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.
281
310
*/
282
311
public Float getEvictionHeapPercentage () {
283
312
return this .evictionHeapPercentage ;
284
313
}
285
314
286
315
/**
287
- * Set the cache's eviction off-heap percentage property.
316
+ * Set the {@link GemFireCache} eviction off-heap percentage property.
288
317
*
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.
290
320
*/
291
321
public void setEvictionOffHeapPercentage (Float evictionOffHeapPercentage ) {
292
322
this .evictionOffHeapPercentage = evictionOffHeapPercentage ;
293
323
}
294
324
295
325
/**
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.
297
330
*/
298
331
public Float getEvictionOffHeapPercentage () {
299
332
return this .evictionOffHeapPercentage ;
300
333
}
301
334
302
335
/**
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}.
304
337
*
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}.
306
339
* @see org.springframework.beans.factory.FactoryBean#getObject()
307
340
* @see org.apache.geode.cache.GemFireCache
308
341
* @see #doGetObject()
309
342
* @see #getCache()
310
343
*/
311
344
@ Override
312
345
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 ();
314
350
}
315
351
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
+ */
316
359
protected abstract GemFireCache doGetObject ();
317
360
318
361
/**
319
362
* Returns the {@link Class type} of {@link GemFireCache} created by this cache {@link FactoryBean}.
320
363
*
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}.
322
365
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
366
+ * @see #doGetObjectType()
323
367
*/
324
368
@ Override
325
369
public Class <? extends GemFireCache > getObjectType () {
@@ -334,6 +378,7 @@ public Class<? extends GemFireCache> getObjectType() {
334
378
*
335
379
* @return {@link GemFireCache} {@link Class} by default.
336
380
* @see org.apache.geode.cache.GemFireCache#getClass()
381
+ * @see #getObjectType()
337
382
* @see java.lang.Class
338
383
*/
339
384
protected Class <? extends GemFireCache > doGetObjectType () {
@@ -461,7 +506,8 @@ public void setPdxSerializer(@Nullable PdxSerializer serializer) {
461
506
/**
462
507
* Set the lifecycle phase for this cache bean in the Spring container.
463
508
*
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.
465
511
* @see org.springframework.context.Phased#getPhase()
466
512
*/
467
513
protected void setPhase (int phase ) {
@@ -471,7 +517,7 @@ protected void setPhase(int phase) {
471
517
/**
472
518
* Returns the configured lifecycle phase for this cache bean in the Spring container.
473
519
*
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.
475
521
* @see org.springframework.context.Phased#getPhase()
476
522
*/
477
523
@ Override
@@ -696,6 +742,19 @@ protected <T extends GemFireCache> T fetchCache() {
696
742
return cache != null ? cache : doFetchCache ();
697
743
}
698
744
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
+ */
699
758
protected abstract <T extends GemFireCache > T doFetchCache ();
700
759
701
760
/**
0 commit comments