Everything about CronJobs in Hybris (Part 2)
In the first part of this article, we explored the basics of Cronjobs, Jobs and Triggers, and also the differences between them.
In this part of the article we are going to make a concrete example of a Cronjob, a HelloWorldCronJob
.
HelloWorldCronJob will take a firstName as an input and print Hello firstName as an output.
1. CronJob
Create a new item type extends CronJob, with name HelloWorldCronJob
and attribute firstName.
<itemtype code="HelloWorldCronJob" extends="CronJob" jaloclass="com.stackextend.training.core.jalo.HelloWorldCronJob">
<attributes>
<attribute qualifier="firstName" type="java.lang.String">
<modifiers/>
<persistence type="property"/>
</attribute>
</attributes>
</itemtype>
Run ant all command, then run update system from the HAC.
Note that you don’t need to create a new Cronjob if your Job does not require inputs.
2. Job
Create a Job where we are going to add our Logic (display Hello firstName) :
First create a class that extends from AbstractJobPerformable
and implement the perform()
method:
public class HelloWorldJob extends AbstractJobPerformable<HelloWorldCronJobModel> {
@Override
public PerformResult perform(HelloWorldCronJobModel cronJobModel) {
try {
// Retrieve firstName from the cronJob
String firstName = cronJobModel.getFirstName();
// Display Hello firstName
System.out.println("Hello " + firstName);
// In case of success return result: SUCCESS and status: FINISHED
return new PerformResult(CronJobResult.SUCCESS, CronJobStatus.FINISHED);
} catch(Exception e) {
// In case of exception return result: ERROR and status: ABORTED
return new PerformResult(CronJobResult.ERROR, CronJobStatus.ABORTED);
}
}
}
Register the HelloWorldJob
class as a bean into Spring context.
<bean id="helloWorldJob" class="de.hybris.merchandise.core.job.HelloWorldJob"
parent="abstractJobPerformable" >
<!-- Inject other beans here if it's needed -->
</bean>
Create an instance of the ServicelayerJob
with springId
as helloWorldJob
.
INSERT_UPDATE ServicelayerJob ;code[unique=true] ;springId
;helloWorldJob ;helloWorldJob
Create an instance of our HelloWorldCronJob
, give it a firstName and the instance of ServicelayerJob
that we have just created.
INSERT_UPDATE HelloWorldCronJob ;code[unique=true] ;job(code) ;firstName; sessionLanguage(isocode); sessionCurrency(isocode)
;helloWorldCronJob ;helloWorldJob ;Mouad; en ;EUR
Our Cronjob is ready to be used now, we should be able to start it manually from HMC or Backoffice.
3. Trigger
Create a Trigger to run the Cronjob at specific period of time, every Sunday at 12:00 for example.
INSERT_UPDATE Trigger ;cronjob(code)[unique=true] ;cronExpression
;helloWorldCronJob ;0 0 12 ? * SUN *
You can find the next activation time of our CronJob from HMC.
4. Conclusion
To Create a Cron job in Hybris, we need to :
- Create CronJob that hold all the inputs to be passed to the Job (it’s optional).
- Create Our JobPerformable, then implement the logic to be done inside it.
- Register the JobPerformable as a Spring bean with id.
- Create a new instance of ServicelayerJob, then inject the bean id in it.
- Create an instance CronJob model with this signle run configuration (inputs).
- Then Create a Trigger if it’s needed to schedule the CronJob 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.
Hello Mouad,
Nice article, I congratulate you.
In additionnal to this, Hybris provide to developers the creation of the CronJob at runtime, and without rebuilding the system by using scripting.
To do that you should you need to follow steps below:
1- Add your script to the item type —Script–
INSERT_UPDATE Script; code[unique=true];content
;businessLogicGroovyCode;”println ‘hello Men ‘”
2-Add a scriptingJob (child of ServicelayerJob, which contains the scriptURI
INSERT_UPDATE ScriptingJob; code[unique=true];scriptURI
;myDynamicJob;model://businessLogicGroovyCode
3 Create the CronJob
INSERT_UPDATE CronJob; code[unique=true];job(code);sessionLanguage(isocode);sessionUser(uid);sessionCurrency(isocode)
;myDynamicCronJob;myDynamicJob;en;admin;EUR
Yassine Zeroual
Thank you @Yassine for your contribution !
Hi, we have created a cronjob for c4c lead/order replication. But, while we are staring the cronjob manually, it is went to Aborted state.
Check the log of your cronjob to know exactly whats going on, it is under the log tab of the cronjob in hmc or backoffice.
Hello Mouad, Great description! Thank you so much for that. I am struggling with one very specific situation. Let’s say the cron job in Hybris starts up but there is a database failure so it will error out. I am catching the exception in my code but I am unable to change the status from Running to Finished(that is my intent) since the db is down. When the db comes back, the cron jobs that are in status Running won’t start up again. It does make sense but I do want them to kick off again. Any ideas on how… Read more »
Hello Dagmara,
In my opinion, the issue here is related to the infra more than it is a code problem, normally your database should never be down, otherwise, you are going to have a lot of inconsistency in your project.
So, I invite you to configure your database failover, refer to this link for more details.
I assume your articles are not for beginners, we need more information that where to create HelloWorldCronJob,HelloWorldJob, bean ?
Very good article, but I wold like to see the directory structure where the things must go, I think this was more clear how can we implement the cronjob, and as we know hybris structure must follow some rules in order to be maintainable.
Hello Mouad, Great description! Thank you so much for that.
But Please update this Impex there crone job must have language and currency value.
INSERT_UPDATE HelloWorldCronJob; code[unique=true]; job(code); firstName; sessionLanguage(isocode); sessionCurrency(isocode)
;helloWorldCronJob ;helloWorldJob ;Mouad ;en ;EUR
done, thanks for your contribution
Hi @Mouad,
Thanks for such great tutorials!
So I’ve 1 CronJob that runs every day but can I change 1 attribute of the same ron job based on dates.
for example:
M,W,F – FLAG is TRUE
TTS – FLAG is FALSE
‘Everything about Cronjobs in Hybris’….Hard to say that this is a bare minimum