How to start scheduler / timer when starting server in Grails

I have a timer class that should execute every 5 minutes. I want to run this timer class when tomcat starts for the first time. What would be the best approach to this in Grails?

Thanks. Jay Chandran

+4
source share
2 answers

If you need more flexibility than just a timer, you can use Quartz Plugin and configure the Cron job:

class MyTimerJob { static triggers = { // cron trigger for every 5 minutes cron name: 'myCronTrigger', cronExpression: '0 */5 * * * ?' } def execute = { // perform task } } 

To start Quartz when the application starts (as Jared said: not when tomcat starts), make sure your grails-app/conf/QuartzConfig.groovy has the following:

 quartz { autoStartup = true } 

autoStartup = true by default, so you probably won't need to change anything.

Using this plugin will save you from having to execute timer logic yourself.

+5
source

You cannot run the timer class when starting Tomcat, because it will not have access to your Grails application with all the data it needs to execute. You can start it when the Grails application starts by placing the desired code in Conf / bootstrap.groovy

+2
source