我知道 ShedLock 可以用来做与 Spring 集成的分布式锁:
@Scheduled(cron = "*/5 * * * * ?")
@SchedulerLock(name = "exampleLock", lockAtLeastForString = "20000", lockAtMostForString = "30000")
private void exampleMethod(){
System.out.println(String.format("[%s] test job runs...", new Date()));
}
但就我而言,我想执行从数据库中读取的多个计划任务(Crons),并由 ThreadPoolTaskScheduler 分别独立地进行处理。我编码如下,但是它不能处理多个实例来执行每个计划任务。Spring有没有办法实现这一点?任何想法将不胜感激。
public class ExampleShedLock implements SchedulingConfigurer {
@Resource
private ScheduleTaskRepository scheduleTaskRepository;
@Resource
private TaskScheduler threadPoolTaskScheduler;
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.setTaskScheduler(getThreadPoolTaskScheduler());
Map<String, String> dataMap = scheduleTaskRepository
.selectExpressionByIsActive();
dataMap.forEach(
(key, expression) -> scheduledTaskRegistrar.getScheduler()
.schedule(() -> scheduledTask(),
(TriggerContext triggerContext) -> {
CronTrigger trigger = new CronTrigger(
expression,
TimeZone.getTimeZone(ZoneOffset.UTC));
return trigger.nextExecutionTime(triggerContext);
}));
}
@SchedulerLock(name = "TaskScheduler",
lockAtLeastFor = 20*1000, lockAtMostFor = 30*1000)
private void scheduledTask() {
System.out.println(
Thread.currentThread().getName() + " - " + Calendar
.getInstance()
.getTime();
}
@Bean(name = "threadPoolTaskScheduler")
public TaskScheduler getThreadPoolTaskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(10);
taskScheduler.setThreadNamePrefix("Scheduled-");
taskScheduler.setRejectedExecutionHandler(new
ThreadPoolExecutor.CallerRunsPolicy());
taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
taskScheduler.setAwaitTerminationSeconds(60);
taskScheduler.initialize();
return taskScheduler;
}
}