我正在尝试在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发布于 2019-08-07 16:13:49
您需要在quartz.property文件(来源:click here)中添加以下属性:
org.quartz.jobStore.isClustered : true
有关isClustered属性的更多信息,请参阅this链接。
请注意:
@DisallowConcurrentExecution在同一节点上有两个不同的作业,且作业键相同时,@DisallowConcurrentExecution起作用。
而isClustered属性用于确保在app运行多个节点时执行作业的单个实例,通过数据库表进行通信以实现原子性。
https://stackoverflow.com/questions/57367362
复制相似问题