Cron Jobs Overview

Cron job functionality is used whenever we want to execute some tasks at some point of time.

It’s like an Alarm we set before we sleep 🙂 .

We set Alarm at 5AM, 6AM, 7AM and so on, so at that point of time, Alarm gets executed.
We can set the alarm for daily, alternate days, or any specific day as well.

Similarly Cron jobs can be set up to run only on one specific day or every day or every month, or every hour etc.

Now the main point here is Cron job basically do some work at specified time.

In Hybris, Cron job executes some business logic at specified time.

The business logic which gets executed by the cron job is called as Job.

What time it executes is defined by the Trigger.

What parameters and configuration required for job to execute is defined by the CronJob.


Let’s understand each of these terms in detail

Cron Job Overview


Cron Job :


Its an Item Type we define in the Items.xml with all the attributes required for the Job to be executed.
So we can say that CronJob provides the configuration details for the Job.

Hybris Out of the Box(OOTB) provides CronJob item Type which already has many attributes defined inside it like
Code,active,status.logText,startTime,endTime etc.

Check CronJob itemtype under below file for more details
/platform/ext/processing/resources/processing-items.xml

All these attributes are considered as a configuration for the Job.

In case our Job needs some additional configuration parameters other than what is provided with CronJob Item Type, then we should define a new CronJob Item Type in the *items.xml by extending the CronJob Item Type.

Example:

1
2
3
4
5
6
7
8
9
10
        <itemtype code="SampleCronJob" generate="true" autocreate="true"
           jaloclass="de.hybris.cronjob.jalo.SampleCronJob"
           extends="CronJob">
<attributes>
                <attribute type="java.lang.String" qualifier="sampleValue">
                    <persistence type="property"/>
                    <modifiers optional="false"/>
                </attribute>
            </attributes>     
        </itemtype>
        <itemtype code="SampleCronJob" generate="true" autocreate="true"
           jaloclass="de.hybris.cronjob.jalo.SampleCronJob"
           extends="CronJob">
<attributes>
                <attribute type="java.lang.String" qualifier="sampleValue">
                    <persistence type="property"/>
                    <modifiers optional="false"/>
                </attribute>
            </attributes>     
        </itemtype>


Job:


Job defines the logic to be executed.
We create a new class and extend AbstractJobPerformable class or implement JobPerformable interface.

Inside this newly created class, we provide the definition for perform(CronJobModel cronJobModel) method.
What we define inside this method is called as Business logic of the cron job.

And this class which contains the business logic is called as Job.

Note: This class should be defined as a Spring bean in order to consider this as a Job.

Example:

1
2
3
4
5
6
7
8
public class SampleJob extends AbstractJobPerformable<SampleCronJobModel>
{
    @Override
    public PerformResult perform(final CartRemovalCronJobModel job)
    {
//Logic goes here
    }
}
public class SampleJob extends AbstractJobPerformable<SampleCronJobModel>
{
	@Override
    public PerformResult perform(final CartRemovalCronJobModel job)
	{
//Logic goes here
	}
}

Job will have 2 main things,

1) Status: it is of type CronJobStatus enum
It gives the status of a Running cron Job which indicates whether it’s in Progress,or completed the execution.

possible values for the same can be checked in the CronJobStatus.java enum.

2) Result: it is of type CronJobResult enum
It gives the information about the last execution of the cron job
Like whether cron job has ended with Success or Failure

possible values for the same can be checked in the CronJobResult.java enum.

We can say that Cron Job is completed successfully with the below combination of CronJobStatus and CronJobResult
CronJobStatus.FINISHED and CronJobResult.SUCCESS

So PerformResult is returned by perform() method after the Job completes Business logic execution, which then can be used to determine the overall result and status of the Job.


Trigger:


It defines the scheduling of a Job which helps to decide when the Job has to be executed.
It defines scheduling using the attributes like seconds,minutes,hours etc or it can define it using Cron Expression.

see below link for details on how to define the Cron Expression
Cron Expression Guide

Trigger is actually linked to the Job indirectly which means Trigger will always be defined for CronJob and CronJob holds the actual Job to be executed.

So whenever we define Trigger, we define it for CronJob not for Job.
CronJob in turn identify the corresponding Job and makes sure it gets executed.

We will get more clarity on this with code in the next Article.

In Simple words,
Job defines what should be done, a Trigger says when it has to be done, and a CronJob defines the setup/configuration required for a Job.

About the Author

Karibasappa G C (KB)
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