I'm trying to utilize the @DomainEvents
mechanism provided by Spring Data to publish Events with Spring Cloud Stream (Spring Boot 2.0.0.M7 and Finchley.M5). I have a two-part question.
- Why does
SendTo
not work onEventListeners
? - Is there a better way to accomplish this?
The DomainEvent
is being created and sent to the EventListener
without issues. The problem is that the SendTo
mechanism didn't seem to be working. The first method below would trigger, but not forward the message. Manually building the Message
and sending it as shown in the second method works correctly.
@Async
@TransactionalEventListener
@SendTo(Sink.Output)
StreamedEvent handleEventWithSendTo(MyEvent event) {
// handle and create event
}
@TransactionalEventListener
void handleEvent(MyEvent event) {
// handle and create event
sink.output().send(MessageBuilder.withPayload(payload).build())
}
The call-out in the Spring Cloud Stream docs shows using SendTo
on a StreamListener
, which is not quite the same thing as an EventListener
, but I thought it may work.
For the second part, using DomainEvents
requires the service code persisting the Entity
to know about the event (to either call registerEvent
directly or some method on the Entity
which represents the event). I was curious if using the Spring Data callback hooks (e.g. PreUpdate
, PostUpdate
) would be better. Or if there was a better way all together.