Schedulers are ways by which schedule a job to run at a predefined interval.
Steps to implement scheduler
- Include @EnableScheduling in the Main configuration file.
@SpringBootApplication
@EnableScheduling
public class BasicsApplication
- Annotate with @Scheduled, specifying the criteria fixedRate.
@Scheduled(fixedRate=5000)
public void displayScheduledTime() {
System.out.println("The time is: " + dateFormat.format(new Date()));
log.info("The time now is {} ", dateFormat.format(new Date()));
}
What else can you do using Spring schedulers.
@Scheduled(fixedRate = 5000)
- Runs the job every 5 seconds by comparing with the start.
@Scheduled(fixedDelay = 5000)
- Runs the job by verifying with the delay instead of the start time.
@Scheduled(initialDelay=10000, fixedRate = 500)
- Waits for 10 seconds and then executes the method every 500 ms.
@Scheduled(cron="*/5 * * * * MON-FRI")
- Runs a scheduled cron job.
@Async is a annotation which is used to indicate a method that it should be executed asynchronously.
No comments:
Post a Comment