I\'m using Spring Boot
and have issues scheduling a cron task
using values existing in database.
For the time being, I\'m reading values f
Using @Bean annotated method will do the trick. But as SpringBoot only going to call this method 1 time, and return cached version after that, you will need to restart Spring to getting new value.
To get new run time from database, using SchedulingConfigurer:
@Configuration
public class SchedulerConfig implements SchedulingConfigurer {
@Autowired
private YourService yourService;
@Bean
public YourJob yourJob() {
return new YourJob();
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(
() -> yourJob().performJob(),
(TriggerContext triggerContext) -> yourService.getCron()
);
}
}
Note: don't use @Scheduled with this way.