我运行一个带弹簧的简单控制器来测试石英功能。
@PostMapping(path = ["/api/v1/start/{jobKey}/{jobGroup}"])
fun start(@PathVariable jobKey: String, @PathVariable jobGroup: String): ResponseEntity<String> {
val simpleJob = JobBuilder
.newJob(SampleJob::class.java)
.requestRecovery(true)
.withIdentity(JobKey.jobKey(jobKey, jobGroup))
.build()
val sampleTrigger = TriggerBuilder
.newTrigger()
.withIdentity(jobKey, jobGroup)
.withSchedule(
SimpleScheduleBuilder
.repeatSecondlyForever(5)
.withMisfireHandlingInstructionIgnoreMisfires())
.build()
val scheduler = factory.scheduler
scheduler.jobGroupNames.contains(jobGroup)
if (scheduler.jobGroupNames.contains(jobGroup)) {
return ResponseEntity.ok("Scheduler exists.")
}
scheduler.scheduleJob(simpleJob, sampleTrigger)
scheduler.start()
return ResponseEntity.ok("Scheduler started.")
}
@PostMapping(path = ["/api/v1/stop/{jobKey}/{jobGroup}"])
fun stop(@PathVariable jobKey: String, @PathVariable jobGroup: String): String {
val scheduler = factory.scheduler
scheduler.interrupt(JobKey.jobKey(jobKey, jobGroup))
val jobGroupNames = scheduler.jobGroupNames
logger.info("Existing jobGroup names: {}", jobGroupNames)
return scheduler.deleteJob(JobKey.jobKey(jobKey, jobGroup)).toString()
}
然后我使用相同的代码在不同的端口上启动两个应用程序并开始使用它。我们称它们为APP1和APP2我使用PostgreSQL作为JobStore。
所以我运行了几个场景。
1)使用APP1中的group1和key1创建作业
2)尝试在APP2中创建group1和key1的作业。 - 它给出了作业已经开始的错误。这种行为就像我预期的那样。
3)停止APP1。我希望这份工作将在APP2中执行,因为它仍然存在于JobStore中,但事实并非如此。我是否需要提供一些额外的配置?
4)启动APP1,也没有任何反应。此外,group1和key1的记录仍然显示在DB中,无法启动。
我是否需要修改关闭行为以在应用程序关闭时删除作业并在另一个应用程序中启动作业?或者我只需要以另一种正确的方式配置触发器?
我的坏,这是一个愚蠢的问题。我忘了在我的应用程序中启动调度程序
@Bean
open fun schedulerFactory(): SchedulerFactory {
val factory = StdSchedulerFactory()
factory.initialize(ClassPathResource("quartz.properties").inputStream)
factory.scheduler.start() // this line was missed
return factory
}