I was trying to do exactly what Ben Ooms suggested and it wasn't working for me.
I hadn't set my scheduled tasks using the annotation, I had implemented a workaround to get them scheduled using an autowired "TaskScheduler" and I wasn't able to find my scheduled tasks in the ScheduledTaskHolder, although it was working.
Turns out I had to declare a Bean like:
@Configuration
class SchedulerConfiguration {
@Bean
fun threadPoolTaskScheduler(): TaskScheduler {
val scheduler = ThreadPoolTaskScheduler()
scheduler.poolSize = 20
return scheduler
}
}
And schedule my tasks using an interface: SchedulingConfigurer
:
class MySchedulers(
private val myService: MyService,
@Qualifier("threadPoolTaskScheduler") private val threadPoolTaskScheduler: TaskScheduler
) : SchedulingConfigurer {
private val logger = Logger(this::class.java)
private val cronExpression = "0 0 10 * * *"
override fun configureTasks(taskRegistrar: ScheduledTaskRegistrar) {
taskRegistrar.setTaskScheduler(threadPoolTaskScheduler)
logger.info("Setting scheduler cron=$cronExpression")
taskRegistrar.addCronTask(CronTask({ runMyTask() }, CronTrigger(cronExpression)))
}
}
private fun runMyTask() {
myService.update()
}
}
And then I could successfully find my scheduled tasks using ScheduledTaskHolder as suggested by Ben Ooms.