当前位置: 首页 > 面试题库 >

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

金旺
2023-03-14
问题内容

我喜欢@Scheduled使用.yml文件的不同配置属性来实现一项作业。

在我的yaml文件中,我将其描述cron expression为列表:

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

我使用Configuration读取了这些值,并创建了一个@Beannamed scheduled

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

在我的Job类中,我想开始执行一种方法,但是要执行配置中的两个计划。

 @Scheduled(cron = "#{@schedules}")
 public String execute() {
     System.out.println(converterService.test());
     return "success";
 }

使用此解决方案,应用程序将创建错误:(或多或少清晰)

Encountered invalid @Scheduled method 'execute': Cron expression must consist of 6 fields (found 12 in "[10 * * * * *, 20 * * * * *]")

是否可以使用多个cron表达式声明来配置相同的计划作业方法?

编辑1

经过一番尝试后,我只是在executer方法上使用了第二个注释。

@Scheduled(cron = "#{@schedules[0]}")
@Scheduled(cron = "#{@schedules[1]}")
public String execute() {
    System.out.println(converterService.test());
    return "success";
}

此解决方案有效,但并非真正动态。有没有办法使它动态?


问题答案:

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

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

cronjob.yaml

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

执行 MyTask 的实际任务:

package hello;

import org.springframework.stereotype.Component;

@Component
public 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;

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

上下文/主类 应用程序

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


 类似资料:
  • 我使用Spring-Boot@Scheduled-Cron缓存从持久性存储中检索到的数据。 我有两个不同的任务, 在缓存中设置结果 清除缓存 Task1将每15分钟运行一次。我设置了cron-like 所以频率应该是 12: 00:00 12:15:00 12:30:00 现在,我想在Task1之前10秒运行Task2 ie 11: 59:50 12:14:50 12:29:50 我在尝试这个表达

  • 我想调度多个任务使用@调度注释使用cron表达式。我有三项工作需要在固定时间执行。例如,作业-1被安排在每天晚上11点,作业-2被安排在每天早上7点到晚上9点,间隔1小时,作业-3被安排在每1小时。所有3个计划任务都是同一应用程序的一部分。 我也尝试过同样的方法,但所有三个调度都没有发生。我的应用程序是SpringBoot应用程序。我不是新的调度。请帮帮我。下面是他我的方法 application

  • 我是Spring调度器的新手。我读了很多关于@ScheduledExecutorService和TimerTask的文章。 因此,据我所知,@ScheduledExecutorService和ScheduledExecutorService的功能大部分是相同的,但如果您的代码是在spring中,那么最好在代码中使用@ScheduledExecutorService。 所以我的问题是,假设我想在15

  • 我的路线计划每月在凌晨1:00运行一次,如果我的应用程序在这段时间内关闭,就会出现问题,所以我想在应用程序启动时运行该作业。 它使用时间段(如果服务重新启动,作业将运行),但不使用cron表达式。 我试过使用以下方法,但没有成功。 从(“quartz2://scheduler?cron=0 45 15 1 1/1?”? 有人能让我知道,如果我错过了什么。

  • 我使用石英调度和Spring Batch,我需要在每个月的最后一个星期四运行一个特定的工作。 有可能创建这样的Quartz cron表达式吗? 谢谢

  • 我把这个要点用在我的文档中:https://gist.github.com/httpNick/708219f5002b6a9dc578 它对ES6 Babelfiying非常有效,但我想查看我的系统中的所有文件/public/js文件夹,并将生成版本输出到生成文件夹。有没有办法修改这个gulpfile来查看文件夹中的所有js文件? 从要点复制粘贴:

  • 我想安排一个任务在每个月的最后一天上午10:10运行。cron表达式是