本文介紹Spring任務接入SchedulerX任務調度中的常見問題。
SchedulerX接管後原Spring定時器依舊運行
由於應用中配置了自訂的Scheduler調度器導致SchedulerX覆蓋自訂處理器。請排查業務應用工程中是否存在實現org.springframework.scheduling.annotation.SchedulingConfigurer介面的類,確認是否調用了ScheduledTaskRegistrar的setScheduler方法覆蓋預設調度器,如果存在,請注釋掉相關邏輯即可。
Spring任務如何擷取任務上下文
在業務應用工程代碼中增加以下代碼擷取任務上下文。
JobContext jobContext = ContainerFactory.getContainerPool().getContext();Spring任務是否支援返回結果
版本用戶端大於1.10.11時,Spring任務支援返回結果,您可以直接在定時方法上返回任意結果。
@Scheduled(cron = "0/5 * * * * ?")
public ProcessResult helloStandalone1() {
try {
logger.info(DateUtil.now() + " " + Thread.currentThread().getName() + " hello world. start");
TimeUnit.SECONDS.sleep(2L);
logger.info(DateUtil.now() + " " + Thread.currentThread().getName() + " hello world. end");
} catch (Exception e) {
e.printStackTrace();
logger.info(DateUtil.now() + " " + Thread.currentThread().getName() + " hello world. exception end..");
}
return new ProcessResult(true, "執行結果資訊");
}
@Scheduled(cron = "0/5 * * * * ?")
public String helloStandalone2() {
try {
logger.info(DateUtil.now() + " " + Thread.currentThread().getName() + " hello world. start");
TimeUnit.SECONDS.sleep(2L);
logger.info(DateUtil.now() + " " + Thread.currentThread().getName() + " hello world. end");
} catch (Exception e) {
e.printStackTrace();
logger.info(DateUtil.now() + " " + Thread.currentThread().getName() + " hello world. exception end..");
}
return "執行結果資訊";
}