我有一个带有消息的简单Job类和一个执行5秒钟的setter。此作业类由@DisallowConcurrentExecution注释,因此无法一次执行多次。
@DisallowConcurrentExecution
public class LongJob implements Job {
private String message = "Default";
private int sleepTime = 1000;
public void setMessage(String message) {
this.message = message;
}
public void setSleepTime(int sleepTime) {
this.sleepTime = sleepTime;
}
public LongJob() {
}
@Override
public void execute(JobExecutionContext context)
throws JobExecutionException {
try {
for (int count = 0; count < 5; count++) {
Thread.sleep(sleepTime);
System.out.println(new Date().toString() + " - " + count + ": " + message);
}
} catch (InterruptedException ex) {
Logger.getLogger(LongJob.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
在我的主要方法中,我尝试运行此类6次,每次都使用不同的消息。
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
Scheduler sched = schedFact.getScheduler();
sched.start();
String name = "LongJob2X";
JobDetail job = newJob(LongJob.class)
.withIdentity(name, "group1")
.storeDurably()
.requestRecovery(true)
.build();
sched.addJob(job, true);
JobDataMap data = new JobDataMap();
data.put("message", "Prvy");
JobDataMap data2 = new JobDataMap();
data2.put("message", "Druhy");
JobDataMap data3= new JobDataMap();
data.put("message", "3");
JobDataMap data4 = new JobDataMap();
data.put("message", "stvrty");
JobDataMap data5 = new JobDataMap();
data.put("message", "5");
JobDataMap data6 = new JobDataMap();
data.put("message", "xsiesty6");
sched.triggerJob(job.getKey(), data);
sched.triggerJob(job.getKey(), data2);
sched.triggerJob(job.getKey(), data3);
sched.triggerJob(job.getKey(), data4);
sched.triggerJob(job.getKey(), data5);
sched.triggerJob(job.getKey(), data6);
令我困惑的是输出。尽管每个触发器的代码都相同,但有时不会调用消息的setter,因此输出:
Thu Jan 22 23:32:09 CET 2015 - 0: xsiesty6
Thu Jan 22 23:32:10 CET 2015 - 1: xsiesty6
Thu Jan 22 23:32:11 CET 2015 - 2: xsiesty6
Thu Jan 22 23:32:12 CET 2015 - 3: xsiesty6
Thu Jan 22 23:32:13 CET 2015 - 4: xsiesty6
Thu Jan 22 23:32:14 CET 2015 - 0: Default
Thu Jan 22 23:32:15 CET 2015 - 1: Default
Thu Jan 22 23:32:16 CET 2015 - 2: Default
Thu Jan 22 23:32:17 CET 2015 - 3: Default
Thu Jan 22 23:32:18 CET 2015 - 4: Default
Thu Jan 22 23:32:19 CET 2015 - 0: Default
Thu Jan 22 23:32:20 CET 2015 - 1: Default
Thu Jan 22 23:32:21 CET 2015 - 2: Default
Thu Jan 22 23:32:22 CET 2015 - 3: Default
Thu Jan 22 23:32:23 CET 2015 - 4: Default
Thu Jan 22 23:32:24 CET 2015 - 0: Druhy
Thu Jan 22 23:32:25 CET 2015 - 1: Druhy
Thu Jan 22 23:32:26 CET 2015 - 2: Druhy
Thu Jan 22 23:32:27 CET 2015 - 3: Druhy
Thu Jan 22 23:32:28 CET 2015 - 4: Druhy
Thu Jan 22 23:32:30 CET 2015 - 0: Default
Thu Jan 22 23:32:31 CET 2015 - 1: Default
Thu Jan 22 23:32:32 CET 2015 - 2: Default
Thu Jan 22 23:32:33 CET 2015 - 3: Default
Thu Jan 22 23:32:34 CET 2015 - 4: Default
Thu Jan 22 23:32:35 CET 2015 - 0: Default
Thu Jan 22 23:32:36 CET 2015 - 1: Default
Thu Jan 22 23:32:37 CET 2015 - 2: Default
Thu Jan 22 23:32:38 CET 2015 - 3: Default
Thu Jan 22 23:32:39 CET 2015 - 4: Default
我尝试的每次运行只是第二个和最后一个消息集。一次可以存储多少JobDataMaps是否有限制?或者我做错了什么?感谢。
最后一点:我正在使用JDBC JobStore:
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
答案 0 :(得分:1)
很简单,您几乎每次都将消息放在同一个data
上,而不是data.put(...)
,data2.put(...)
,data3.put(...)
,...
JobDataMap data = new JobDataMap();
data.put("message", "Prvy"); //Correct
JobDataMap data2 = new JobDataMap();
data2.put("message", "Druhy");// Correct
JobDataMap data3= new JobDataMap();
data.put("message", "3");// Here not data3 but data
JobDataMap data4 = new JobDataMap();
data.put("message", "stvrty");// Here not data4 but data
JobDataMap data5 = new JobDataMap();
data.put("message", "5"); // Here not data5 but data
JobDataMap data6 = new JobDataMap();
data.put("message", "xsiesty6"); // Here not data6 but data