Schedule application Spring on Docker cluster

2/22/2019

I need to schedule in batch my application Spring in a Docker cluster with different nodes. I found the solution of set replicas=1 on docker-compose, but in my opinion this isn't the best solution because minimizes the potential of Docker.

Some help or advice? Thank you.

-- Jack
batch-processing
docker
kubernetes
schedule
spring

1 Answer

2/22/2019

If I understand you correctly you want to run several replicas of a spring application (it does not matter if this is managed by docker, k8s, it is standalone, etc). Then you want a background job to be started only on one single instance. Right? In this case I may advise you to have a look at ShedLock.

ShedLock does one and only one thing. It makes sure your scheduled tasks are executed at most once at the same time. If a task is being executed on one node, it acquires a lock which prevents execution of the same task from another node (or thread). Please note, that if one task is already being executed on one node, execution on other nodes does not wait, it is simply skipped.

It integrates smoothly in Spring. For example a scheduled batch job may look like this:

@Scheduled(cron = ...)
@SchedulerLock(name = "scheduledTaskName")
public void scheduledTask() {
   // do something
}

Various options may be used under the hood to implement a distributed lock, e.g. MySQL, Redis, Zookeeper and others.

-- Lachezar Balev
Source: StackOverflow