How to Write Your First Cron Job with SAP Hybris Commerce?

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.addTo('info@hybrisarchitect.com');
email.subject = 'Hello Hybris Architect';
email.msg = 'Hello Hybris Architect!';
email.send();"

This Groovy script is very simple. It sends an email to info@hybrisarchitect.com. 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
;helloHybrisArchitectEmailScriptJob;model://helloHybrisArchitectEmailScript

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)
;helloHybrisArchitectEmailScriptCronJob;helloHybrisArchitectEmailScriptJob;false;en

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

INSERT_UPDATE Trigger;cronJob(code)[unique=true];second;minute;hour;day;month;year;relative;active;maxAcceptableDelay
;helloHybrisArchitectEmailScriptCronJob;0;0;0;-1;-1;-1;false;true;-1

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

Hybris-Impex-Script-Cron-Job

6. Click on Import Content button

SAP-Hybris-Impex-Script-Cron-Job-Import-Content

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.

About the author

Marc is the Founder of HybrisArchitect.com. He enjoys helping others learn more about SAP Hybris Commerce. Marc has held the role of Hybris Architect at Exemplis and Nasty Gal. He is a long-time Java/Spring developer. Marc holds an M.S. Software Engineering from Carnegie Mellon University and a B.S. in Accountancy from California State University, Fresno. He can be reached at: info@hybrisarchitect.com

2 Comments

  1. Kumar November 8, 2018 Reply
    • Marc RaygozaAuthor November 12, 2018 Reply

Add a Comment

Your email address will not be published. Required fields are marked *