Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Scheduler code within an App with multiple instances with multiple JVMs

I have a spring scheduler task configured with either of fixedDelay or cron, and have multiple instances of this app running on multiple JVMs.

The default behavior is all the instances are executing the scheduler task.

Is there a way by which we can control this behavior so that only one instance will execute the scheduler task and others don't.

Please let me know if you know any approaches.

Thank you

like image 493
narayan-sambireddy Avatar asked Dec 17 '25 19:12

narayan-sambireddy


1 Answers

We had similar problem. We fixed it like this:

  1. Removed all @Scheduled beans from our Spring Boot services.
  2. Created AWS Lambda function scheduled with desired schedule.
  3. Lambda function hits our top level domain with scheduling request.
  4. Load balancer forwards this request to one of the service instances.

This way we are sure that scheduled task is executed only once across the cluster of our services.

like image 133
luboskrnac Avatar answered Dec 19 '25 13:12

luboskrnac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!