r/SpringBoot • • 8h 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:

  1. Is there a cleaner Spring-native hook that guarantees the web server is actually accepting connections before I start consuming?
  2. Have others seen this in production, or is it mostly a local-dev timing issue?
  3. Should the consumer lifecycle be tied to the WebServer instead 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.

3 Upvotes

6 comments sorted by

•

u/This_Link881 3h ago

I think this behavior of spring is correct, the consumer layer (component) is independent of the web layer. You can create a spring boot application without the mvc starter using just a mq starter lib. What would you like to achieve with your app? Do you have a websocket connection to send data by consumed messages?

•

u/sereikis 2h ago

in a standard Boot startup, WebServerInitializedEvent precedes ApplicationReadyEvent, so a connector still being unbound needs a reproducer rather than an extra delay. log the event's context ID, server port and callback URL to check you're observing the same server, then compare loopback with the externally routed address; a listening local port doesn't guarantee load-balancer readiness.

•

u/bikeram 7h ago

What exactly are you trying to do?

For a message queue I would create a component that extends SmartLifecycle.

That gives you easy methods to override for start(), but more importantly stop()

•

u/koffiegorilla 1h ago

You can create a custom ApplicationStartedEvent that extends ApplicationEvent.
The return from run in main is an ApplicationContext.
You can then publish your custom event.

public static void main(String[] args) {
    var ctx = SpringApplication.run(MyApplication.class, args);
    ctx.publishEvent(new ApplicationStartedEvent(ctx));
}

•

u/koffiegorilla 1h ago

If you need, you can use ctx to obtain the WebServer and loop until isStarted.

•

u/Paw565 24m ago

Why don't you just use ApplicationRunner?