Cron Jobs Subsidiary Information

How Trigger gets Triggered ?

The execution of a CronJob is taken care by the Trigger as per the scheduling we define in that Trigger using Cron Expression.

So Who will be responsible for invoking the Trigger ?

Who evaluates the Cron expression ?

TaskEngine: For every trigger there is always one Task item gets created in Hybris.

TaskEngine will keep on polling the Tasks for every X seconds which we configure in the file as below

  1. cronjob.trigger.interval=30

By default the timer is set to 30 seconds, so if we want to change this value we can define it with new value in the file as mentioned above.

Here X is 30 seconds, so for every 30 seconds Timer task fires a DB query to check for any triggers to be fired , if any triggers matches the current time or its overdue then Trigger is fired immediately.

Since Timer Task polls a DB every X seconds as we specify in the file, we should not give its value too less unless and until its very much essential otherwise more DB calls will be made unnecessarily.

So this Task polled by TaskEngine will check the cron expression whether it matches or exceeded the current time, if so it will immediately execute the Cron job.

Aborting a cron job

How to abort or terminate a running cron job ?

Assume that our cron job has been running from the past one hour, now i want to abort it, then we can do that in Hyrbis

Job is abortable only if it satisfies one of the below conditions

1) isAbortable() method in cronjob performable class should be overridden to return true

2) Define a new property in the *-spring.xml file where we defined our Job’s bean definition

  1. <property name="abortable" value="true"/>
<property name="abortable" value="true"/>

Now Job is abortable and can be done using below code

  1. cronJobService.requestAbortCronJob(running_cronjob_code);

After aborting the cron job,the cron job results should be with result as ERROR and status as ABORTED

Setting session related attributes to the cron job

Some time we write a cron job whose logic requires some session attributes like user,sessionLanguage and sessionCurrency etc.

So how do we set these attributes to the cron job so that we can access them while writing the logic of the cron job ?

It can be done in any one of the 2 ways listed below

1) Set the session attributes through impex

2) Set the session attributes through code

lets see the code for the same

1) Session attributes through impex

  1. INSERT_UPDATE CronJob;code[unique=true];job(code);sessionUser(uid);sessionLanguage(isocode);sessionCurrency(isocode)
  3. ;myCronJob;myJob;user1;en;EUR
INSERT_UPDATE CronJob;code[unique=true];job(code);sessionUser(uid);sessionLanguage(isocode);sessionCurrency(isocode)


Here when we define the instance of Cron Job , we also specify the session attributes like user,currency and language

Above session values can be used while writing the logic inside Perform() method of Job

2) Session attributes through Code

  1. CronJobModel myCronJob=modelService.create(CronJobModel.class);
  3. // set JobModel to CronJobModel
  5. myCronJob.setSessionUser(mySessionUserModel);
  7. myCronJob.setSessionLanguage(mySessionLanguage);
  9. myCronJob.setSessionCurrency(mySessionCurrency);
  13. cronJobService.performCronJob(myCronJob);
CronJobModel myCronJob=modelService.create(CronJobModel.class);

// set JobModel to CronJobModel





Here we have created the instance of CronJobModel and set all the session attributes then saved the Cron job model to DB.

Then we are calling performCronJob() method by passing the Cron job code.

Cron Jobs in Clustered system

How cron jobs run in a clustered hybris suite, will it run in all the nodes or only on single node ?

Remember Cron jobs always executes on a single node only.

we can specify the Node Identifier for the cron job to indicate on which Node it has to run using the below code

  1. CronJobModel myCronJob=modelService.create(CronJobModel.class);
  3. // set JobModel to CronJobModel
  5. //set session attributes if required
  7.  myCronJob.setNodeID(3);
  11. cronJobService.performCronJob(myCronJob);
CronJobModel myCronJob=modelService.create(CronJobModel.class);

// set JobModel to CronJobModel

//set session attributes if required



Now above cron job runs only in the Node 3 of the clustered environment.

If we don’t set any Node Id for the Cron Job then Cron job can be executed by any Node within the cluster but only one Node at a time.

Running Cron Job through Ant

We can also run the cron job using Ant command as below

  1. ant runcronjob -Dcronjob=myCronJob -Dtenant=master
ant runcronjob -Dcronjob=myCronJob -Dtenant=master

Above command runs the cron job called myCronJob in a master tenant

Cron Job execution during the Server startup

Whenever we start the Hybris server, each trigger will be evaluated first , if the trigger evaluated time is overdue or matches the current time then Trigger will be fired which means Cron jobs will be executed immediately.

How much overdue time we can allow for the triggers to get fired during the server startup ?
This can be done by setting maxAcceptableDelay attribute to the Trigger.
This attribute should have the value in seconds

If its value is set as 300, then delay of 5 minutes is allowed for the overdue triggers to be fired at the server startup.

So if the trigger defines the cron expression to run the job at 5 PM , server started at 5:05 PM then above trigger will be fired(as 5 minutes of delay is accepted).

If the server started at 5:06 PM then trigger will not be fired as it does not allow the delay of more than 5 minutes.

About the Author

Founder of
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

Connect with me on Facebook for more updates

Share this article on