Package org.springframework.data.gemfire
Class GemfireTemplate
java.lang.Object
org.springframework.data.gemfire.GemfireAccessor
org.springframework.data.gemfire.GemfireTemplate
- All Implemented Interfaces:
InitializingBean
,GemfireOperations
The
GemfireTemplate
class simplifies Apache Geode data access operations, converting Apache Geode
GemFireCheckedExceptions
and GemFireExceptions
into
Spring DataAccessExceptions
, following the org.springframework.dao
Exception
hierarchy.
The central method is execute
, supporting Apache Geode data access code implementing the
GemfireCallback
interface. It provides dedicated handling such that neither the GemfireCallback
implementation nor the calling code needs to explicitly care about handling Region
life-cycle
Exceptions
.
This template class is typically used to implement data access operations or business logic services using Apache
Geode within their implementation but are Geode-agnostic in their interface. The latter or code calling the latter
only have to deal with business objects, query objects, and org.springframework.dao
Exceptions
.- Author:
- Costin Leau, John Blum
- See Also:
-
Field Summary
Fields inherited from class org.springframework.data.gemfire.GemfireAccessor
logger
-
Constructor Summary
ConstructorDescriptionConstructs a new, uninitialized instance ofGemfireTemplate
.GemfireTemplate
(Region<K, V> region) Constructs a new instance of theGemfireTemplate
initialized with the givenRegion
on which (cache) data access operations will be performed. -
Method Summary
Modifier and TypeMethodDescriptionvoid
boolean
containsKey
(Object key) boolean
boolean
containsValue
(Object value) boolean
<K,
V> void create
(K key, V value) protected <K,
V> Region<K, V> createRegionProxy
(Region<K, V> region) Create a close-suppressing proxy for the given Apache Geode cacheRegion
.<T> T
execute
(GemfireCallback<T> action) Executes the given data access operation defined by theGemfireCallback
in the context of Apache Geode.<T> T
execute
(GemfireCallback<T> action, boolean exposeNativeRegion) Executes the given data access operation defined by theGemfireCallback
in the context of Apache Geode.<E> SelectResults<E>
Executes a GemFire query with the given (optional) parameters and returns the result.<T> T
findUnique
(String query, Object... arguments) Executes a GemFire query with the given (optional) parameters and returns the result.<K,
V> V get
(K key) <K,
V> Map<K, V> getAll
(Collection<?> keys) boolean
<K,
V> V put
(K key, V value) <K,
V> void <K,
V> V putIfAbsent
(K key, V value) <E> SelectResults<E>
Shortcut forRegion.query(String)
method.<K,
V> V remove
(K key) void
removeAll
(Collection<?> keys) <K,
V> V replace
(K key, V value) <K,
V> boolean replace
(K key, V oldValue, V newValue) protected QueryService
resolveQueryService
(Region<?, ?> region) Returns theQueryService
used by this template in its query/finder methods.void
setExposeNativeRegion
(boolean exposeNativeRegion) Configure whether to expose the nativeRegion
toGemfireCallback
code.Methods inherited from class org.springframework.data.gemfire.GemfireAccessor
convertGemFireAccessException, convertGemFireAccessException, convertGemFireQueryException, getRegion, setRegion
-
Constructor Details
-
GemfireTemplate
public GemfireTemplate()Constructs a new, uninitialized instance ofGemfireTemplate
.- See Also:
-
GemfireTemplate
Constructs a new instance of theGemfireTemplate
initialized with the givenRegion
on which (cache) data access operations will be performed.
-
-
Method Details
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSet
in interfaceInitializingBean
- Overrides:
afterPropertiesSet
in classGemfireAccessor
-
setExposeNativeRegion
public void setExposeNativeRegion(boolean exposeNativeRegion) Configure whether to expose the nativeRegion
toGemfireCallback
code.Default is false, therefore a
Region
proxy will be returned, suppressingclose
calls.As there is often a need to cast to an interface, the exposed proxy implements all interfaces implemented by the original
Region
. If this is not sufficient, turn this flag to true.- Parameters:
exposeNativeRegion
- a boolean value indicating whether the nativeRegion
should be exposed to theGemfireCallback
.- See Also:
-
isExposeNativeRegion
public boolean isExposeNativeRegion()- Returns:
- a boolean value indicating whether the native
Region
or theRegion
proxy is exposed toGemfireCallback
code. - See Also:
-
containsKey
- Specified by:
containsKey
in interfaceGemfireOperations
-
containsKeyOnServer
- Specified by:
containsKeyOnServer
in interfaceGemfireOperations
-
containsValue
- Specified by:
containsValue
in interfaceGemfireOperations
-
containsValueForKey
- Specified by:
containsValueForKey
in interfaceGemfireOperations
-
create
public <K,V> void create(K key, V value) - Specified by:
create
in interfaceGemfireOperations
-
get
public <K,V> V get(K key) - Specified by:
get
in interfaceGemfireOperations
-
getAll
- Specified by:
getAll
in interfaceGemfireOperations
-
put
public <K,V> V put(K key, V value) - Specified by:
put
in interfaceGemfireOperations
-
putAll
- Specified by:
putAll
in interfaceGemfireOperations
-
putIfAbsent
public <K,V> V putIfAbsent(K key, V value) - Specified by:
putIfAbsent
in interfaceGemfireOperations
-
remove
public <K,V> V remove(K key) - Specified by:
remove
in interfaceGemfireOperations
-
removeAll
- Specified by:
removeAll
in interfaceGemfireOperations
-
replace
public <K,V> V replace(K key, V value) - Specified by:
replace
in interfaceGemfireOperations
-
replace
public <K,V> boolean replace(K key, V oldValue, V newValue) - Specified by:
replace
in interfaceGemfireOperations
-
query
Description copied from interface:GemfireOperations
Shortcut forRegion.query(String)
method. Filters the values of this region using the predicate given as a string with the syntax of the WHERE clause of the query language. The predefined variable this may be used inside the predicate to denote the current element being filtered. This method evaluates the passed in where clause and returns results. It is supported on servers as well as clients. When executed on a client, this method always runs on the server and returns results. When invoking this method from the client, applications can pass in a where clause or a complete query.- Specified by:
query
in interfaceGemfireOperations
- Type Parameters:
E
- type parameter specifying the type of the select results.- Parameters:
query
- an OQL Query language boolean query predicate.- Returns:
- A SelectResults containing the values of this Region that match the predicate.
- See Also:
-
find
public <E> SelectResults<E> find(String query, Object... arguments) throws InvalidDataAccessApiUsageException Description copied from interface:GemfireOperations
Executes a GemFire query with the given (optional) parameters and returns the result. Note this method expects the query to return multiple results; for queries that return only one element useGemfireOperations.findUnique(String, Object...)
. As oppose, to theGemfireOperations.query(String)
method, this method allows for more generic queries (against multiple regions even) to be executed. Note that the local query service is used if the region is configured as a client without any pool configuration or server connectivity - otherwise the query service on the default pool is being used.- Specified by:
find
in interfaceGemfireOperations
- Type Parameters:
E
- type parameter specifying the type of the select results.- Parameters:
query
- the OQL query statement to execute.arguments
- an array of Object values used as arguments to bind to the OQL query parameters (such as $1).- Returns:
- A
SelectResults
instance holding the objects matching the query - Throws:
InvalidDataAccessApiUsageException
- in case the query returns a single result (not aSelectResults
).- See Also:
-
findUnique
public <T> T findUnique(String query, Object... arguments) throws InvalidDataAccessApiUsageException Description copied from interface:GemfireOperations
Executes a GemFire query with the given (optional) parameters and returns the result. Note this method expects the query to return a single result; for queries that return multiple elements useGemfireOperations.find(String, Object...)
. As oppose, to theGemfireOperations.query(String)
method, this method allows for more generic queries (against multiple regions even) to be executed. Note that the local query service is used if the region is configured as a client without any pool configuration or server connectivity - otherwise the query service on the default pool is being used.- Specified by:
findUnique
in interfaceGemfireOperations
- Type Parameters:
T
- type parameter specifying the returned result type.- Parameters:
query
- the OQL query statement to execute.arguments
- an array of Object values used as arguments to bind to the OQL query parameters (such as $1).- Returns:
- The (single) object that represents the result of the query.
- Throws:
InvalidDataAccessApiUsageException
- in case the query returns multiple objects (throughSelectResults
).- See Also:
-
resolveQueryService
Returns theQueryService
used by this template in its query/finder methods.- Parameters:
region
-Region
used to acquire theQueryService
.- Returns:
- the
QueryService
that will perform theQuery
. - See Also:
-
execute
Executes the given data access operation defined by theGemfireCallback
in the context of Apache Geode.- Specified by:
execute
in interfaceGemfireOperations
- Type Parameters:
T
-type
returned by theGemfireCallback
.- Parameters:
action
-GemfireCallback
object defining the Apache Geode action to execute; must not be null.- Returns:
- the result of executing the
GemfireCallback
. - Throws:
DataAccessException
- if an Apache Geode error is thrown by a data access operation.IllegalArgumentException
- ifGemfireCallback
is null.- See Also:
-
execute
public <T> T execute(@NonNull GemfireCallback<T> action, boolean exposeNativeRegion) throws DataAccessException Executes the given data access operation defined by theGemfireCallback
in the context of Apache Geode.- Specified by:
execute
in interfaceGemfireOperations
- Type Parameters:
T
-type
returned by theGemfireCallback
.- Parameters:
action
-GemfireCallback
object defining the Apache Geode action to execute; must not be null.exposeNativeRegion
- boolean value indicating whether to pass the nativeRegion
or theRegion
proxy to theGemfireCallback
.- Returns:
- the result of executing the
GemfireCallback
. - Throws:
DataAccessException
- if an Apache Geode error is thrown by a data access operation.IllegalArgumentException
- ifGemfireCallback
is null.- See Also:
-
createRegionProxy
Create a close-suppressing proxy for the given Apache Geode cacheRegion
. Called by theexecute
method.
-