1

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?

D.Anush
  • 147
  • 6
  • 15
  • what version of spring are you using? You'll need 5.3 or higher for improved cron expressions: https://spring.io/blog/2020/11/10/new-in-spring-5-3-improved-cron-expressions – billjamesdev May 22 '22 at 17:46
  • @billjamesdev I added System.out.println("version: " + SpringVersion.getVersion()); in the main method and i checked and it saying my version is 5.3.18. – D.Anush May 22 '22 at 17:57
  • Not enough info. Is that method is part of some bean? How really do you test? Such a method call is going to be performed in other thread from main JUnit one, so there has to be some barrier to before verification or some timing verification. – Artem Bilan May 22 '22 at 20:32
  • @ArtemBilan, No this is not a part of bean, This is a simple test method. As this is a cron, i started the app in debug mode and put a debug point to check weather the method calling after 10s – D.Anush May 23 '22 at 05:32
  • Then it’s not correct. If it is not the bean method then Spring doesn’t know that such a method has to be proxies and exposed respectively . Also: if it is unit test , it cannot be initiated when you start the app. You definitely have to start a test class manually or vie build tool task. – Artem Bilan May 23 '22 at 12:09
  • @ArtemBilan, yes. You gave me the point. the class where i wrote this snippet marked as a service bean. So it is working now. – D.Anush May 23 '22 at 14:28

2 Answers2

-1

Here is very good article about how to run Scheduled tasks with cron expression or with fixed rate or fixed delay: The @Scheduled Annotation in Spring. This should give you all you need. However, At some point I felt that all options provided in Spring boot were not convinient enough especially for human readability of the params. You have to specify time in milliseconds or with cron-expression which is not very easily readable. So, I wanted to be able specify time like "9m" for 9 minutes or "3h" for 3 hours and so forth. So, I wrote my own background task scheduler that can easily be used with Spring boot. I published it in Open source library called MgntUtils which is written and maintained by me. Here is a Javadoc page that explains my idea and how to use the feature. (This is part of MgntUtils library javadoc) You can get the library as Maven artifact here or on the github (with javadoc and source-code included). On the github you can look in the package: com.mgnt.lifecycle.management.backgroundrunner.example. That package contains complete working example of 2 scheduled tasks. If you download the source you can just run them out of the box and see how they work

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
-1

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

D.Anush
  • 147
  • 6
  • 15