栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用yaml文件中的多个cron表达式来启动一个@Scheduled任务

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用yaml文件中的多个cron表达式来启动一个@Scheduled任务

(编辑,因为我找到了一种执行此操作的方法)

您实际上可以做到这一点。下面我展示了一个工作示例:

cronjob.yaml

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

执行 MyTask 的实际任务:

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

您的 CronConfig 如下:

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;        }    }

ScheduledTask 豆,负责安排所有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;@Componentpublic 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)) );    }}

上下文/主类 应用程序

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@EnableAsyncpublic 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();    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377849.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号