Sherif
Sherif

Reputation: 93

Injecting service in Quartz job in Grails

developing application using Grails 2.5.1 i used Quartz plugin , and created a job successfully , but when i inject a service in this job i get org.quartz.JobExecutionException: java.lang.NullPointerException

here is the Job's code:

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

def execute() {

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

Upvotes: 2

Views: 1022

Answers (1)

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27200

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. 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 which looks like this:

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
    }
}

Upvotes: 3

Related Questions