Grails Quartz - 如何将相似的两项工作合并为一项工作?
Grails Quartz - How to conbine similar two jobs into one job?
我有两个相似的工作,想把两个工作合二为一。
如何将 AJob 和 BJob 合并为一个新的 Job?
换句话说,我想将 AJob.groovy 和 BJob.groovy 重构为一个 class NewJob.groovy.
AJob.groovy
class AJob {
def testService
static triggers = {
cron name: 'myTrigger', cronExpression: "0 0 6 * * ?"
}
def group = "MyGroup"
def description = "A job with hello"
def execute() {
testService.hello("hello")
}
}
BJob.groovy
class BJob {
def testService
static triggers = {
cron name: 'myTrigger', cronExpression: "0 0 7 * * ?"
}
def group = "MyGroup"
def description = "B job with goodbye"
def execute() {
testService.hello("goodbye")
}
}
TestService.groovy
class TwitterService {
def hello(message){
print message
}
}
我认为这样做的唯一原因(相对于让 Cron 同时调用两者)是以某种方式同步它们,以保证在 B 之后调用 A。最明显的解决方案是如上所述:
class NewJob{
def execute() {
testService.hello("hello")
testService.hello("goodbye")
}
}
如果出于某种原因这些是异步调用(并且您仍想同步),则必须在线程上使用回调或创建单独的线程并以某种方式注入依赖项。不这样做的唯一原因是,如果您需要以某种方式记录 quartz 作业的依赖注入。更多详情?
我有两个相似的工作,想把两个工作合二为一。
如何将 AJob 和 BJob 合并为一个新的 Job?
换句话说,我想将 AJob.groovy 和 BJob.groovy 重构为一个 class NewJob.groovy.
AJob.groovy
class AJob {
def testService
static triggers = {
cron name: 'myTrigger', cronExpression: "0 0 6 * * ?"
}
def group = "MyGroup"
def description = "A job with hello"
def execute() {
testService.hello("hello")
}
}
BJob.groovy
class BJob {
def testService
static triggers = {
cron name: 'myTrigger', cronExpression: "0 0 7 * * ?"
}
def group = "MyGroup"
def description = "B job with goodbye"
def execute() {
testService.hello("goodbye")
}
}
TestService.groovy
class TwitterService {
def hello(message){
print message
}
}
我认为这样做的唯一原因(相对于让 Cron 同时调用两者)是以某种方式同步它们,以保证在 B 之后调用 A。最明显的解决方案是如上所述:
class NewJob{
def execute() {
testService.hello("hello")
testService.hello("goodbye")
}
}
如果出于某种原因这些是异步调用(并且您仍想同步),则必须在线程上使用回调或创建单独的线程并以某种方式注入依赖项。不这样做的唯一原因是,如果您需要以某种方式记录 quartz 作业的依赖注入。更多详情?