【问题标题】:Quartz job in GrailsGrails中的Quartz工作
【发布时间】:2012-08-17 17:03:25
【问题描述】:

考虑以下代码:

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

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

【问题讨论】:

    标签: grails quartz-scheduler


    【解决方案1】:

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

    class MyJob {
        static triggers = { }
    
        ...
    }
    

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

    【讨论】:

      【解决方案2】:

      您要完成的工作有点含糊。如果您想在“创建”触发器时执行此操作,那么您实际上不需要将其放入 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()
           }
         }
      }
      

      【讨论】:

      • 在我看来,每当你做一份工作时,默认触发器都会自动分配给它。当我添加自己的触发器时,这项工作将受到两个触发器的影响。我的主要问题是如何禁用默认触发器?应该是关于设置的。
      【解决方案3】:

      如果我明白你想要做什么,你会先安装石英配置:

      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?)
      

      【讨论】:

      • 我之前做过设置和作业调度,但似乎它禁用了所有触发器。什么也没有发生(quartz { autoStartup = false jdbcStore = false })
      • 它确实禁用了所有触发器......这就是“动态安排工作”部分的用武之地。也许你应该澄清你的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-21
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      相关资源
      最近更新 更多