How to schedule a task and run only once independent of the number of instances?

Asked

Viewed 463 times

4

My application has a scheduled method to run every 5 minutes with the @Scheduled of Spring, but I would like to rise more than one instance of implementation and that this task should not be carried out more than once independent of the number of instances.

Main Class

@SpringBootApplication
public class SchedulingApplication {
    public static void main(String[] args) {
        System.setProperty("spring.application.name", "scheduling-application");
        SpringApplication.run(SchedulingApplication.class, args);
    }
}

Scheduled Task

@Scheduled(cron = "0 * * * * *")
public void searchAndSend() {
    // FAZ UMA BUSCA E MANDA PRA FILA
}

As it stands, if I go up two instances the search and send task to the queue will be executed twice a minute. If I go up three, it will be three per minute. How do I make this task run only once independent of the number of instances?

  • 2

    You can use the strategy of setting a property at the startup of your instance, so only one of them has the property to activate the Schedule of your activity. However, the Schedule mechanism should be done manually.

  • If you need help I can help with the implementation

  • Interesting. Can we consider this a gambiarra?! Hahahaha

  • 1

    I believe not, even because the property will be set the same spring.application.name, but when initializing the jar or the server will inform on the boot parameters -Dspring.application.name=xpto -Dx.application.scheduler=true for example, this already eliminates the first line of your method

  • 1

    Based on the second property, Voce configures (or not) the Scheduler manually

  • managed to make?

  • I did using a solution called Shedlock

  • Got it, actually the solution then got a little different from the question, you keep all instances running the Scheduler always, but, just one at a time.

  • The function with Scheduler exists in all instances. Which instance will perform the function does not matter, as long as it runs only once. Send the email only once for example.

Show 4 more comments

1 answer

1

Hello, all right?

For distributed tasks it is recommended that you get some information where all instances of your Cluster have access.

Some strategies can be used as:

Work with a component/application that performs this type of task in a more professional way, such as Quartz. Follow some links:

http://www.quartz-scheduler.org/

https://www.baeldung.com/spring-quartz-schedule

Quartz can manage its Schedulers/Jobs in a distributed way and with the possibility of follow-ups such as: Jobs executed, Jobs paused, Jobs canceled, Jobs with error. Moreover.

Other alternatives are:

  • Work with distributed database or distributed cache execution status for applications, before running Job, check if any instances are already running the same;
  • Working with Fila concepts using some Message Broker like Rabbitmq or Activemq.

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.