Spring-Boot one @Scheduled task using multiple cron expressions from yaml file

后端 未结 2 1585
孤城傲影
孤城傲影 2021-02-11 00:19

I like to have an implementation of one @Scheduled job using different configuration properties of .ymlfile.

Means in my yaml file I describe

相关标签:
2条回答
  • 2021-02-11 00:28

    A trick related to it: while defining the corn job timing, attribute name should be in lower case

    for example: if it is in Camel case, spring did not kick the job :( application.yml:

    common: scheduler: feedeErrorLogCleanUp: 0 0/5 * ? * *

    WHEREAS BELOW STUFF WORKS

    common: scheduler: feedeerrorlogcleanUp: 0 0/5 * ? * *

    0 讨论(0)
  • 2021-02-11 00:39

    (edit since I found a way to perform this)

    You can actually do this. Below I'm showcasing a working example:

    cronjob.yaml

    job:
      schedules:
      - 10 * * * * *
      - 20 * * * * *
    

    the actual task to perform MyTask:

    package hello;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyTask implements Runnable {
    
        @Override
        public void run() {
            //complicated stuff
        }
    }
    

    Your CronConfig as is:

    package hello;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import java.util.List;
    
        @Configuration
        @ConfigurationProperties(prefix="job", locations = "classpath:cronjob.yml")
        public class CronConfig {
    
            private List<String> schedules;
    
            @Bean
            public List<String> schedules() {
                return this.schedules;
            }
    
            public List<String> getSchedules() {
                return schedules;
            }
    
            public void setSchedules(List<String> schedules) {
                this.schedules = schedules;
            }
        }
    

    The ScheduledTask bean that is responsible to schedule all crons:

    package hello;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.scheduling.TaskScheduler;
    import org.springframework.scheduling.support.CronTrigger;
    import org.springframework.stereotype.Component;
    
    @Component
    public class ScheduledTasks {
    
        @Autowired
        private TaskScheduler taskScheduler;
    
        @Autowired
        private CronConfig cronConfig;
    
        @Autowired
        private MyTask myTask;
    
        private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
    
        public void scheduleAllCrons() {
    
            cronConfig.getSchedules().forEach( cron -> taskScheduler.schedule(myTask, new CronTrigger(cron)) );
        }
    }
    

    The context/main class Application:

    package hello;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.Bean;
    import org.springframework.scheduling.TaskScheduler;
    import org.springframework.scheduling.annotation.EnableAsync;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
    
    @SpringBootApplication
    @EnableScheduling
    @EnableAsync
    public class Application {
    
        @Bean
        public TaskScheduler taskScheduler() {
            return new ConcurrentTaskScheduler();
        }
    
    
        public static void main(String[] args) throws Exception {
            ApplicationContext ctx = SpringApplication.run(Application.class);
    
            ScheduledTasks scheduledTasks = ctx.getBean(ScheduledTasks.class);
    
            scheduledTasks.scheduleAllCrons();
        }
    }
    
    0 讨论(0)
提交回复
热议问题