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.
NOTE: Transactional event listeners only work with thread-bound transactions
managed by a PlatformTransactionManager
. A reactive transaction managed by a
ReactiveTransactionManager
uses the Reactor context instead of thread-local variables, so from the perspective of
an event listener, there is no compatible active transaction that it can participate in.
- 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
.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
. -
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:
-