Spring Scheduler

less than 1 minute read

Scheduling with Cron

using @Scheduled annotation on the method

@Component
@Slf4j
@AllArgsConstructor
public class ChronService {
    private final MyService myService;

    @Scheduled(cron="${report.cron-expr}", zone="GMT")
    public void runReport(){
        ...
        myService.callMyServiceAtScheduledTime();
    }
}

Application yml file

create cron expression using https://crontab.guru/

report:
    cron-expr:  0 0/2 * * * ? #run every 2 min
    #0 1 * * 1-7  #At 01:00 AM GMT or 8 PM Central Time on every day-of-week from Monday through Sunday.

Async Scheduling

use the two annotations @EnableAsync and @EnableScheduling at the application level

import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableAsync
@EnableScheduling
public class MemoryIssuesApplication {
    public static void main(String[] args) {
        SpringApplication.run(MemoryIssuesApplication.class, args);
    }
}

Method scheduled to run periodically

//@Scheduled(fixedDelay = 1000)
public void runTask() {
CompletableFuture.runAsync(this::myScheduledMethod);
}