gpt4 book ai didi

java - Quartz Scheduler 当前职位正在增加

转载 作者:行者123 更新时间:2023-11-30 02:29:33 26 4
gpt4 key购买 nike

我使用quartz 创建了一个示例程序。主程序将触发一个每 3 秒运行一次的 Controller 作业,并且从该 Controller 我已经安排了一个作业在 cron 计划中运行。

我的要求是,如果作业的一个实例正在运行,则下一个实例不应启动。因此,我迭代了调度程序上下文中当前 Activity 作业的作业上下文列表,并检查是否存在与当前作业实例匹配的作业实例,然后简单地返回。

为了测试这一点,我在各自的作业中实现了超过预定时间的线程 sleep 。作业实例不会按预期并行触发,但我的调度程序上下文当前 Activity 作业列表正在不断增加。我需要帮助来减少/持续检查当前 Activity 作业的规模。

我的程序如下。

主程序:

package com.test.objectpool;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class CronTriggerExample {

public static void main(String[] args) throws Exception {
JobDetail job = JobBuilder.newJob(QuartzSchedulerController.class).withIdentity("job-a-cntrl", "group.12-1")
.build();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger.1", "group.12-1")
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever()).build();

Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);

}
}

Controller 工作:

package com.test.objectpool;

import org.quartz.CronScheduleBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;

public class QuartzSchedulerController implements Job {

public void execute(JobExecutionContext context) {

try {
context.getScheduler().getCurrentlyExecutingJobs().forEach(job -> {
if (job.getTrigger().equals(context.getTrigger()) && !job.getJobInstance().equals(this)) {
System.out.println("There's another instance running crontroller , so leaving" + this);
return;
}
});

Thread.sleep(4000);
System.out.println("Inside scheduler controller --- >> ");
Scheduler scheduler = context.getScheduler();

JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity("hello-1.1", "group1-1.2").build();
JobKey jbK = job.getKey();
System.out.println("Job key is " + jbK);
if (!scheduler.checkExists(jbK)) {
System.out.println("Scheduling hellow world -----");
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger.1", "group1-1.2").withSchedule(
CronScheduleBuilder.cronSchedule("0/2 * * * * ?").withMisfireHandlingInstructionDoNothing())
.build();

scheduler.scheduleJob(job, trigger);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

最后由 Controller 控制的作业类是:

package com.test.objectpool;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;

public class HelloJob implements Job {
private static int count = 0;

public void execute(JobExecutionContext context) throws JobExecutionException {

try {
List<JobExecutionContext> jobs = context.getScheduler().getCurrentlyExecutingJobs();
System.out.println("The size of the job queue is " + jobs.size());
for (JobExecutionContext job : jobs) {
if (job.getTrigger().equals(context.getTrigger()) && !job.getJobInstance().equals(this)) {
System.out.println("There's another instance running, so leaving" + this);
return;
}

}
Date dt = Calendar.getInstance().getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
String uniqueID = UUID.randomUUID().toString();
System.out.println("Hello Quartz!" + context.getScheduledFireTime() + " :With count " + count
+ " :Current date " + sdf.format(dt) + " UUID =" + uniqueID);

Thread.sleep(10000);
// System.out.println("Hello Quartz!"+
// context.getScheduledFireTime());

System.out.println("Completed " + "With count " + count + " UUID : " + uniqueID);
count++;
} catch (SchedulerException | InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

}

问题是,通过这种方法,context.getScheduler().getCurrentlyExecutingJobs() 不断增加。

最佳答案

只需用 @DisallowConcurrentExecution 注释您的工作即可,像这样:

@DisallowConcurrentExecution
public class QuartzSchedulerController implements Job {
...

Quartz 会注意不要运行超过 1 个实例。

An annotation that marks a Job class as one that must not have multiple instances executed concurrently (where instance is based-upon a JobDetail definition - or in other words based upon a JobKey).

关于java - Quartz Scheduler 当前职位正在增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44572734/

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