gpt4 book ai didi

java - 检查作业是否正在运行?

转载 作者:行者123 更新时间:2023-12-01 12:47:21 27 4
gpt4 key购买 nike

我想检查Quartz作业是否正在运行。我发现它可以使用 scheduler.getCurrentlyExecutingJobs() 。我对此感到困惑,我到底应该把它放在哪里才能得到结果?谢谢

try {

// specify the job' s details..
JobDetail job = JobBuilder.newJob(TestJob.class)
.withIdentity("testJob")
.build();

// specify the running period of the job
Trigger trigger = TriggerBuilder.newTrigger()
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(10)
.repeatForever())
.build();

//schedule the job
SchedulerFactory schFactory = new StdSchedulerFactory();
Scheduler sch = schFactory.getScheduler();
sch.start();
sch.scheduleJob(job, trigger);

System.out.println("******* " + sch.getCurrentlyExecutingJobs());

} catch (SchedulerException e) {
e.printStackTrace();
}

最佳答案

您可以使用scheduler.getCurrentlyExecutingJobs()来获取当前正在运行的所有作业的列表。

       List<JobExecutionContext> currentJobs = scheduler.getCurrentlyExecutingJobs();
for (JobExecutionContext jobCtx: currentJobs){
jobName = jobCtx.getJobDetail().getName();
groupName = jobCtx.getJobDetail().getGroup();
if (jobName.equalsIgnoreCase("job_I_am_looking_for_name") && groupName.equalsIgnoreCase("job_group_I_am_looking_for_name")) {
//found it!
logger.warn("the job is already running - do nothing");
return;
}
}

关于java - 检查作业是否正在运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24568282/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com