Home Java Task Scheduler and Sending Mail in Grails

Task Scheduler and Sending Mail in Grails

by admin

This article is for people who are already familiar with Grails, if you are not, read here Now to the point. Sending mail is quite a simple and necessary task, it is a bit more difficult to implement a periodic check of some event. And if you combine these two tasks you get a normal task, which requires a certain amount of time. Grails is created to make our life easier and relieve us from routine work. In order to implement periodic event checking we will use Quartz (more precisely its plugin under Grails ), and for easy mailing the plugin Mail Plugin will be just right.
The Mail Plugin simply connects via smtp and sends mail. When you download it you don’t choose anything or answer anything, you just click download on its page. Quartz, on the other hand, does what you would do by hand if you had to create a thread which you, directed to a service in RAM. Of course you could create a separate jar and run it with cron, but this method will not be discussed here. When I downloaded quartz, I downloaded a stable version of Quartz (grails-quartz-0.4.2.zip – Grails-Quartz 0.4.2 (Stable Release)). Installing plugin
After you have downloaded both plugins, find where you installed the grails, copy the plugins in the directory plugins. The next step is in the command line :
Go to the directory where we have the Grails project and write the following :
grails install-plugin quartz
Task Scheduler and Sending Mail in Grails
After successfully installing quartz, install mail,
grails install-plugin mail
For installation you need to make active the line in BuildConfig.groovy file. Namely the line where mavenCentral() method is called.
Task Scheduler and Sending Mail in Grails
After installing the plugins, all we have to do is configure them. Let’s start with Quartz. First we need to create a Job. This is done using the command
grails create-job Habra where Habra is the name
Grails will create a class for us inside the Jobs folder and name it HabraJob.groovy.
Let’s tweak it so that after 10 seconds it says hello to HabraJob every 5 seconds in the console and tells it the current time :
class HabraJob {
static triggers = {
simple name: 'HabraTrigger', startDelay: 2000, repeatInterval: 2000
def group = "HabraGroup"
def execute() {
println "Hello Habrauser "+new Date()

In the code, first we create a Trigger named HabraTrigger, set in the same block startDelay – the time after which the first run will occur, repeatInterval – repeat time. Then we create a group named HabraGroup and the execute() method itself – which is responsible for what to execute at each run. Here in the description section you can get acquainted with the Groovy syntax of Quartz.
Now let’s move on to sending mail with the Mail Plugin. To do this, we need to write in the Config.groovy file the settings of :
grails {
mail {
host = "smtp.gmail.com"
port = 465
username = "********@gmail.com"
password = "**********"
props = ["mail.smtp.auth":"true",

Almost done, all that’s left is to write a call to send mail that looks like this :

sendMail {
to "user@habrahabr.com"
subject "Hello %habrauser%"
body 'How are you?'

That’s it, if you copy this code into execute() method and set the start time, the task will be done. Good luck to all.

You may also like