Everything about CronJobs in Hybris (Part 1)

Hybris Logo

Cronjob (Cron job) in Hybris is a task that is executed manually by a user (Administrator) or started automatically via a Trigger, it runs in the background as a single Thread.

Typically Cronjobs are used for long and periodic processes, for example, Catalog Synchronization, Data Indexation, Carts Cleaning, Backups…

In this rrticle, I will show you how to create and to use Cronjobs, Jobs and Triggers, and what the difference between them.

1. Concept overview

There are 3 different components that interact with one another : Cronjob, Job and Trigger (we will see each component in detail).

concept of cronjob in hybris

 

1.1. Trigger

Let’s start with the Trigger because it is the easiest one to explain and to use 🙂

Trigger is used to schedule when the Job should be executed using Cron expressions.

Don’t worry if you can’t figure out the morning of the Cron experiences symbols, personally I use CronMaker to generate Cron expressions for my Triggers, it will make your life easier (go and play with it).

This is an example of some Cron expressions:

Cron expression Explanation
0 0/10 * 1/1 * ? * Job will be executed every 10 Minutes.
0 0 12 ? * MON * Job will be executed every Monday at 12:00.

1.2. Job

The Job is the most important part here, it’s the one who holds the logic to be done (synchronization, indexation, cleaning,…).

So how to create a Job ?

The Job consists of 2 parts a Model and a Jalo:

  • Model is the identity of the Job for Hybris
  • Jalo is a Java class where we will write the business logic to be executed.

Let’s write a Hello World Job as an example:

First create a HelloWorldJobModel extends from JobModel.

<itemtype code="HelloWorldCustomerJob" extends="Job" 
	autocreate="true" generate="true" 
	jaloclass="com.stackextend.training.core.jalo.HelloWorldCustomerJob">

	<attributes>
		<!-- no attribute needed -->
	</attributes>
	
</itemtype>

Then in the HelloWorldCustomerJob (Jalo) that extends from Job (Jalo) implement the method performCronJob(Cronjob) and write the logic to be done on it.

public class HelloWorldCustomerJob extends Job {

    public HelloWorldCustomerJob () {}

	@Override
    protected CronJobResult performCronJob(CronJob cronJob) throws AbortCronJobException {
        
        System.out.println("Hello World from Job!");
		
    }
}

Congratulation! You have successfully created your very first Job 🙂

Well, now it’s the time to tell you to forget everything I told about creating a Job 😛 because this not how it’s should be done actually!

Thanks to Hybris teams, we don’t need to go all over this to create a Job, they did all the dirty job for us.

Actually we need just to create a simple Bean Spring that implements JobPerformable and put the business logic on it, refer to the part 2 to see how to create a Job properly.

create job performable and servicelayer job in hybris

1.3. CronJob

For a Job to do the task properly it may need some inputs (configuration), the Cronjob is the holder of this configuration, we can create as many configurations (instances of CronJobModel) we want, each instance of the Cronjob will define a single run of the Job.

If we suppose that we have a Job called LogExtractorJob that extracts the Server Logs in a interval of time, the inputs to this Job would be start date and end date (interval):

  • We can create an instance of LogExtractorJob with start date as A and end date as B.
  • And another instance with start date as X and end date as Y.

In the next section of this article we will have o concrete example on how to create a CronJob.

2. Conclusion

Cronjobs (Cron Jobs) in Hybris is made to take care of the long and repetitive tasks that should be executed in background manually or schedule in time.

  • Job : is the part hosting the logic to be done.
  • Cronjob : it holds the information to be passed to the Job to perform properly.
  • Trigger : is the component responsible for scheduling the task in time.
5 8 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

11 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
bss
bss
6 years ago

thks for sharing keep up the good work!!

Varata
Varata
6 years ago

Very well explained, thanks

swathi
swathi
6 years ago

Hi Mouad

This is swathi. I am new to hybris. i want to learn hybris, r u providing any online classes on hybris. please let me know

Thanks

manish singh
manish singh
Reply to  swathi
6 years ago

i provide the classes.. can you mail me for more details . [email protected]

rakesh
rakesh
Reply to  swathi
5 years ago

have u got any online tutorial

Ankur Bader
Ankur Bader
5 years ago

What if I give a past date in the trigger?

Nidhi Baghel
Nidhi Baghel
Reply to  Ankur Bader
4 years ago

that job will never run… because that time will never come…

Kiranraj Swamy
Kiranraj Swamy
Reply to  Ankur Bader
4 years ago

If you create a trigger, whose activation date is set to a point in the past, the trigger is fired immediately after it is created.

Sagar
Sagar
3 years ago

How to trigger cronjob without trigger?

Aya hassani
Aya hassani
2 years ago

Thankies Mouad !!

pqvazmi315@vigoneo.com
1 year ago

‘Everything about Cronjobs in Hybris’….Hard to say that this is a bare minimum

11
0
Would love your thoughts, please comment.x
()
x