Grails cron type planning guidelines: run the method once per hour

Suppose I want to run the following foo() method each time in Grails:

 class FooController { public static void foo() { // stuff that needs to be done once every hour (at *:00) } } 

What is the easiest / recommended way to set up such graphical planning in Cron in Grails?

+4
source share
2 answers

Quartz Plugin: http://grails.org/plugin/quartz

Adds quartz work scheduling features ...

Starting with 1.0-RC3, this plugin uses Quartz 2.1.x and no longer uses Quartz 1.8.x. If you want to use Terracotta 3.6+, this is the plugin to use. This is because another quartz2 plugin does not use the JobDetailsImpl class, which is required by Terracotta 3.6. See https://jira.terracotta.org/jira/browse/QTZ-310 for more information ...

Full documentation can be found here.

+11
source

If you do not want to add another plugin dependency, the alternative is to use the Timer JDK class. Just add the following to 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) } 
+3
source

Source: https://habr.com/ru/post/1306867/


All Articles