Description I have a service MyServiceImpl that listen to the ApplicationReadyEvent In the method doStuff I run a while(true) - run forever (In the original app I run a (infinite) sink that is getting data from a rest endpoint and publish it to kafka)
In spring boot 2.3.1 the actuator endpoint status for readiness is OUT_OF_SERVICE because the listener doesn't finish In spring boot 2.2.8 the actuator endpoint status for readiness is UP
spring-boot or spring-actuator problem
-
Start the app with spring boot 2.3.1 mvn spring-but:run -access the endpoint for readiness http://localhost:8080/.mystatus/ready -The health is "status": "OUT_OF_SERVICE"
-
Start the app with spring boot 2.2.8 mvn spring-but:run access the endpoint for readiness http://localhost:8080/.mystatus/ready The health is "status": "UP"
```@Service public class MyServiceImpl { @EventListener(ApplicationReadyEvent.class) public void doStuff(){ int count = 0; while(true){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(count++); } } }
Comment From: bclozel
This expected behavior and a combination of two things.
First, the new liveness and readiness support implies new application events. The event changing the readiness state once the app has started is fired after the event you’re relying on: https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-application-events-and-listeners
Now that event is never fired and the readiness state never changes as you’re locking the event publishing thread with the processing. It’s generally bad practice to process long running tasks while handling an application event. You should in most cases kick off that processing in a worker thread/executor.
In this very case, Spring Boot is providing an ApplicationRunner interface you can implement. Such components will be called at startup (and can be ordered). This allows you to keep the application lifecycle consistent with what you’re trying to achieve. See https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-kubernetes-probes-lifecycle
Let us know if updating your application with such a change has the expected result.
Thanks!
Comment From: laurentiu-miu
Ok, you are right it is not a bug it is a feature. 10x for help and sorry for raising this issue.
Comment From: bclozel
No worries!
I've reviewed the documentation and I think we could add a note in the application events section that points to the ApplicationRunner
infrastructure; the Spring Framework reference docs itself doesn't clearly make the point about long-running tasks on the event publishing thread so we might improve things there as well.
I'm turning this issue into a documentation improvement. Thanks!