Search code examples
javaspring-mvcspring-bootscheduled-tasksspring-scheduled

setting different cron params dynamically in java spring @Scheduled


How can I set a my own variable here - "${cron.expression}" ?

This param want final constant. I want to change the values dynamically when program is runing. For example I need to set the LocalDate when this element should execute.

Please write an example , how I can do it.

@Service
    public class ScheduledService {
        @Scheduled(cron = "${cron.expression}")
        public void demoServiceMethod() {
            //do something 
        }

    }

Solution

  • I solved this problem with that way: I will use @Scheduled to check the date from the database. And if today - date that I need - do something... Like that:

    @Scheduled(fixedRate = 86_400_000)// One day
        public void demoServiceMethod() {
    if(Localdate.now().isEqual(//date that I need)){
    //do something
       }
            //do something everyday
     }