The question is: how to make the logic from the @PostConstruct method be called in only one instance and not called in others?
An example of my code:
@Component
@AllArgsConstructor
public class TestJob {
private TestService testService;
@PostConstruct
public void init() {
testService.upload();
}
@Scheduled(cron = "${cron}")
@SchedulerLock(name = "uploadJob", lockAtMostFor = "${lockAtMostFor}")
public void execute() {
testService.upload();
}
}
It should work if you put the @PostConstruct
method to a different service and call the execute()
method.
The reason is that ShedLock by default uses Spring AOP which wraps each method marked by @SchedulerLock
in an aspect that does the locking. And Spring AOP usually does not get applied if you call another method in the same class.