Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish event for @TransactionalEventListener with Spring WebFlux #27515

Closed
hoangshiva opened this issue Oct 3, 2021 · 2 comments
Closed

Publish event for @TransactionalEventListener with Spring WebFlux #27515

hoangshiva opened this issue Oct 3, 2021 · 2 comments
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@hoangshiva
Copy link

hoangshiva commented Oct 3, 2021

Affects: <Spring Framework 5.3.10>


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?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 3, 2021
@rstoyanchev rstoyanchev added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Nov 8, 2021
@jhoeller jhoeller changed the title Publish event after transaction completed in Spring webflux Publish event for @TransactionalEventListener with Spring WebFlux Aug 1, 2023
@jhoeller jhoeller self-assigned this Aug 1, 2023
@jhoeller jhoeller added in: data Issues in data modules (jdbc, orm, oxm, tx) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Aug 1, 2023
@jhoeller jhoeller added this to the 6.1.0-M4 milestone Aug 1, 2023
@jhoeller
Copy link
Contributor

jhoeller commented 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")

@koenpunt
Copy link
Contributor

koenpunt commented Aug 1, 2023

It's also possible to use the TransactionSynchronizationManager to perform some code when a transaction finishes.

For example in kotlin:

@Transactional
suspend fun performSomething() {

  userRepository.register()

  val manager = TransactionSynchronizationManager.forCurrentTransaction().awaitSingle()
  
  manager.registerSynchronization(object : TransactionSynchronization {
      override fun afterCommit(): Mono<Void> = mono {
          sendEmail()
      }
  })
}

Or similar in Java:

@Transactional
public void performSomething() {

    userRepository.register();

    TransactionSynchronization transactionSynchronization =
            TransactionSynchronizationManager.forCurrentTransaction();

    if (transactionSynchronization != null) {
        transactionSynchronization.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                sendEmail();
            }
        });
    }
}

@jhoeller jhoeller changed the title Publish event for @TransactionalEventListener with Spring WebFlux Publish event for @TransactionalEventListener with Spring WebFlux Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants