4

Am using the Quartz plugin for Grails and have a simple job:

class MyJob{

   static triggers = {
       cron name: 'MyJobTrigger', cronExpression: '0 0/1 * * * ?'
   }
   def execute(){
       println "do some work"
   }
}

All works fine, job fires off every minute as expected.

Now I want the want the cron expression to be property driven so I can override in different environments. So Config.groovy contains the default value:

myJob.cron = '0 0/1 * * * * ?'

And I change my class to have:

   GrailsApplication grailsApplication

   static triggers = {
       cron name: 'MyJobTrigger', cronExpression: grailsApplication.config.myJob.cron
   }

When I run my code, I get this error:

Caused by MissingPropertyException: No such property: grailsApplication for class: MyJob

Assuming this is something to do with the way the MyJob class is loaded/initialised, the static triggers created before the GrailsApplication is injected??? This use of GrailsApplication is the usual way I get project properties.

How else can I have a property driven cron trigger?

tim_yates
  • 167,322
  • 27
  • 342
  • 338
shuttsy
  • 1,585
  • 2
  • 19
  • 34
  • That does appear to work, but ConfigurationHolder is marked as deprecated with a comment of 'Use dependency injection instead'. Any suggestions on what the alternative is? – shuttsy Sep 04 '13 at 13:15
  • Use `def grailsApplication` and `grailsApplication.config...` as you were, but in bootstrap – tim_yates Sep 04 '13 at 13:18

1 Answers1

3

I just ran into this, and adding the scheduling to Bootstrap.groovy worked just fine. In your MyJob class, set triggers to an empty closure:

class MyJob {

   static triggers = {
        // Job is scheduled in Bootstrap.groovy so that it can be externalized
    }

   def execute() {
       println "do some work"
   }
}

Then in your Bootstrap.groovy file, set it up like this:

class BootStrap {

    def grailsApplication

    def init = { servletContext ->
        MyJob.schedule(grailsApplication.config.myJob.cron)
    }
}
grantmcconnaughey
  • 10,130
  • 10
  • 37
  • 66
  • That doesn't work. I get a 'no property MyJob for class BootStrap' runtime error. Even if I What plugin are you using? I've just got the quartz 1.0-RC9. MyJob is a groovy class under grails-app/jobs/myApp, which is where the plugin's create-job command creates it. – shuttsy Sep 05 '13 at 18:54
  • 3
    Be sure to import that class at the top of Bootstrap.groovy. The schedule method is static, so you should call it using the class name (with a capital first letter, assuming you named the class with a capital letter). Also, I am using the same plugin and version you are. Quartz 1.9-RC9. – grantmcconnaughey Sep 05 '13 at 22:42
  • i tried your method but on bootstrap its shows an error( no such property myjob).on importing class job on bootstrap compiler showing a red line – Mohammed Shaheen MK Sep 30 '16 at 07:23