Search code examples
spring-bootscheduled-tasksspring-webfluxcron-taskreactor

How to write a cron job ( Scheduled task ) in spring web flux?


I am implementing forget password feature in spring web flux application and where I am sending an OTP with a expiration of 5 min upfront. So I want to invalidate them by executing a cron job every 2 seconds.

Though this is a web flux project I want to test by writing a hello world as a cron job at first like this.

I added @EnableScheduling in starter main class and i have written a test class like.

@Scheduled(cron = "*/2 * * * * *")
    public Disposable invalidateOtp(){

       return Mono.fromCallable(() -> {
           return "Hello";
       }).subscribe();

    }

But I would say this method does not execute after every 2 seconds. Can anyone suggest me why?


Solution

  • I finally found the answer. I forget to annotate my service class @Service. Otherwise spring will not found any bean.