0

I am using a @Scheduled cron job on a method is

Controller A { 

 @Scheduled(cron = "0 0/2 * * * *", zone = "GMT")
 public void methodA() {
 // Do something

}`

Is there a way my ControllerB is notified while methodA() is busy?

Controller B { 

     @Scheduled
     public void methodB() {
     // I want to be informed that a scheduled job is running
    
    }`
ErEcTuS
  • 777
  • 1
  • 14
  • 33
  • It depends on context. you can add flag to DB and set it to true when methodA is running and from methodB read it. Or you can add it to some mem cache or to distributet cache. Or you can use shedlock. – Aleks D Oct 08 '21 at 13:10
  • Thanks for the answer, I don't have a db but I can maybe use a global variable that is flagged to true and then false as soon as MethodA finishes the execution. I was thinking of a built-in option in spring boot schedules..but apparently there's not – ErEcTuS Oct 08 '21 at 13:14
  • May be this can help. https://stackoverflow.com/a/4501314/5471104 – Mohammed Atif Oct 08 '21 at 13:21
  • Another option is to use Spring's event publishing mechanism. When the job starts it would publish a 'start' event that the dependent controller can listen for. When the job ends it would publish and 'end' event. By default Spring events are synchronous. You can change this behavior using the @Async annotation. See [this link](https://www.baeldung.com/spring-events) for more information about Spring events. It may be overkill but would decouple you from the shared global variable. – Lee Greiner Oct 08 '21 at 13:43
  • Hey Lee, very interesting thanks. Worth to explore and try.. – ErEcTuS Oct 08 '21 at 13:46

0 Answers0