evan klinger opened SPR-16325 and commented
I have 3 @Service
beans, each with a void method with @Scheduled
annotations. Only one of these is picked up during startup. I have set log level to trace and even in the trace the ScheduledAnnotationBeanPostProcessor only detects it on one of the beans. The syntax, packages, and everything else are identical. I am using a cron expression with SpringEL to derive the cron value. All have the same cron value. All beans are annotated with @Profile
(that is not active), each has a @PostConstruct
method and one @Scheduled
. I am not sure if this is a Spring bug or what could be causing this? I am registering a taskScheduler bean as follows:
@Bean(destroyMethod = "shutdown")
public Executor taskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(4);
scheduler.setThreadNamePrefix("taskScheduler-");
scheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
scheduler.setErrorHandler(t -> logger.error("Error executing scheduled task", t));
scheduler.initialize();
return scheduler;
}
This is in an @Configuration
class that also implements SchedulingConfigurer and sets the scheduler like so:
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskScheduler());
}
Any help would be appreciated! Thank you
Affects: 5.0.2
Comment From: spring-projects-issues
evan klinger commented
ScheduledAnnotationBeanPostProcessor also doesn't even print that it doesn't find @Scheduled
on those beans. But the beans are instantiated and the @PostConstruct
method is called, so they are being detected.
Comment From: spring-projects-issues
Juergen Hoeller commented
So the profile conditions on those three beans are mutually exclusive? And depending on which profile is active, the @Scheduled
annotations are supposed to be picked up on a specific one of those beans - and this doesn't work in all three cases?
I'm afraid this is a hard to analyze without a repro example.
Comment From: spring-projects-issues
evan klinger commented
Hi Juergen,
The @Profile
is the same on all 3 beans.
I also have a few @Configuration
classes but I don't see anything wrong there. App is run without any active profile.
@Configuration
@EnableAsync
@EnableScheduling
@Profile
("!reservations")
public class SessionConfig implements AsyncConfigurer, SchedulingConfigurer
@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy
(proxyTargetClass = true)
@ComponentScan
(basePackages = { "com.jjk.hub" })
public class CoreConfig implements WebMvcConfigurer
Spring does detect the @Service
classes since the @PostConstruct
runs on them. But the @Scheduled
is never discovered in those classes, except for one of them.
What would cause Spring to not search for @Scheduled
on some beans? Is there a breakpoint I can set somewhere that would indicate why it's not being processed?
Thanks
Evan
Comment From: spring-projects-issues
evan klinger commented
Hi Juergen,
I think I have solved the issue. It appears that the @Configuration
classes were clashing with each other, or somehow there was a cyclic dependency which caused the other beans to not be fully ready when @Scheduled
is being checked. I have moved the beans' dependencies into one @Configuration
and now the @Scheduled
is found and registered correctly.
Thanks!