Quartz job triggering from Config.groovy

2019-01-24 23:04发布

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?

2条回答
叛逆
2楼-- · 2019-01-24 23:35

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(){ ... }
}
查看更多
Juvenile、少年°
3楼-- · 2019-01-24 23:55

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.

查看更多
登录 后发表回答