Cron Jobs play an important role in any software development project. Cron Jobs allow developers and system administrators to create automated tasks responsible for running programming jobs in off-hours, or on a consistent schedule. In my mind, all developers should have the ability to create Cron Jobs that run important, automated tasks when needed. In this tutorial, I will walk you through the basics of creating a Cron Job with SAP Hybris Commerce.
If you haven't noticed already, I like to use Groovy scripts in my tutorials. For the following reasons, they are easy to write and easy to deploy. No long Hybris restart is required to implement a Groovy Script in SAP Hybris Commerce. This tutorial will schedule a simple hello world style email every night at midnight using a Groovy script.
1. Create an IMPEX Script that sends a Hello Hybris Architect Email using Groovy
INSERT_UPDATE Script; code[unique=true];content
;helloHybrisArchitectEmailScript;"email = de.hybris.platform.util.mail.MailUtils.getPreConfiguredEmail();
email.subject = 'Hello Hybris Architect';
email.msg = 'Hello Hybris Architect!';
This Groovy script is very simple. It sends an email to firstname.lastname@example.org. Please, please change the email address before as you perform this tutorial. I love to hear from you guys and gals, but I don't want to get spammed unnecessarily either 🙂
2. Create an IMPEX script for Scripting Job that calls the helloHybrisArchitectEmailScript from Step 1
INSERT_UPDATE ScriptingJob; code[unique=true];scriptURI
Play close attention how the scriptURI is the name of the Script Code from Step 1.
3. Create an IMPEX script that creates the Cron Job that will run the helloHybrisArchitectEmailScriptJob Scripting Job
INSERT_UPDATE CronJob; code[unique=true];job(code);singleExecutable;sessionLanguage(isocode)
This is an important step as we define the name of the Cron Job
4. Create an IMPEX Script that creates the Cron Job Trigger that will run the helloHybrisArchitectEmailScriptCronJob
This Cron Job Trigger will run once a day exactly at midnight (00:00) UTC.
5. Copy and paste the above Impex Scripts and run them in the Hybris HAC Impex Import Content console
6. Click on Import Content button
7. The Import Finished Successfully message will appear
8. Log into your Hybris Backoffice to confirm the Cron Job has been successfully saved
9. Once Logged into the Hybris Backoffice, search for Cronjobs in the search text box
The Cron Job helloHybrisArchitectEmailScriptCronJob will appear at the top of the Cron Job list.
10. Check your email box for an email if you changed the the email address in Step 1.
This tutorial is done. You now know how to write a Cron Job with SAP Hybris Commerce.