【发布时间】:2012-08-17 17:03:25
【问题描述】:
考虑以下代码:
class MyJob {
def execute() {
println "Hello at->"+new Date()
}
}
当我运行此代码时,它每分钟都开始运行,而无需分配任何触发器。我怎么能禁用这个属性?我想在触发时开始这项工作。
【问题讨论】:
考虑以下代码:
class MyJob {
def execute() {
println "Hello at->"+new Date()
}
}
当我运行此代码时,它每分钟都开始运行,而无需分配任何触发器。我怎么能禁用这个属性?我想在触发时开始这项工作。
【问题讨论】:
如果你想禁用默认触发器而不是在启动时分配触发器,那么你只需要在类中设置一个空的 triggers 闭包。
class MyJob {
static triggers = { }
...
}
这会将闭包的触发器(没有)分配给作业,而不是默认触发器。
【讨论】:
您要完成的工作有点含糊。如果您想在“创建”触发器时执行此操作,那么您实际上不需要将其放入 cron 作业中。
但是,如果您想在某个事件之后激活它。然后您仍然会创建一个触发器,但在执行之前进行某种检查。例如。
class MyJob {
static triggers = { .... create the schedule
def execute() {
// instead of create a trigger, you create a temp file that would allow/prevent
// the execution
def runIt = new File(runFile.txt)
if (runIt.exists()) {
println "Hello at->"+new Date()
}
}
}
【讨论】:
如果我明白你想要做什么,你会先安装石英配置:
grails install-quartz-config
然后禁用自动启动:
quartz {
autoStartup = false
jdbcStore = false
}
然后在你的应用程序中dynamically schedule the job:
// creates cron trigger;
MyJob.schedule(String cronExpression, Map params?)
// creates simple trigger: repeats job repeatCount+1 times with delay of repeatInterval milliseconds;
MyJob.schedule(Long repeatInterval, Integer repeatCount?, Map params?) )
// schedules one job execution to the specific date;
MyJob.schedule(Date scheduleDate, Map params?)
//schedules job's execution with a custom trigger;
MyJob.schedule(Trigger trigger)
// force immediate execution of the job.
MyJob.triggerNow(Map params?)
【讨论】: