I got a class that sends out an application event. The receiver must not miss this event, so the sender is dependent on the receiver.
@Service
@DependsOn("receiver")
class Sender {
...
@PostConstruct
public void init(){
applicationEventPublisher.publishEvent(new MyEvent());
}
}
@Service
class Receiver {
...
@EventListener
public void onEvent(MyEvent event) {
System.out.println("Event catched");
}
}
In debugging mode you are able to see that Sender
is initialised after Receiver
, which should result in the Receiver always catching the event of the Sender - but it isn't.
In fact there seems to be a delay between initialising the Receiver and the point it is ready to receive events. If I delay publishing the event in the Sender by a few milliseconds, the receiver catches it as expected.
So it seems like the @DependsOn
doesn't make it entirely sure that the Seceiver is fully initialised before the Sender, which is the exact opposite of what is documented.
How to accomplish that the receiver catches the event without using any ugly delays?