Scheduling executors
Earlier in this chapter, we discussed how the command-line runners can be used as a place to start the scheduled executor thread pools to run the worker threads in intervals. While that is certainly a possibility, Spring provides you with a more concise configuration to achieve the same goal: @EnableScheduling.
Getting ready
We will enhance our application so that it will print a count of books in our repository every 10 seconds. To achieve this, we will make the necessary modifications to the BookPubApplication and StartupRunner classes.
How to do it...
- Let's add an
@EnableSchedulingannotation to theBookPubApplicationclass, as follows:
@SpringBootApplication
@EnableScheduling
public class BookPubApplication {...}- As a
@Scheduledannotation can be placed only on methods without arguments, let's add a newrun()method to theStartupRunnerclass and annotate it with the@Scheduledannotation, as shown in the following line:
@Scheduled(initialDelay = 1000, fixedRate = 10000...