【问题标题】:Quartz job triggering from Config.groovy从 Config.groovy 触发 Quartz 作业
【发布时间】:2011-11-15 13:24:45
【问题描述】:

我的应用程序中正在运行以下 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()
        }
  }
}

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

【问题讨论】:

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


    【解决方案1】:

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

    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 
    

    祝你好运。如果有帮助,请告诉我。

    【讨论】:

    • 啊啊,印象深刻!就是这样。非常比你
    • 哦,实际上我认为它有效,因为我的工作在我告诉他的时候被触发了,但这是因为它一直在后台运行。我还没有设法从 BootStrap 启动它,但我正在使用它 :)
    • 不应该设置所有的键吗? (名称和 cronExpression)。另外,如何添加多个触发器?并删除触发器?谢谢!
    【解决方案2】:

    这是我最终的做法:

    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-10-25
      • 1970-01-01
      • 2010-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多