restquarkus

Preventing Parallel Executions of a Method Triggered by Quartz Scheduler and REST Call


I have a Quartz Scheduler that calls my configUpdate method every hour. However, this same method can also be invoked via a REST API call in my controller. Since this method is complex and asynchronous, I want to ensure that it never runs concurrently, regardless of whether it's triggered by the scheduler or the REST call.

What options do I have to achieve this?


Solution

  • You need to lock your method so that only 1 thread at a time can access it.

    If your app runs as a single instance then you can either “synchronize” your method or use the java.concurrent library.

    Otherwise you need to combine it with something like Shedlock or Quartz.