Spring Data for VMware GemFire
Package org.springframework.data.gemfire
Class GemfireTemplate
java.lang.Object
org.springframework.data.gemfire.GemfireAccessor
org.springframework.data.gemfire.GemfireTemplate
- All Implemented Interfaces:
org.springframework.beans.factory.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.- See Also:
-
MapGemFireCheckedExceptionGemFireExceptionRegionClientCacheQueryQueryServiceSelectResultsGemfireAccessorGemfireOperations
-
Field Summary
Fields inherited from class org.springframework.data.gemfire.GemfireAccessor
logger -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new, uninitialized instance ofGemfireTemplate.GemfireTemplate(org.apache.geode.cache.Region<K, V> region) Constructs a new instance of theGemfireTemplateinitialized with the givenRegionon which (cache) data access operations will be performed. -
Method Summary
Modifier and TypeMethodDescriptionvoidbooleancontainsKey(Object key) booleanbooleancontainsValue(Object value) boolean<K,V> void create(K key, V value) protected <K,V> org.apache.geode.cache.Region<K, V> createRegionProxy(org.apache.geode.cache.Region<K, V> region) Create a close-suppressing proxy for the given Apache Geode cacheRegion.<T> Texecute(GemfireCallback<T> action) Executes the given data access operation defined by theGemfireCallbackin the context of Apache Geode.<T> Texecute(GemfireCallback<T> action, boolean exposeNativeRegion) Executes the given data access operation defined by theGemfireCallbackin the context of Apache Geode.<E> org.apache.geode.cache.query.SelectResults<E>Executes a GemFire query with the given (optional) parameters and returns the result.<T> TfindUnique(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> org.apache.geode.cache.query.SelectResults<E>Shortcut forRegion.query(String)method.<K,V> V remove(K key) voidremoveAll(Collection<?> keys) <K,V> V replace(K key, V value) <K,V> boolean replace(K key, V oldValue, V newValue) protected org.apache.geode.cache.query.QueryServiceresolveQueryService(org.apache.geode.cache.Region<?, ?> region) Returns theQueryServiceused by this template in its query/finder methods.voidsetExposeNativeRegion(boolean exposeNativeRegion) Configure whether to expose the nativeRegiontoGemfireCallbackcode.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
public GemfireTemplate(org.apache.geode.cache.Region<K, V> region) Constructs a new instance of theGemfireTemplateinitialized with the givenRegionon which (cache) data access operations will be performed.- Type Parameters:
K-typeof theRegionkey.V-typeof theRegionvalue.- Parameters:
region-Regionon which data access operations will be performed by this template; must not be null.- Throws:
IllegalArgumentException- ifRegionis null.- See Also:
-
-
Method Details
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Overrides:
afterPropertiesSetin classGemfireAccessor
-
setExposeNativeRegion
public void setExposeNativeRegion(boolean exposeNativeRegion) Configure whether to expose the nativeRegiontoGemfireCallbackcode.Default is false, therefore a
Regionproxy will be returned, suppressingclosecalls.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 nativeRegionshould be exposed to theGemfireCallback.- See Also:
-
isExposeNativeRegion
public boolean isExposeNativeRegion()- Returns:
- a boolean value indicating whether the native
Regionor theRegionproxy is exposed toGemfireCallbackcode. - See Also:
-
containsKey
- Specified by:
containsKeyin interfaceGemfireOperations
-
containsKeyOnServer
- Specified by:
containsKeyOnServerin interfaceGemfireOperations
-
containsValue
- Specified by:
containsValuein interfaceGemfireOperations
-
containsValueForKey
- Specified by:
containsValueForKeyin interfaceGemfireOperations
-
create
public <K,V> void create(K key, V value) - Specified by:
createin interfaceGemfireOperations
-
get
public <K,V> V get(K key) - Specified by:
getin interfaceGemfireOperations
-
getAll
- Specified by:
getAllin interfaceGemfireOperations
-
put
public <K,V> V put(K key, V value) - Specified by:
putin interfaceGemfireOperations
-
putAll
- Specified by:
putAllin interfaceGemfireOperations
-
putIfAbsent
public <K,V> V putIfAbsent(K key, V value) - Specified by:
putIfAbsentin interfaceGemfireOperations
-
remove
public <K,V> V remove(K key) - Specified by:
removein interfaceGemfireOperations
-
removeAll
- Specified by:
removeAllin interfaceGemfireOperations
-
replace
public <K,V> V replace(K key, V value) - Specified by:
replacein interfaceGemfireOperations
-
replace
public <K,V> boolean replace(K key, V oldValue, V newValue) - Specified by:
replacein interfaceGemfireOperations
-
query
Description copied from interface:GemfireOperationsShortcut 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:
queryin 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:
-
Region.query(String)
-
find
public <E> org.apache.geode.cache.query.SelectResults<E> find(String query, Object... arguments) throws org.springframework.dao.InvalidDataAccessApiUsageException Description copied from interface:GemfireOperationsExecutes 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:
findin 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
SelectResultsinstance holding the objects matching the query - Throws:
org.springframework.dao.InvalidDataAccessApiUsageException- in case the query returns a single result (not aSelectResults).- See Also:
-
QueryService.newQuery(String)Query.execute(Object[])SelectResults
-
findUnique
public <T> T findUnique(String query, Object... arguments) throws org.springframework.dao.InvalidDataAccessApiUsageException Description copied from interface:GemfireOperationsExecutes 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:
findUniquein 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:
org.springframework.dao.InvalidDataAccessApiUsageException- in case the query returns multiple objects (throughSelectResults).- See Also:
-
QueryService.newQuery(String)Query.execute(Object[])
-
resolveQueryService
protected org.apache.geode.cache.query.QueryService resolveQueryService(org.apache.geode.cache.Region<?, ?> region) Returns theQueryServiceused by this template in its query/finder methods.- Parameters:
region-Regionused to acquire theQueryService.- Returns:
- the
QueryServicethat will perform theQuery. - See Also:
-
RegionRegion.getRegionService()RegionService.getQueryService()ClientCache.getLocalQueryService()
-
execute
public <T> T execute(@NonNull GemfireCallback<T> action) throws org.springframework.dao.DataAccessException Executes the given data access operation defined by theGemfireCallbackin the context of Apache Geode.- Specified by:
executein interfaceGemfireOperations- Type Parameters:
T-typereturned by theGemfireCallback.- Parameters:
action-GemfireCallbackobject defining the Apache Geode action to execute; must not be null.- Returns:
- the result of executing the
GemfireCallback. - Throws:
org.springframework.dao.DataAccessException- if an Apache Geode error is thrown by a data access operation.IllegalArgumentException- ifGemfireCallbackis null.- See Also:
-
execute
public <T> T execute(@NonNull GemfireCallback<T> action, boolean exposeNativeRegion) throws org.springframework.dao.DataAccessException Executes the given data access operation defined by theGemfireCallbackin the context of Apache Geode.- Specified by:
executein interfaceGemfireOperations- Type Parameters:
T-typereturned by theGemfireCallback.- Parameters:
action-GemfireCallbackobject defining the Apache Geode action to execute; must not be null.exposeNativeRegion- boolean value indicating whether to pass the nativeRegionor theRegionproxy to theGemfireCallback.- Returns:
- the result of executing the
GemfireCallback. - Throws:
org.springframework.dao.DataAccessException- if an Apache Geode error is thrown by a data access operation.IllegalArgumentException- ifGemfireCallbackis null.- See Also:
-
createRegionProxy
@NonNull protected <K,V> org.apache.geode.cache.Region<K,V> createRegionProxy(@NonNull org.apache.geode.cache.Region<K, V> region) Create a close-suppressing proxy for the given Apache Geode cacheRegion. Called by theexecutemethod.- Type Parameters:
K-typeof theRegionkey.V-typeof theRegionvalue.- Parameters:
region-Regionfor which a proxy will be created.- Returns:
- the Region proxy implementing all interfaces implemented by the passed-in Region object.
- See Also:
-
Region.close()execute(GemfireCallback, boolean)
-