2

我的应用程序中正在运行以下 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如果是,你能解释一下怎么做吗?

4

2 回答 2

5

我不知道这是否真的有效,因为我不确定石英作业何时配置,但理论上它似乎有效。如果您拥有一份以上的工作,您可能会看到如何使这变得更有活力。

配置文件

quartz.yourCronJobName="0 0 * * * ?"

BootStrap.groovy

import org.codehaus.groovy.grails.commons.ConfigurationHolder as ConfigHolder
...
def cronExpression = ConfigHolder.config.yourCronJobName
ScraperJob.triggers.cronExpression = cronExpression 

祝你好运。让我知道它是否有帮助。

于 2011-09-13T10:43:13.847 回答
3

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(){ ... }
}
于 2011-09-14T18:02:00.113 回答