Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to perform tasks after application startup #22100

Closed
laurentiu-miu opened this issue Jun 25, 2020 · 3 comments
Closed

Document how to perform tasks after application startup #22100

laurentiu-miu opened this issue Jun 25, 2020 · 3 comments
Assignees
Labels
type: documentation A documentation update
Milestone

Comments

@laurentiu-miu
Copy link

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

git example

  1. 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"

  2. 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"

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++);
    }
  }
}

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 25, 2020
@bclozel
Copy link
Member

bclozel commented Jun 25, 2020

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!

@bclozel bclozel added the status: waiting-for-feedback We need additional information before we can continue label Jun 25, 2020
@laurentiu-miu
Copy link
Author

Ok, you are right it is not a bug it is a feature.
10x for help and sorry for raising this issue.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jun 25, 2020
@bclozel bclozel added type: documentation A documentation update and removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged labels Jun 25, 2020
@bclozel bclozel self-assigned this Jun 25, 2020
@bclozel bclozel added this to the 2.3.x milestone Jun 25, 2020
@bclozel
Copy link
Member

bclozel commented Jun 25, 2020

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!

@bclozel bclozel changed the title spring boot 2.3.1 and 2.2.8 actuator endpoints behaves differently Document how to perform tasks after application startup Jun 25, 2020
@snicoll snicoll self-assigned this Aug 31, 2020
@snicoll snicoll modified the milestones: 2.3.x, 2.3.4 Aug 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

4 participants