我正在使用java石英schedular。我可以完美地安排工作,但我想要的是等待工作完成之後再運行第二輪,因爲運行每項工作所需的時間會有所不同。Quartz @DisallowConcurrentExecution不按預期工作
我使用了@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);
你會等一分多鐘嗎?根據你的日程安排,cron將在每分鐘的第二個'0'和第二個'5'運行,但**將等待直到完成啓動另一個工作** –
這次執行,Wed Nov 25 12:01:15 EAT 2015和現在是2015年11月25日星期三12:32,所以基本上我已經等了> 30分鐘。 。 。並且如果第二個15被執行,則沒有其他工作 –
意味着您的日程安排不起作用。 –