This article is designed for those who already have a first acquaintance with the Grails, but if this has not happened yet, you can read it
here . Now to the point. Sending mail is quite simple and necessary task, a little more difficult to implement periodic checking of any event. And if you connect these two tasks, you get a normal task that requires certain time-consuming. Grails - created to make life easier for us, frees us from routine work. In order to implement periodic checking for events, we will use
Quartz (or rather its
plugin under Grails ), and for convenient sending of mail, the
Mail Plugin plugin will be just right.
Mail Plugin simply connects via smtp and sends mail. When downloading it, we don’t select anything and don’t respond to anything, just click download on its page. Quartz does what you do with your hands, if you had to create a thread that you sent to the RAM for service. Of course, it was possible to create a separate jar and run it using cron, but this method will not be considered here. When I downloaded the quartz, I took not the last but the stable version of Quartz (grails-quartz-0.4.2.zip - Grails-Quartz 0.4.2 (Stable Release)).
Plug-in installationAfter both plugins have been downloaded, we find where the grails have been installed, copy the plugins to the plugins directory. The next step is already on the command line:
Go to the directory where we have the Grails-project and write the following:
grails install-plugin quartz

After successful installation of quartz, we install mail,
grails install-plugin mail
To install, it is required to make active the commented string in the BuildConfig.groovy file. Namely, the line where the mavenCentral () method is called.

')
After installing the plugins, we just have to configure them. Let's start with Quartz. First we need to create a job. This is done with the command
grails create-job Habra
where Habra is the nameGrails will create a class for us inside the Jobs folder and name it HabraJob.groovy.
Let's adjust it so that it 10 seconds after launch, every 5 seconds in the console, greet Habrayuser and tell him 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, a trigger is first created with the name HabraTrigger; we set startDelay in the same block — the time after which the first launch will occur; repeatInterval — the repetition time. Next, a group is created with the name HabraGroup and the execute () method itself is responsible for what to execute at each launch. Here, in the description section, you can get a closer look at Groovy’s Quartz syntax.
Now let's move on to sending mail using the Mail Plugin. To do this, you need to write in the Config.groovy file settings:
grails {
mail {
host = "smtp.gmail.com"
port = 465
username = "********@gmail.com"
password = "**********"
props = ["mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"465",
"mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback":"false"]
}
}
Almost done, it remains only to write a call to send mail, which looks like this:
sendMail {
to "user@habrahabr.com"
subject "Hello %habrauser%"
body 'How are you?'
}
That's all, if you copy this code into the execute () method and set the start time, the task will be executed. Good luck everyone.