2016-12-02 17 views
2

.ymlという異なる構成プロパティを使用して、1つの@Scheduledジョブを実装したいと思っています。yamlファイルの複数のcron式を使用した@Scheduledタスク

手段私のYAMLファイルで、私はリストとしてcron expressionについて説明します。

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

私はコンフィギュレーションを使用してこれらの値を読み出してscheduledという名前@Beanを作成しました:私の仕事のクラスで

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

私は1つの方法の実行を開始したいが、私の設定の両方のスケジュールの実行を希望する。このソリューションで

@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式の複数の宣言と同じスケジュールされたジョブ方式を設定する方法はありますか?いくつかの後


EDIT 1

は、私はちょうど実行者法上の第2のアノテーションを使用してみてください。

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

このソリューションは機能しますが、実際は動的ではありません。このダイナミックにする方法もありますか?あなたが実際にこれを行うことができます

答えて

2

(私はこれを実行するための方法を見つけたので、編集)

。以下では、実際の例を紹介します:

cronjobYAML

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

あなたの答えをありがとう。はい、クォーツが解決策になるかもしれません。解決策の解決策を使って質問を編集しました。この回避策を構築するための他のソリューションがあるかどうか考えていますか? – Patrick

+0

@Patrick私はちょうどエレガントな解決策を見つけ、完全な実例で答えを修正しました – dimitrisli

0

トリック: コーンジョブタイミングを規定しながら、属性名は小文字でなければならない

例えばキャメルケースの場合、春は仕事を蹴らなかった:( application.yml:

共通: スケジューラ: feedeErrorLogCleanUp:0 0/5 *? * * STUFF BELOW一方

共通

ワークス: スケジューラ: feedeerrorlogcleanUp:0 0/5 *? * *

関連する問題