在Grails中的Quartz作业中注入服务

时间:2017-07-13 12:26:57

标签: grails quartz-scheduler

使用Grails 2.5.1开发应用程序我使用了Quartz plugin,并成功创建了一个作业,但是当我在这个作业中注入服务时,我得到org.quartz.JobExecutionException: java.lang.NullPointerException

这是Job的代码:

class EveryMonthJob {
def usersUtilsService
static triggers = {
    cron name: 'EveryOneMonthJob', cronExpression: "* 31 2 L * ?" 
}

def execute() {

    usersUtilsService.testMe() // getting the exception here 
        }
} 

1 个答案:

答案 0 :(得分:3)

There are any number of reasons that might not work. If you are creating an instance of the job yourself (as opposed to Spring creating the instance and subjecting it to dependency injection), that would explain why the reference is null. Another explanation could be that you have the property name wrong.

See the project at https://github.com/jeffbrown/sherifquestion中每个

的悬停。这是一个Grails 2.5.1应用程序,可以完成您所描述的内容并且工作正常。请参阅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
    }
}