Search code examples
springspring-bootspring-scheduled

Weird spring boot scheduler behavior - don't work with lazy initialization


I have bizarre scheduler behavior in my spring boot application. I run SB 2.6.7 and java 17. My code

@Configuration
@EnableScheduling
public class SchedulingConfigurerConfiguration implements SchedulingConfigurer {
    
    @Value("${schedule.pool}")
    private int DYNO_CORES;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        var taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(DYNO_CORES * 4);
        taskScheduler.initialize();
        taskRegistrar.setTaskScheduler(taskScheduler);
    }

       
    @Bean
    SimpleAsyncTaskExecutor executor() {
        var executor = new SimpleAsyncTaskExecutor("bg_tasks_");
        executor.setConcurrencyLimit(UNBOUNDED_CONCURRENCY);//these jobs are mainly IO-bound, so let's not set a limit for now
        return executor;
    }
}

And my class with one task.

@Slf4j
@Service
public class NoPrimaryServiceTasks {
    @Scheduled(fixedDelay = 1000, initialDelay = 1000 )
    public void checkBacklogSize() {
        log.info("logzzz");
    }
}

And it doesn't work. But when I paste context.getBean(NoPrimaryServiceTasks.class); in main method everything works. How to solve it without this workaround?

org.springframework.boot.autoconfigure.task.ScheduledBeanLazyInitializationExcludeFilter - also this class doesn't work


Solution

  • someone added into our codebase this code :)

    for ( String name : beanFactory.getBeanDefinitionNames() ) {
        beanFactory.getBeanDefinition( name ).setLazyInit( true );
            }