Abdennour TOUMI
Abdennour TOUMI

Reputation: 93203

scheduleWithFixedRate does not work in grails project

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

Known that CriticalTask class inherits from java.util.TimerTask class.

The code inside run method of CriticalTask class works fine. However, the scheduling does not work.

i don't know if it is an issue of web.xml configuration or another thing?

This job is assumed to be launch for every minute.


Be informed also that i use :

Upvotes: 0

Views: 141

Answers (1)

Holger
Holger

Reputation: 298183

If you are using a ScheduledExecutorService, you don’t need a TimerTask, in fact, using a TimerTask can be very counter-productive due to its confusing results. It works, because TimerTask implements Runnable, but all other methods besides run() have no meaning then.

I.e. cancel() and scheduledExecutionTime() work only if you use TimerTask together with the class Timer.

The method ScheduledExecutorService.scheduleAtFixedRate returns a ScheduledFuture which has methods like getDelay and cancel which you can use to control the task.

Upvotes: 2

Related Questions