Issue
I'm in the need of runnning/not running a @Scheduled job based on a boolean that's dynamically computed inside of a bean (periodically and also via @Scheduled the bean reads a file and check its content). I've spent a lot time thinking and Googling and haven't come up with a solution until now.
- @ConditionalOn* would not work as that is only read once during initialization
- Subclassing the scheduler doesn't work as the schedule methods are also only called once at startup
- Checking for the property in the scheduled method doesn't work for me as it must not get called at all
Anyone got a clever idea?
Thanks!
Solution
I solved it with an Aspect @Around Spring's @Scheduled annotation and then not calling proceedingJoinPoint.proceed():
@Around("@annotation(org.springframework.scheduling.annotation.Scheduled)")
Handling Shedlock wasn't as 'easy' but I was able to stop the executions with this @Around and then returning Optional.empty() (pretending Shedlock cannot get hold of a lock):
@Around("execution(* net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider.lock(..)) "
+ "|| execution(* net.javacrumbs.shedlock.core.LockProvider.lock(..))")
Answered By - N4zroth