Where to place a recurring task in a Grails application?

I am studying grails, and I would like to include a recurring task that fires every five seconds while my application is running, and should have access to my domain objects and the like. What is the right way to achieve this in Grail?

I thought about starting Timer in BootStrap.groovy , but this could be disposed of and kill the timer.

+4
source share
2 answers

I never used it, but the Grails Quartz plugin should let you do what you want.

+9
source

Quartz ( http://grails.org/plugin/quartz ) allows you to define repetitive tasks in the same way that a CRON task will run on a single server.

You can install it in your project as follows:

 grails install-plugin quartz 

After installing it, you can create a new task with

 grails create-job 

Then you can configure it like this:

 class MyJob { static triggers = { simple name: 'mySimpleTrigger', startDelay: 60000, repeatInterval: 1000 } def group = "MyGroup" def execute(){ print "Job run!" } } 

If you prefer CRON formatting, you can assign a trigger in the same format:

  static triggers = { cron name: 'myTrigger', cronExpression: "0 0 6 * * ?" } 

However, since the grails application can be deployed on multiple servers (QA, production, deployment, load balancing ...), the quartz plugin allows a particular process to run regardless of which server it is deployed to.

One thing you should pay attention to is the synchronization of the server clock, otherwise you may encounter some strange functionality (especially if several servers use the same database).

+2
source

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


All Articles