使用yaml文件中的多个cron表达式Spring-Boot一个@Scheduled任务

时间:2016-12-02 09:49:36

标签: java spring spring-boot cron

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

我的yaml文件中的含义我将cron expression描述为列表:

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

我使用配置读取了这些值,并创建了一个名为@Bean的{​​{1}}:

scheduled

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

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

使用此解决方案,应用程序会产生错误:(或多或少清晰)

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

有没有办法配置具有多个cron表达式声明的相同预定作业方法?

编辑1

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

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

此解决方案有效,但并非真正动态。还有办法使这种动态吗?

2 个答案:

答案 0 :(得分:3)

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

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

<强> cronjob.yaml

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_YEAR, dayOfYear); // Set the day of the year value
String day = cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US); 
// Use Calendar.SHORT for abbreviated names or .LONG for full names
// and be sure to change the locale as necessary

执行 MyTask 的实际任务:

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

您的 CronConfig 原样:

package hello;

import org.springframework.stereotype.Component;

@Component
public class MyTask implements Runnable {

    @Override
    public void run() {
        //complicated stuff
    }
}

负责安排所有crons的 ScheduledTask bean:

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

上下文/主要类应用程序

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

答案 1 :(得分:0)

与之相关的技巧: 在定义玉米作业时间时,属性名称应为小写

例如:如果它是在Camel的情况下,春天没有踢这个工作:( application.yml:

共同:     调度:         feedeErrorLogCleanUp:0 0/5 *? * *

鉴于STUFF工作

共同:     调度:         feedeerrorlogcleanUp:0 0/5 *? * *