我在下面编写代码,我正在运行两个工作。首先是10秒的间隔,另一个是3秒的间隔。但最终在某些时候它们会同时执行。有没有机制来避免这种情况
JobDetail jDetail = new JobDetail("Job1", "group1", MyJob.class);
CronTrigger crTrigger = new CronTrigger("cronTrigger", "group1", "0/10 * * * * ?");
sche.scheduleJob(jDetail, crTrigger);
jDetail = new JobDetail("Job2","group2",MyJob2.class);
crTrigger = new CronTrigger("cronTrigger2","group2","0/3 * * * * ?");
sche.scheduleJob(jDetail, crTrigger);
答案 0 :(得分:3)
没有完全回答你的问题,但这是你如何查询以线程安全方式运行的东西:
//sched is your org.quartz.Scheduler
synchronized (sched) {
JobDetail existingJobDetail = sched.getJobDetail(jobName, jobGroup);
if (existingJobDetail != null) {
List<JobExecutionContext> currentlyExecutingJobs = (List<JobExecutionContext>) sched.getCurrentlyExecutingJobs();
for (JobExecutionContext jec : currentlyExecutingJobs) {
if (existingJobDetail.equals(jec.getJobDetail())) {
// This job is currently executing
}
}
}
答案 1 :(得分:3)
Configure the Quartz threadpool只有一个帖子。
org.quartz.threadPool.threadCount=1
答案 2 :(得分:2)
您可以创建一个辅助对象来使两个作业同步:
//In the base class
public static Object lock = new Object();
//In the first class
public void execute() {
synchronized(lock) {
//do stuff
}
}
//In the second class
public void execute() {
synchronized(lock) {
//do stuff
}
}
详细了解同步: http://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html
答案 3 :(得分:1)
你试过了吗?
org.quartz.jobStore.isClustered: true
或者,您将工作变为有状态的工作(并将isClustered设置为true),并且shoujld解决您的问题。 (糟糕,不推荐使用StatefulJob;请使用DisallowConcurrentExecution。)