我有一个类似MyController.foo()
的方法,我想每天00:00执行它。
我已经看到一些使用TimerTask的解决方案,其延迟时间以秒为单位,但以天为单位的更改会带来一些错误。
那么……实现这一目标的最有效方法是什么?
最佳答案
首先,您需要使用spring boot注释启用调度:
@EnableScheduling
class Application extends GrailsAutoConfiguration {
static void main(String[] args) {
GrailsApp.run(Application, args)
}
}
然后可以将@Scheduled注释放在您的方法上:
@Scheduled(cron="00 00 * * *")
def foo() {
//do something
}
Cron表达式
00 00 * * *
表示在每个中间执行一次。
关于grails - 每天午夜执行任务-Grails 3.2.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46424549/