I have the following code where i get a list of Cronjobs from the DB and execute them programmatically using SchedulingConfigurer.
Any pointers on spinning each task with its own LockableTaskScheduler?
I tried with the below code, but it does not work.
Shedlock is not creating an entry for each cron in the DB, its just using the last Cron.
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
Map<String, TaskConfig> cronTasks = findJobs();
for (Map.Entry<String, TaskConfig> cronTask : cronTasks.entrySet()) {
taskRegistrar.setTaskScheduler(getScheduler(cronTask.getValue()));
log.info("Registering cron task for scheduling: {}, cronExpression: {}", cronTask.getKey(), cronTask
.getValue());
taskRegistrar.addTriggerTask(() -> executeTask(cronTask.getValue()), cronTask.getValue().getCronTrigger());
}
}
public LockableTaskScheduler getScheduler(TaskConfig value) {
LockConfigurationExtractor lockConfigurationExtractor = task -> Optional
.of(new LockConfiguration(Instant.now(), value.getConfigEntity().get_id(), Duration.ofMinutes(3), Duration
.ofMinutes(2)));
LockManager lockManager = new DefaultLockManager(lockProvider, lockConfigurationExtractor);
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setThreadNamePrefix("MyThreadPoolTaskScheduler");
scheduler.initialize();
return new LockableTaskScheduler(scheduler, lockManager);
}