Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails - using property value for cron expression in scheduled job [duplicate]

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?

like image 966
shuttsy Avatar asked Oct 22 '25 02:10

shuttsy


1 Answers

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)
    }
}
like image 174
grantmcconnaughey Avatar answered Oct 23 '25 15:10

grantmcconnaughey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!