I have the following Quartz job running in my application:
class ScraperJob {
def scraperService
static triggers = {
cron name: 'scraperTrigger', cronExpression: "0 0 * * * ?" // run every minute
}
def execute(){
try {
scraperService.storing()
log.info "${new Date()} - Scraping went smoothly."
}
catch(IOException) { // Connexion problem
log.error "${new Date()} - Method: parsing >> Connexion down or interrupted while parsing !"
}
catch(SAXException) { // Any SAXParser exception
log.error "${new Date()} - Method: parsing >> Parser error."
}
finally { // if not closed, the application crashes when the connexion fails
scraperService.slurper.finalize()
scraperService.parser.finalize()
}
}
}
I would like to know if it is possible to set the triggers
property from the Config.groovy
file. If it is开发者_如何学运维, could you explain how?
I have no idea if this would actually work because i'm not sure when the quartz jobs get configured but in theory it would seem to work. You could probably see how you could also make this much more dynamic if you have more than one job.
Config.groovy
quartz.yourCronJobName="0 0 * * * ?"
BootStrap.groovy
import org.codehaus.groovy.grails.commons.ConfigurationHolder as ConfigHolder
...
def cronExpression = ConfigHolder.config.yourCronJobName
ScraperJob.triggers.cronExpression = cronExpression
Good luck. Let me know if it helps.
Here is how I eventually did it:
Config.groovy
scraperJob= "0 * * * * ?"
ScraperJob.groovy
import org.codehaus.groovy.grails.commons.ConfigurationHolder as ConfigHolder
class ScraperJob {
static triggers = {
cron cronExpression: ConfigHolder.config.scraperJob // Calling the ScraperJob set in Config.groovy
}
def execute(){ ... }
}
精彩评论