Quartz job triggering from Config.groovy

本小妞迷上赌 提交于 2019-11-27 22:47:02

问题


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?


回答1:


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.




回答2:


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(){ ... }
}


来源:https://stackoverflow.com/questions/7391976/quartz-job-triggering-from-config-groovy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!