scheduleWithFixedRate在grails项目中不起作用

时间:2014-10-16 02:33:04

标签: grails concurrency cron java-8

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

class BootStrap {
    def init = { servletContext ->
      def sd =  Executors.newSingleThreadScheduledExecutor()
      sd.scheduleAtFixedRate(new CriticalTask(), 0, 1, TimeUnit.MINUTES)
   }
}

已知CriticalTask类继承自java.util.TimerTask类。

CriticalTask​​类的run方法内部的代码运行正常。但是,调度不起作用。

我不知道这是web.xml配置还是其他问题?

这项工作假定每分钟都会启动。


另请注意我使用:

  • java8
  • grails 2.4.3

1 个答案:

答案 0 :(得分:2)

如果您使用的是ScheduledExecutorService,则不需要TimerTask,事实上,使用TimerTask可能会因为其混乱的结果而适得其反。它有效,因为TimerTask实现了Runnable,但run()之外的所有其他方法都没有意义。

即。仅当cancel()与班级Timer一起使用时,scheduledExecutionTime()TimerTask才有效。

方法ScheduledExecutorService.scheduleAtFixedRate会返回ScheduledFuture,其中包含getDelaycancel等可用于控制任务的方法。