Dynamic Cron Job / Cron job scripting

If you need basic idea of Cron job, Please go through below link
Cron job overview

We all know that creating cron job is time consuming as it involves multiple steps

Example : We need to create java class, define spring bean,build and restart server etc.

But through dynamic scripting, Cron jobs can be created with very minimal steps and more importantly we don’t need to build and restart server.

Yes It allows the creation of cronjobs dynamically at run time.

Before jumping on how it can be achieved, lets understand below jargons

Script : It is an item type(corresponding table) called Script created by Hybris to store the script content
Script content can be written in any of the supported dynamic languages like “Groovy”

Scripting Job : It is also an item type called ScriptingJob created by Hybris to store new Service layer Job item,
This contains the reference to defined Script using ScriptURI

ScriptingJobPerformable : This is the actual Java class created by Hybris which contains the Perform method
This java class is responsible for executing scripted cronjob logic

Whenever the cronjob is triggered,The execution will be delegated to the ScriptingJobPerfomable class.

Lets see how we define Dynamic Scripting Jobs

Step 1

Define the Script as below

  1. INSERT_UPDATE Script; code[unique=true];content
  2. ;myFirstDynamicScript;println 'Welcome to Dynamic Scripting Job! '+ new Date()
INSERT_UPDATE Script; code[unique=true];content 
;myFirstDynamicScript;println 'Welcome to Dynamic Scripting Job! '+ new Date()

code: Represents the unique code within that type

Content : Content above is a groovy script to just print Welcome message with Today’s date
This can be a complex logic based on the requirement
Example : Deletion of few rows in some table.
Run the above impex in HAC, this will store the script in DB

Step 2

Define the Scripting Job as below

  1. INSERT_UPDATE ScriptingJob; code[unique=true];scriptURI
  2. ;myFirstDynamicScriptJob;model://myFirstDynamicScript
INSERT_UPDATE ScriptingJob; code[unique=true];scriptURI

code : Represents the unique code within that type
scriptURI : This will point to the location of Script
In our case, we stored Script in DB, we refer it using model://referenceToScript

Run the above impex in HAC, this will store the Scripting Job along with reference to Script in DB

Step 3

Define the Cronjob
This step is similar to traditional way of defining cron job

  1. INSERT_UPDATE CronJob; code[unique=true];job(code);singleExecutable;sessionLanguage(isocode)
  2. ;myFirstDynamicScriptCronJob;myFirstDynamicScriptJob;true;en
INSERT_UPDATE CronJob; code[unique=true];job(code);singleExecutable;sessionLanguage(isocode)

code: Represents the unique code within that type
job(code) : Holds the reference to the above defined job(in Step 2)

Step 4

Executing the CronJob

We will define Groovy script to execute the cron job as below

  1. def dynamicCJ = cronJobService.getCronJob("myFirstDynamicScriptCronJob")
  2. cronJobService.performCronJob(dynamicCJ,true)
def dynamicCJ = cronJobService.getCronJob("myFirstDynamicScriptCronJob")

Go to the scripting languages console in HAC,select Groovy as scripting language, Run the above script in Commit mode

Step 5

If you want to schedule the cron job instead of running it at directly,then we can avoid Step 4 and can be scheduled using Trigger as below

  1. INSERT_UPDATE Trigger;cronjob(code)[unique=true];cronExpression
  2. ;myFirstDynamicScriptCronJob; 0 0 * ? * * *
INSERT_UPDATE Trigger;cronjob(code)[unique=true];cronExpression
;myFirstDynamicScriptCronJob; 0 0 * ? * * *

Now above trigger defines the schedule to run the cron job for every one hour

Output of the defined scron job is very simple, it just prints Welcome message with Current date

Note : It is possible to define much complex logic instead of just Welcome message in the real time requirements.


One of the main limitations of this approach is that, since script content is stored as column value in DB,we can not store large chunk of content due to column length limit in DB

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on