Satya
Satya

Reputation: 358

execute spring job with scheduler except holidays

I have a java job configured with @EnableScheduling and I am able to run job with annotation

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

}

But I don't want to run this job for US holidays what is the best way to update schedule.

Upvotes: 1

Views: 1545

Answers (1)

Cepr0
Cepr0

Reputation: 30474

Try to set your schedule explicitly, using CronTrigger or custom Trigger, for example:

@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) {
       //...
    }
}

Upvotes: 1

Related Questions