Everything about CronJobs in Hybris (Part 1)
A 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).
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.
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.
Software Craftsmanship, Stackextend author and Full Stack developer with 6+ years of experience in Java/Kotlin, Java EE, Angular and Hybris…
I’m Passionate about Microservice architectures, Hexagonal architecture, Event Driven architecture, Event Sourcing and Domain Driven design (DDD)…
Huge fan of Clean Code school, SOLID, GRASP principles, Design Patterns, TDD and BDD.
thks for sharing keep up the good work!!
Very well explained, thanks
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
i provide the classes.. can you mail me for more details . [email protected]
have u got any online tutorial
What if I give a past date in the trigger?
that job will never run… because that time will never come…
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.
How to trigger cronjob without trigger?
Thankies Mouad !!
‘Everything about Cronjobs in Hybris’….Hard to say that this is a bare minimum