I have created two batch jobs and both jobs have initial delay of 20 seconds. Now I want to put some delay between those 2 jobs. If Job1 is running then Job2 has to wait for some seconds and then execute only if Job1 is done similarly if Job2 is running then Job1 has to wait for some seconds and then execute only if Job2 is done.
@Component
public class Job1{
@Scheduled(fixedRate = 20000)
public void execute() {
//business logic
}
}
@Component
public class Job2{
@Scheduled(fixedRate = 20000)
public void execute() {
//business logic
}
}
You can achieve the behaviour by having the jobs run in a single synchronized method, for example by introducing a class which has a method to execute a synchronized method, i.e.
A full working example which utilizes this class across different @Scheduled-methods:
Varying rates and sleep-times were used to show that the jobs never overlap. If you need the delay between the methods which you mention in your question, this could be added to the
run()-method.