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

@Scheduled method not triggered when spring.main.lazy-initialization=true #25315

Closed
arikanorh opened this issue Feb 13, 2021 · 4 comments
Closed
Assignees
Labels
type: bug A general bug
Milestone

Comments

@arikanorh
Copy link

Spring Boot Version: 2.4.2
Spring Framework Version: 5.3.3

  1. @EnableScheduling
  2. Create a @Component with a @Scheduled method
  3. Add spring.main.lazy-initialization=true to application.properties
  4. Start application

The method is never called.

@snicoll snicoll transferred this issue from spring-projects/spring-boot Feb 14, 2021
@sbrannen sbrannen changed the title Scheduled function doesnt get trigger when spring.main.lazy-initialization=true @Scheduled function not triggered when spring.main.lazy-initialization=true Feb 16, 2021
@sbrannen
Copy link
Member

I've edited your comment to improve the formatting. You might want to check out this Mastering Markdown guide for future reference.

@sbrannen sbrannen changed the title @Scheduled function not triggered when spring.main.lazy-initialization=true @Scheduled method not triggered when spring.main.lazy-initialization=true Feb 16, 2021
@snicoll
Copy link
Member

snicoll commented Feb 16, 2021

Moving it back to Spring Boot as @wilkinsona reminded me we have dedicated logic in other areas to prevent that from happening.

@snicoll snicoll transferred this issue from spring-projects/spring-framework Feb 16, 2021
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 16, 2021
@wilkinsona
Copy link
Member

A general-purpose fix for this would require us to mimic some of the logic in ScheduledAnnotationBeanPostProcessor. When scheduling and lazy initialization are enabled, we'd need a LazyInitializationExcludeFilter that excludes any bean with a @Scheduled- or @Schedules-annotated method. That doesn't feel too bad I suppose. The alternative would be to document this as a restriction. Let's see what the rest of the team thinks.

@arikanorh While we're deciding what to do, you should be able to work around the problem with one or more LazyInitializationExcludeFilters of your own. If you know the types that are using @Scheduled then you can do something like this:

@Bean
LazyInitializationExcludeFilter lazyInitializationExcludeFilter() {
	return LazyInitializationExcludeFilter.forBeanTypes(TypeWithScheduledMethods.class);
}

@wilkinsona wilkinsona added the for: team-attention An issue we'd like other members of the team to review label Mar 17, 2021
@philwebb philwebb added type: bug A general bug and removed for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged labels Mar 17, 2021
@philwebb philwebb added this to the 2.4.x milestone Mar 17, 2021
@snicoll snicoll self-assigned this Apr 19, 2021
@snicoll
Copy link
Member

snicoll commented Apr 19, 2021

There are two issues here. The filtering we've described above and the fact our TaskScheduler isn't picked up by Spring Framework when it is flagged lazy (as this is the case for all beans in the context when the option is enabled). We're investigating if an issue in Spring Framework is necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

6 participants