【发布时间】:2019-08-06 00:04:29
【问题描述】:
我正在尝试在 Quartz 1.6 中创建一个 Job,但只需要执行一次,因为我有两个具有相同版本的 .war 文件的测试实例。
这是我的TestPlugin 类,Job 将每 60 秒执行一次:
public class TestPlugin implements PlugIn {
public TestPlugin() {
super();
}
public void destroy() {
}
public void init(ActionServlet arg0, ModuleConfig arg1)
throws ServletException {
try {
JobDetail job = JobBuilder.newJob(TestDemonio.class)
.withIdentity("anyJobName", "group1").build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("anyTriggerName", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0/60 * * ? * * *"))
.build();
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.scheduleJob(job, trigger);
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
然后我让我的班级TestExecute 打印一个简单的输出:
@DisallowConcurrentExecution
public class TestDemonio implements Job {
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("QUARTZ JOB MESSAGE");
}
}
我研究了如何通过添加注释@DisallowConcurrentExecution 来实现我想要的,只执行一次作业,但我收到了打印在每个实例上的消息。
这是我的quartz.properties 文件:
# Default Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance, if a different
# properties file is not explicitly specified.
#
org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 10
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
org.quartz.jobStore.misfireThreshold: 60000
org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
【问题讨论】:
-
你能分享一下quartz.properties 文件吗?另外,请检查数据库表中的作业条目,了解它显示了多少作业实例?
-
另外,请注意
@DisallowConcurrentExecution,据说是为了迎合单个节点上的单个作业执行。对于多个节点,石英集群负责。 -
声明
necessity to execute only once和will be executed every 60 seconds相互排斥。请澄清所需的行为 -
@Ankur 我添加了石英属性文件。
标签: java quartz-scheduler quartz