在 Grails 的 Quartz 作业中注入服务
Injecting service in Quartz job in Grails
使用 Grails 2.5.1
开发应用程序 我使用 Quartz plugin 并成功创建了一个作业,但是当我在此作业中注入服务时,我得到 org.quartz.JobExecutionException: java.lang.NullPointerException
这是作业的代码:
class EveryMonthJob {
def usersUtilsService
static triggers = {
cron name: 'EveryOneMonthJob', cronExpression: "* 31 2 L * ?"
}
def execute() {
usersUtilsService.testMe() // getting the exception here
}
}
有多种原因可能无法正常工作。如果您自己创建作业的一个实例(而不是 Spring 创建实例并对其进行依赖注入),这将解释为什么引用为空。另一种解释可能是您的 属性 名称有误。
查看 https://github.com/jeffbrown/sherifquestion. That is a Grails 2.5.1 app that does just what you are describing and it works fine. See https://github.com/jeffbrown/sherifquestion/blob/e0179f836314dccb5f83861ae8466bfd99717995/grails-app/jobs/demo/EveryMonthJob.groovy 中的项目,如下所示:
class EveryMonthJob {
// generally I would statically type this property but
// am leaving it dynamically typed top be consistent with
// a question being asked...
def usersUtilsService
static triggers = {
simple repeatInterval: 5000l // execute job once in 5 seconds
}
def execute() {
usersUtilsService.testMe() // this works fine
}
}
使用 Grails 2.5.1
开发应用程序 我使用 Quartz plugin 并成功创建了一个作业,但是当我在此作业中注入服务时,我得到 org.quartz.JobExecutionException: java.lang.NullPointerException
这是作业的代码:
class EveryMonthJob {
def usersUtilsService
static triggers = {
cron name: 'EveryOneMonthJob', cronExpression: "* 31 2 L * ?"
}
def execute() {
usersUtilsService.testMe() // getting the exception here
}
}
有多种原因可能无法正常工作。如果您自己创建作业的一个实例(而不是 Spring 创建实例并对其进行依赖注入),这将解释为什么引用为空。另一种解释可能是您的 属性 名称有误。
查看 https://github.com/jeffbrown/sherifquestion. That is a Grails 2.5.1 app that does just what you are describing and it works fine. See https://github.com/jeffbrown/sherifquestion/blob/e0179f836314dccb5f83861ae8466bfd99717995/grails-app/jobs/demo/EveryMonthJob.groovy 中的项目,如下所示:
class EveryMonthJob {
// generally I would statically type this property but
// am leaving it dynamically typed top be consistent with
// a question being asked...
def usersUtilsService
static triggers = {
simple repeatInterval: 5000l // execute job once in 5 seconds
}
def execute() {
usersUtilsService.testMe() // this works fine
}
}