Interface TransactionalApplicationListener<E extends ApplicationEvent>
- Type Parameters:
E
- the specificApplicationEvent
subclass to listen to
- All Superinterfaces:
ApplicationListener<E>
,EventListener
,Ordered
- All Known Implementing Classes:
TransactionalApplicationListenerAdapter
,TransactionalApplicationListenerMethodAdapter
ApplicationListener
that is invoked according to a TransactionPhase
.
This is a programmatic equivalent of the TransactionalEventListener
annotation.
Adding Ordered
to your listener implementation
allows you to prioritize that listener amongst other listeners running before or after
transaction completion.
As of 6.1, transactional event listeners can work with thread-bound transactions managed
by a PlatformTransactionManager
as well as reactive
transactions managed by a ReactiveTransactionManager
.
For the former, listeners are guaranteed to see the current thread-bound transaction.
Since the latter uses the Reactor context instead of thread-local variables, the transaction
context needs to be included in the published event instance as the event source:
see TransactionalEventPublisher
.
- Since:
- 5.3
- Author:
- Juergen Hoeller, Oliver Drotbohm
- See Also:
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
Callback to be invoked on synchronization-driven event processing, wrapping the target listener invocation (processEvent(E)
). -
Field Summary
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Add a callback to be invoked on processing within transaction synchronization, i.e.static <T> TransactionalApplicationListener<PayloadApplicationEvent<T>>
forPayload
(Consumer<T> consumer) Create a newTransactionalApplicationListener
for the given payload consumer, to be applied in the default phaseTransactionPhase.AFTER_COMMIT
.static <T> TransactionalApplicationListener<PayloadApplicationEvent<T>>
forPayload
(TransactionPhase phase, Consumer<T> consumer) Create a newTransactionalApplicationListener
for the given payload consumer.default String
Return an identifier for the listener to be able to refer to it individually.default int
getOrder()
Return the execution order within transaction synchronizations.default TransactionPhase
Return theTransactionPhase
in which the listener will be invoked.void
processEvent
(E event) Immediately process the givenApplicationEvent
.default boolean
Transaction-synchronized listeners do not support asynchronous execution, only their target listener (processEvent(E)
) potentially does.Methods inherited from interface org.springframework.context.ApplicationListener
onApplicationEvent
-
Method Details
-
getOrder
default int getOrder()Return the execution order within transaction synchronizations.Default is
Ordered.LOWEST_PRECEDENCE
. -
supportsAsyncExecution
default boolean supportsAsyncExecution()Transaction-synchronized listeners do not support asynchronous execution, only their target listener (processEvent(E)
) potentially does.- Specified by:
supportsAsyncExecution
in interfaceApplicationListener<E extends ApplicationEvent>
- Returns:
true
if this listener instance can be executed asynchronously depending on the multicaster configuration (the default), orfalse
if it needs to immediately run within the original thread which published the event- Since:
- 6.1
- See Also:
-
getListenerId
Return an identifier for the listener to be able to refer to it individually.It might be necessary for specific completion callback implementations to provide a specific id, whereas for other scenarios an empty String (as the common default value) is acceptable as well.
-
getTransactionPhase
Return theTransactionPhase
in which the listener will be invoked.The default phase is
TransactionPhase.AFTER_COMMIT
. -
addCallback
Add a callback to be invoked on processing within transaction synchronization, i.e. whenprocessEvent(E)
is being triggered during actual transactions.- Parameters:
callback
- the synchronization callback to apply
-
processEvent
Immediately process the givenApplicationEvent
. In contrast toApplicationListener.onApplicationEvent(ApplicationEvent)
, a call to this method will directly process the given event without deferring it to the associatedtransaction phase
.- Parameters:
event
- the event to process through the target listener implementation
-
forPayload
static <T> TransactionalApplicationListener<PayloadApplicationEvent<T>> forPayload(Consumer<T> consumer) Create a newTransactionalApplicationListener
for the given payload consumer, to be applied in the default phaseTransactionPhase.AFTER_COMMIT
.- Type Parameters:
T
- the type of the event payload- Parameters:
consumer
- the event payload consumer- Returns:
- a corresponding
TransactionalApplicationListener
instance - See Also:
-
forPayload
static <T> TransactionalApplicationListener<PayloadApplicationEvent<T>> forPayload(TransactionPhase phase, Consumer<T> consumer) Create a newTransactionalApplicationListener
for the given payload consumer.- Type Parameters:
T
- the type of the event payload- Parameters:
phase
- the transaction phase in which to invoke the listenerconsumer
- the event payload consumer- Returns:
- a corresponding
TransactionalApplicationListener
instance - See Also:
-