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

后端 未结 2 1584
孤城傲影
孤城傲影 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: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 schedules;
    
            @Bean
            public List schedules() {
                return this.schedules;
            }
    
            public List getSchedules() {
                return schedules;
            }
    
            public void setSchedules(List 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();
        }
    }
    

提交回复
热议问题