Spring Boot : Getting @Scheduled cron value from database

前端 未结 4 1687
再見小時候
再見小時候 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:57

    you can add a bean to get cron value from database in the SpringBootApplication main class or in any of the configuration class. Example code is below:

    @Autowired
    private CronRepository cronRepo;
    
    @Bean
    public int getCronValue()
    {
        return cronRepo.findOne("cron").getCronValue();
    }
    

    you should create a table and provide suitable values in the database. After that you can provide the bean inside the @Scheduled. Example code is below:

    @Scheduled(cron="#{@getCronValue}")
    

    Hope it works for your issue.

提交回复
热议问题