Spring Boot : Getting @Scheduled cron value from database

前端 未结 4 1692
再見小時候
再見小時候 2020-12-09 09:16

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

4条回答
  •  既然无缘
    2020-12-09 09:50

    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.

提交回复
热议问题