緣起
筆者經常使用Quartz(http://quartz-scheduler.org/)作為排程工作的framework。目前在為這部分實作後台管理與監控的頁面時遇到此問題:「如何取得 Quartz 目前正在執行的Jobs?」範例程式
protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {
try {
List<JobExecutionContext> jobsList = ctx.getScheduler().getCurrentlyExecutingJobs();
for (JobExecutionContext context: jobsList) {
if ((context != null) && (context.getJobDetail() != null) && (context.getJobDetail().getKey() != null)) {
String currentJobName = context.getJobDetail().getKey().getName();
logger.info("currentJobName = " + currentJobName);
}
}
}
} catch (SchedulerException ex) {
logger.error("executeInternal(JobExecutionContext) - exception: ", ex);
}
}
try {
List<JobExecutionContext> jobsList = ctx.getScheduler().getCurrentlyExecutingJobs();
for (JobExecutionContext context: jobsList) {
if ((context != null) && (context.getJobDetail() != null) && (context.getJobDetail().getKey() != null)) {
String currentJobName = context.getJobDetail().getKey().getName();
logger.info("currentJobName = " + currentJobName);
}
}
}
} catch (SchedulerException ex) {
logger.error("executeInternal(JobExecutionContext) - exception: ", ex);
}
}