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 expressionExplanation
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.

7
Leave a Reply

avatar
5 Comment threads
2 Thread replies
4 Followers
 
Most reacted comment
Hottest comment thread
6 Comment authors
Ankur Baderrakeshmanish singhswathiVarata Recent comment authors

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

  Subscribe  
newest oldest most voted
Notify of
bss
Guest
bss

thks for sharing keep up the good work!!

Varata
Guest
Varata

Very well explained, thanks

swathi
Guest
swathi

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
Guest
manish singh

i provide the classes.. can you mail me for more details . go4manish.singh@gmail.com

rakesh
Guest
rakesh

have u got any online tutorial

trackback

[…] Controllare questo link : stackextend.com/hybris/… […]

Ankur Bader
Guest
Ankur Bader

What if I give a past date in the trigger?