gpt4 book ai didi

java - Quartz @DisallowConcurrentExecution 未按预期工作

转载 作者:行者123 更新时间:2023-11-30 07:46:13 27 4
gpt4 key购买 nike

我正在使用javaquartz调度。我能够完美地安排作业,但我想要的是在运行第二轮之前等待作业完成,因为运行每个作业所需的时间各不相同。

我使用了@DisallowConcurrentExecution,它所做的只是让作业运行一次,不再运行。从作业监听器显示该作业已成功完成一次。

Job
=============================================================

@DisallowConcurrentExecution
public class SalesJob implements Job{
List<Transaction> unsentTransaction = new ArrayList<Transaction>();
List<Sale> sales = new ArrayList<Sale>();

public void execute(JobExecutionContext jec) throws JobExecutionException {
System.out.println("Sales Job. . .");
}
}

作业监听器:

public class SalesJobListener implements JobListener{
public static final String LISTENER_NAME = "dummyJobListenerName";

public String getName() {
return LISTENER_NAME;
}

public void jobToBeExecuted(JobExecutionContext context) {
String jobName = context.getJobDetail().getKey().toString();
System.out.println("jobToBeExecuted");
System.out.println("Job : " + jobName + " is going to start...");
}

public void jobExecutionVetoed(JobExecutionContext jec) {
System.out.println("jobExecutionVetoed");
}

public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
System.out.println("jobWasExecuted");
String jobName = context.getJobDetail().getKey().toString();
System.out.println("Job : " + jobName + " is finished...");
System.out.println("=====================================");
System.out.println("==========" + new Date() + "===========");

if (!jobException.getMessage().equals("")) {
System.out.println(
"Exception thrown by: " + jobName + " Exception: " + jobException.getMessage());
}
}

}

这是时间表

JobKey salesJobKey = new JobKey("salesJob", "group1");
JobDetail salesJob = JobBuilder.newJob(SalesJob.class)
.withIdentity(salesJobKey).build();

Trigger salesTrigger = TriggerBuilder
.newTrigger()
.withIdentity("salesTrigger", "group1")
.withSchedule(
CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
.build();

Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.getListenerManager().addJobListener(
new SalesJobListener(), KeyMatcher.keyEquals(salesJobKey)
);

scheduler.start();
scheduler.scheduleJob(salesJob, salesTrigger);

最佳答案

问题

it executed this time, Wed Nov 25 12:01:15 EAT 2015 and now is Wed Nov 25 12:32 2015, so basically i have waited > 30 mins. . . and there is no another job

这就是说调度程序不起作用。

为什么?

  • 您无法在一分钟的第 15 秒执行作业,因为模式:0/5 * * * * ? 使调度程序在第 0 秒和第 5 秒运行每分钟。
  • 如果另一个相同类型的作业已在运行,则使用 @DisallowConcurrentExecution 将阻止执行作业。
<小时/>

解决方案:

错误在于您的代码顺序,您在告诉它必须安排作业之前执行调度程序 (scheduler.start();) (scheduler.scheduleJob(salesJob, salesTrigger);):

scheduler.start();
scheduler.scheduleJob(salesJob, salesTrigger);

检查this example并交换你的线路:

scheduler.scheduleJob(salesJob, salesTrigger);
scheduler.start();

就这些了...

关于java - Quartz @DisallowConcurrentExecution 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33912724/

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