对Grails中类似cron的调度的建议:每小时运行一次方法
假设我想在Grails中每小时运行一次以下方法对Grails中类似cron的调度的建议:每小时运行一次方法,grails,Grails,假设我想在Grails中每小时运行一次以下方法foo(): class FooController { public static void foo() { // stuff that needs to be done once every hour (at *:00) } } 在Grails中设置类似cron的调度最简单/推荐的方法是什么?Quartz插件: 添加Quartz作业调度功能 从1.0-RC3开始,该插件使用Quartz 2.1.x,不再使用Quartz 1.8.
foo()
:
class FooController {
public static void foo() {
// stuff that needs to be done once every hour (at *:00)
}
}
在Grails中设置类似cron的调度最简单/推荐的方法是什么?Quartz插件:
添加Quartz作业调度功能
从1.0-RC3开始,该插件使用Quartz 2.1.x,不再使用Quartz 1.8.x。如果你想使用Terracotta3.6+,这就是要使用的插件。这是因为另一个“quartz2”插件没有使用Terracotta 3.6所要求的JobDetailSiml类。有关更多信息,请参阅
可以找到完整的文档
如果不想添加另一个插件依赖项,另一种方法是使用JDK定时器类。只需将以下内容添加到
Bootstrap.groovy
def init = { servletContext ->
// The code for the task should go inside this closure
def task = { println "executing task"} as TimerTask
// Figure out when task should execute first
def firstExecution = Calendar.instance
def hour = firstExecution.get(Calendar.HOUR_OF_DAY)
firstExecution.clearTime()
firstExecution.set(Calendar.HOUR_OF_DAY, hour + 1)
// Calculate interval between executions
def oneHourInMs = 1000 * 60 * 60
// Schedule the task
new Timer().scheduleAtFixedRate(task, firstExecution.time, oneHourInMs)
}