package net.libertacasa.pubsh.web; import java.time.Duration; import java.time.Instant; import org.springframework.boot.CommandLineRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.github.kagkarlsson.scheduler.Scheduler; import com.github.kagkarlsson.scheduler.task.Task; import com.github.kagkarlsson.scheduler.task.helper.Tasks; import static com.github.kagkarlsson.scheduler.task.schedule.Schedules.fixedDelay; @Configuration public class SchedulerBean { @Bean Task recurringSampleTask(CounterService counter) { return Tasks .recurring("recurring-sample-task", fixedDelay(Duration.ofMinutes(1))) .execute((instance, ctx) -> { System.out.printf("Recurring testing task. Instance: %s, ctx: %s\n", instance, ctx); CounterService.increase(); }); } @Bean Task sampleOneTimeTask() { return Tasks.oneTime("one-time-sample-task") .execute((instance, ctx) -> { System.out.printf("OK, one-shot testing task.\n"); }); } @Bean CommandLineRunner executeOnStartup(Scheduler scheduler, Task sampleOneTimeTask) { System.out.println("Scheduling one-shot testing task to execute now."); return ignored -> scheduler.schedule( sampleOneTimeTask.instance("command-line-runner"), Instant.now() ); } }