我的应用程序中运行以下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
属性。如果是,你能解释一下是怎么回事吗?
我不知道这是否真的有效,因为我不确定石英作业何时配置,但理论上它似乎是有效的。你可能会看到,如果你有多个工作,你也可以让它更有活力。
Config.groovy
quartz.yourCronJobName="0 0 * * * ?"
中
import org.codehaus.groovy.grails.commons.ConfigurationHolder as ConfigHolder
...
def cronExpression = ConfigHolder.config.yourCronJobName
ScraperJob.triggers.cronExpression = cronExpression
好运。如果有帮助,请告诉我。
我最终是这样做的:
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(){ ... }
}