spring-bootevent-listener

TransactionalEventListener is not firing even with fallbackExecution set to true


I have a local library that is integrated as a maven dependency in my project. The library publishes an event and listen to it by itself.

Code where the event is being published:

public void send(String pushDestination, String message) {
    MessagePush msgPush = new MessagePush(pushDestination, Collections.singletonList(message));
    this.applicationEventPublisher.publishEvent(msgPush);
}

Following is the piece of code where the library is listening to the event:

@TransactionalEventListener(
    phase = TransactionPhase.AFTER_COMMIT,
    fallbackExecution = true
)
public <T> void transactionEventListener(MessagePush msgPush) throws PushException {
    // Some logging here
    this.push(msgPush);
}

So how the code is expected to work is that the code in my project should call send method which will publish an event with MessagePush. That event should be listened by transactionEventListener and should call push in-turn.

In my code the send method is not called with in transaction boundaries. But though the flag fallbackExecution is set in the TransactionalEventListener annotation, still the method transactionEventListener is not fired.

To dig a bit what I did is that I created a listener in my project that listens to the library event and that is working perfectly fine:

@EventListener
public void onMessageReceive(MessagePush msgPush) {
    log.info("Message received inside DemoEventListener: {}", new Gson().toJson(msgPush));
}

Is there a way to effectively debug this problem.


Solution

  • It turned out that the problem lied in my project's setup. The application didn't had transaction management in the active spring context at all.

    Since TransactionalEventListener is part of spring transactions, transaction management should be available in the context to make it working.

    Adding following dependency in my pom solve the problem:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
    </dependency>