You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Spring webflux does not support @TransactionalEventListener to publish event after transaction completed. So what i need to do to publish event only when transaction completed in Spring webflux?
changed the title [-]Publish event after transaction completed in Spring webflux[/-][+]Publish event for @TransactionalEventListener with Spring WebFlux[/+]on Aug 1, 2023
As of 6.1, @TransactionalEventListener can work with thread-bound transactions managed by PlatformTransactionManager as well as reactive transactions managed by ReactiveTransactionManager. For the former, as of #30244, 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, e.g.:
TransactionContextManager.currentContext()
.map(source -> new PayloadApplicationEvent<>(source, "myPayload"))
.doOnSuccess(this.eventPublisher::publishEvent)
Or via the new TransactionalEventPublisher delegate:
new TransactionalEventPublisher(this.eventPublisher).publishEvent(source -> new PayloadApplicationEvent<>(source, "myPayload"))
new TransactionalEventPublisher(this.eventPublisher).publishEvent("myPayload")
changed the title [-]Publish event for @TransactionalEventListener with Spring WebFlux[/-][+]Publish event for `@TransactionalEventListener` with Spring WebFlux[/+]on Aug 2, 2023
Activity
[-]Publish event after transaction completed in Spring webflux[/-][+]Publish event for @TransactionalEventListener with Spring WebFlux[/+]jhoeller commentedon Aug 1, 2023
As of 6.1,
@TransactionalEventListener
can work with thread-bound transactions managed byPlatformTransactionManager
as well as reactive transactions managed byReactiveTransactionManager
. For the former, as of #30244, 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, e.g.:Or via the new
TransactionalEventPublisher
delegate:@TransactionalEventListener
only works with non-reactive transactions #25805ApplicationEventPublisher
in a reactive call stack [SPR-16481] #21025koenpunt commentedon Aug 1, 2023
It's also possible to use the
TransactionSynchronizationManager
to perform some code when a transaction finishes.For example in kotlin:
Or similar in Java:
[-]Publish event for @TransactionalEventListener with Spring WebFlux[/-][+]Publish event for `@TransactionalEventListener` with Spring WebFlux[/+]@EventListener
#28343