Hi,
I have a timer class scheduled to execute every 5 mins. I want to start this timer class when tomcat starts up the first time. What would be the best approach to do this in Grails?
Thank You. Jay Chandran
Hi,
I have a timer class scheduled to execute every 5 mins. I want to start this timer class when tomcat starts up the first time. What would be the best approach to do this in Grails?
Thank You. Jay Chandran
You can't start the timer class when Tomcat starts since it wouldn't have access to your Grails app with all the associated data it needs to execute. You can start it when your Grails app starts though by putting the necessary code in Conf/bootstrap.groovy
If you need more flexibility than just a timer, you can use the Quartz Plugin and configure a 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 on application startup (like Jared said: not on tomcat startup), make sure your grails-app/conf/QuartzConfig.groovy
has the following:
quartz {
autoStartup = true
}
autoStartup = true
is the default, so you probably won't need to change anything there.
Using this plugin will save you from having to handle the timer logic yourself.