Package org.springframework.geode.cache
Class RepositoryAsyncEventListener<T,ID>
java.lang.Object
org.springframework.geode.cache.RepositoryAsyncEventListener<T,ID>
- All Implemented Interfaces:
org.apache.geode.cache.asyncqueue.AsyncEventListener
,org.apache.geode.cache.CacheCallback
,org.apache.geode.cache.Declarable
public class RepositoryAsyncEventListener<T,ID>
extends Object
implements org.apache.geode.cache.asyncqueue.AsyncEventListener
An Apache Geode
AsyncEventListener
that uses a Spring Data CrudRepository
to perform
data access operations to a backend, external data source asynchronously, triggered by cache operations.- Since:
- 1.4.0
- See Also:
-
Function
Operation
AsyncEvent
AsyncEventListener
CrudRepository
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
RepositoryAsyncEventListener.AbstractAsyncEventOperationRepositoryFunction
is an abstract base class implementing theRepositoryAsyncEventListener.AsyncEventOperationRepositoryFunction
interface to provided a default template implementation of theFunction.apply(Object)
method.static class
RepositoryAsyncEventListener.AsyncEventError
is a wrapper class encapsulating theAsyncEvent
along with theerror
that was thrown while processing the event.static interface
TheRepositoryAsyncEventListener.AsyncEventErrorHandler
interface is aFunction
andFunctionalInterface
used to handle errors while processingAsyncEvents
.static interface
TheRepositoryAsyncEventListener.AsyncEventOperationRepositoryFunction
interface is aFunction
andFunctionalInterface
that translates theAsyncEvent
Operation
into a Spring DataCrudRepository
method invocation.static class
AnRepositoryAsyncEventListener.AsyncEventOperationRepositoryFunction
capable of handlingOperation.CREATE
andOperation.UPDATE
AsyncEvents
.static class
-
Field Summary
Modifier and TypeFieldDescriptionprotected static final RepositoryAsyncEventListener.AsyncEventErrorHandler
-
Constructor Summary
ConstructorDescriptionRepositoryAsyncEventListener
(org.springframework.data.repository.CrudRepository<T, ID> repository) Constructs a new instance ofRepositoryAsyncEventListener
initialized with the given Spring DataCrudRepository
. -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
doProcessEvents
(List<org.apache.geode.cache.asyncqueue.AsyncEvent> events) Gets the configuredRepositoryAsyncEventListener.AsyncEventErrorHandler
used to handle errors that may occur when this listener is invoked with a batch ofAsyncEvents
.long
Determines how many times this listener has been fired (triggered) by the GemFire/Geode AEQ system.Gets a reference to the configured Spring DataCrudRepository
used by thisAsyncEventListener
to perform data access operations to a external, backend data source asynchronously when triggered by a cache operation.Gets aList
ofRepositoryAsyncEventListener.AsyncEventOperationRepositoryFunction
objects used to processAsyncEvents
passed to this listener by inspecting theOperation
on theAsyncEvent
and calling the appropriateCrudRepository
method.boolean
hasFired()
Determines whether this listener has (ever) been fired (triggered) by the GemFire/Geode AEQ system.boolean
Determines whether this listener has been fired (triggered) by the GemFire/Geode AEQ system since the last check.final boolean
processEvents
(List<org.apache.geode.cache.asyncqueue.AsyncEvent> events) Processes eachAsyncEvent
in order by first determining whether theAsyncEvent
can be processed by this listener and then invokes the appropriate Spring DataCrudRepository
data access operation corresponding to theAsyncEvent
Operation
.boolean
register
(RepositoryAsyncEventListener.AsyncEventOperationRepositoryFunction<T, ID> repositoryFunction) Registers aRepositoryAsyncEventListener.AsyncEventOperationRepositoryFunction
capable of processingAsyncEvents
byOperation
and invoking the appropriate Spring DataCrudRepository
data access operation.void
setAsyncEventErrorHandler
(RepositoryAsyncEventListener.AsyncEventErrorHandler asyncEventErrorHandler) Configures anRepositoryAsyncEventListener.AsyncEventErrorHandler
to handle errors that may occur when this listener is invoked with a batch ofAsyncEvents
.boolean
unregister
(RepositoryAsyncEventListener.AsyncEventOperationRepositoryFunction<T, ID> repositoryFunction) Unregisters the givenRepositoryAsyncEventListener.AsyncEventOperationRepositoryFunction
from this listener.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.apache.geode.cache.CacheCallback
close
Methods inherited from interface org.apache.geode.cache.Declarable
init, initialize
-
Field Details
-
DEFAULT_ASYNC_EVENT_ERROR_HANDLER
protected static final RepositoryAsyncEventListener.AsyncEventErrorHandler DEFAULT_ASYNC_EVENT_ERROR_HANDLER
-
-
Constructor Details
-
RepositoryAsyncEventListener
public RepositoryAsyncEventListener(@NonNull org.springframework.data.repository.CrudRepository<T, ID> repository) Constructs a new instance ofRepositoryAsyncEventListener
initialized with the given Spring DataCrudRepository
.- Parameters:
repository
- Spring DataCrudRepository
used to perform data access operations to a backend, external data source when triggered by a cache operation; must not be null.- Throws:
IllegalArgumentException
- ifCrudRepository
is null.- See Also:
-
CrudRepository
-
-
Method Details
-
hasFired
public boolean hasFired()Determines whether this listener has (ever) been fired (triggered) by the GemFire/Geode AEQ system.- Returns:
- a boolean value indicating whether this listener has been fired (triggered).
- See Also:
-
hasFiredSinceLastCheck
public boolean hasFiredSinceLastCheck()Determines whether this listener has been fired (triggered) by the GemFire/Geode AEQ system since the last check. A call to this method clears the flag.- Returns:
- a boolean value indicating whether this listener has been fired (triggered) since the last check.
- See Also:
-
getFiredCount
public long getFiredCount()Determines how many times this listener has been fired (triggered) by the GemFire/Geode AEQ system.- Returns:
- a
Long
value indicating how many times this listener has been fired (triggered).
-
setAsyncEventErrorHandler
public void setAsyncEventErrorHandler(@Nullable RepositoryAsyncEventListener.AsyncEventErrorHandler asyncEventErrorHandler) Configures anRepositoryAsyncEventListener.AsyncEventErrorHandler
to handle errors that may occur when this listener is invoked with a batch ofAsyncEvents
. Since the processing ofAsyncEvents
is asynchronous, theRepositoryAsyncEventListener.AsyncEventErrorHandler
gives users the opportunity to respond to errors for eachAsyncEvent
as it is is processed given this listener is designed to coordinate data/state changes occurring in an Apache Geode cache with an external data source.- Parameters:
asyncEventErrorHandler
-RepositoryAsyncEventListener.AsyncEventErrorHandler
used to handle errors while processing the batch ofAsyncEvents
.- See Also:
-
getAsyncEventErrorHandler
Gets the configuredRepositoryAsyncEventListener.AsyncEventErrorHandler
used to handle errors that may occur when this listener is invoked with a batch ofAsyncEvents
. Defaults to anRepositoryAsyncEventListener.AsyncEventErrorHandler
that always returns false on any error.- Returns:
- the configured
RepositoryAsyncEventListener.AsyncEventErrorHandler
; never null. - See Also:
-
getRepository
Gets a reference to the configured Spring DataCrudRepository
used by thisAsyncEventListener
to perform data access operations to a external, backend data source asynchronously when triggered by a cache operation.- Returns:
- a reference to the configured Spring Data
CrudRepository
; never null. - See Also:
-
CrudRepository
-
getRepositoryFunctions
@NonNull protected List<RepositoryAsyncEventListener.AsyncEventOperationRepositoryFunction<T,ID>> getRepositoryFunctions()Gets aList
ofRepositoryAsyncEventListener.AsyncEventOperationRepositoryFunction
objects used to processAsyncEvents
passed to this listener by inspecting theOperation
on theAsyncEvent
and calling the appropriateCrudRepository
method.- Returns:
- a
List
ofRepositoryAsyncEventListener.AsyncEventOperationRepositoryFunction
objects to process theAsyncEvents
; never null. - See Also:
-
processEvents
Processes eachAsyncEvent
in order by first determining whether theAsyncEvent
can be processed by this listener and then invokes the appropriate Spring DataCrudRepository
data access operation corresponding to theAsyncEvent
Operation
.- Specified by:
processEvents
in interfaceorg.apache.geode.cache.asyncqueue.AsyncEventListener
- Parameters:
events
-List
ofAsyncEvents
to process.- Returns:
- a boolean value indicating whether all
AsyncEvents
were processed successfully by this listener. If anyAsyncEvent
fails to be processed (just one), then this method will return false. If anyAsyncEvent
cannot be handled, then this method will return false, even if otherAsyncEvents
were successfully processed. - See Also:
-
doProcessEvents
- See Also:
-
register
public boolean register(@NonNull RepositoryAsyncEventListener.AsyncEventOperationRepositoryFunction<T, ID> repositoryFunction) Registers aRepositoryAsyncEventListener.AsyncEventOperationRepositoryFunction
capable of processingAsyncEvents
byOperation
and invoking the appropriate Spring DataCrudRepository
data access operation.AsyncEventOperationRepositoryFunctions
can be registered forAsyncEvent
Operations
not currently handled by this listener. Alternatively, users can override existingAsyncEventOperationRepositoryFunctions
provided by this listener to alter the default behavior, or effectively the Spring DataCrudRepository
data access operation invoked based on theAsyncEvent
Operation
. TherepositoryFunction
arguments are prepended to theList
of registeredFunctions
to implement the override, where the firstFunction
found capable of handling theAsyncEvent
Operation
will be applied.- Parameters:
repositoryFunction
-RepositoryAsyncEventListener.AsyncEventOperationRepositoryFunction
used to processAsyncEvents
byOperation
invoking the appropriate Spring DataCrudRepository
data access operation; must not be null.- Returns:
- a boolean value indicating whether the registration was successful.
- See Also:
-
unregister
public boolean unregister(@Nullable RepositoryAsyncEventListener.AsyncEventOperationRepositoryFunction<T, ID> repositoryFunction) Unregisters the givenRepositoryAsyncEventListener.AsyncEventOperationRepositoryFunction
from this listener.- Parameters:
repositoryFunction
-RepositoryAsyncEventListener.AsyncEventOperationRepositoryFunction
to unregister.- Returns:
- a boolean value indicating whether the un-registration was successful.
- See Also:
-