我正在使用java quartz schedular。我可以完美地安排工作,虽然我想要的是等待工作完成第二轮之前,因为每个工作的运行时间各不相同。
我使用了@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);
答案 0 :(得分:1)
问题
它执行了这次,11月25日星期三12:01:15 EAT 2015,现在是2015年11月25日星期三12:32,所以基本上我等了&gt; 30分钟。 。而且没有其他工作
这就是说Scheduler
无效。
<强> WHY吗
0/5 * * * * ?
使调度程序在每分钟的第0和第5秒运行 ONLY 。@DisallowConcurrentExecution
将阻止执行作业。<强> SOLUTION:强>
错误是按照您的代码顺序执行,然后执行调度程序(scheduler.start();
),然后告诉它必须安排作业(scheduler.scheduleJob(salesJob, salesTrigger);
):
scheduler.start();
scheduler.scheduleJob(salesJob, salesTrigger);
检查this example并交换您的行:
scheduler.scheduleJob(salesJob, salesTrigger);
scheduler.start();
这就是......
答案 1 :(得分:0)
只需添加以下空检查
if(null != jobException) {
if (!jobException.getMessage().equals("")) {
logger.debug("Exception thrown by: " + jobName
+ " Exception: " + jobException.getMessage());
}
}
您的代码将起作用