3

考虑以下代码:

class MyJob {  
    def execute() {
        println "Hello at->"+new Date()
    }
}

当我运行此代码时,它每分钟都开始运行而不分配任何触发器。我怎么能禁用这个属性?每当我触发时,我都想开始这项工作。

4

3 回答 3

3

如果您想禁用默认触发器而不是在启动时分配触发器,那么您只需要在类中设置一个空的 triggers 闭包。

class MyJob {
    static triggers = { }

    ...
}

这会将闭包的触发器(无触发器)分配给作业,而不是默认触发器。

于 2012-08-18T14:17:50.373 回答
0

你想要完成的事情有点模糊。如果您想在“创建”触发器时执行此操作,那么您实际上不需要将其放入 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()
     }
   }
}
于 2012-08-17T17:50:57.857 回答
0

如果我了解您想要做什么,您将首先安装石英配置:

grails install-quartz-config

然后禁用自动启动:

quartz {
    autoStartup = false
    jdbcStore = false
}

然后在您的应用程序中动态安排作业

// 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?)
于 2012-08-17T19:34:16.830 回答