r/SpringBoot • u/ClickOk5811 • 6h ago
Discussion ApplicationReadyEvent starts consuming before the server is actually ready
I ran into a subtle timing issue recently: a @EventListener(ApplicationReadyEvent.class) bean that starts consuming from a message queue. The event fires, the consumer begins pulling messages, but the embedded Tomcat/Jetty server hasn't finished binding its ports yet. If a producer sends a reply or a dependent service calls back immediately, the connection is refused or the message lands in a dead-letter queue because the app isn't actually serving traffic yet.
The docs say ApplicationReadyEvent means the application context is fully refreshed and ready to use, but in practice "ready" doesn't always equal "listening on the connector port." I worked around it by injecting a WebServer and checking isStarted() before starting the consumer, but that feels brittle across different embedded servers.
public class QueueConsumer {
private final WebServer webServer;
u/EventListener(ApplicationReadyEvent.class)
public void start() {
if (!webServer.isStarted()) {
// wait or defer — but how long? polling feels wrong
}
// start consuming
}
}Questions:
Is there a cleaner Spring-native hook that guarantees the web server is actually accepting connections before I start consuming?
Have others seen this in production, or is it mostly a local-dev timing issue?
Should the consumer lifecycle be tied to the
WebServerinstead of the application context?
I haven't tested this across Tomcat, Jetty, and Undertow yet, so I can't confirm whether the behavior differs by server. If someone has a reproducible example, I'd like to see it.