Background Tasks with Spring Boot


Add @EnableScheduling to the Application class

@SpringBootApplication
@EnableScheduling
class Application {
  companion object {
    @JvmStatic
    fun main(args: Array<String>) {
      SpringApplication.run(Application::class.java, *args)
    }
  }
}

Add @Sccheduled to the background method

@Service
class BackgroundRunner {
  @Scheduled(fixedDelay = 1000 * 60 * 60) // Run every hour
  fun doSomething() {
    ...
  }
}

See also