除假期外,用调度程序执行spring job

时间:2017-10-26 00:36:54

标签: spring spring-mvc spring-boot spring-data spring-batch

我有一个使用@EnableScheduling配置的java作业,我可以使用注释运行作业

@Scheduled(cron = "${job1.outgoingCron:0 45 15,18,20 * * MON-FRI}", zone = "America/New_York")
public void Process() {

}

但我不想在美国假期开展这项工作,更新时间表的最佳方法是什么。

1 个答案:

答案 0 :(得分:0)

尝试使用CronTrigger或自定义Trigger设置时间表explicitly,例如:

@SpringBootApplication
@EnableScheduling
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @Bean
    public TaskScheduler taskScheduler() {
        return new ThreadPoolTaskScheduler();
    }
}

@Component
@RequiredArgsConstructor // Lombok annotation
public class StartUp implements ApplicationRunner {

    @NonNull private final TaskScheduler scheduler;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        // Variant 1 
        scheduler.schedule(this::myTask, new CronTrigger(/* your schedule */));

        // Variant 2
        scheduler.schedule(this::myTask, this::myTriger);
    }

    private void myTask() {
        //...
    }

    private Date myTrigger(TriggerContext triggerContext) {
       //...
    }
}