从Config.groovy触发Quartz作业

时间:2011-09-12 18:05:53

标签: grails cron quartz-scheduler grails-plugin grails-config

我在我的应用程序中运行了以下Quartz作业:

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

我想知道是否可以从triggers文件设置Config.groovy属性。如果是,你能解释一下吗?

2 个答案:

答案 0 :(得分:5)

我不知道这是否真的有效,因为我不确定何时配置石英作业,但从理论上看它似乎有效。如果您有多个工作,您可能会看到如何使这更加动态。

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 
祝你好运。如果有帮助,请告诉我。

答案 1 :(得分:3)

以下是我最终的表现:

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