Everything about CronJobs in Hybris (Part 2)

Hybris Logo

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.

Start cronjob manually from hmc in hybris

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.

Cronjob trigger activation time from hmc hybris

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.

 

3.3 6 votes
Article Rating
Subscribe
Notify of
guest

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

12 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Yassine
Yassine
6 years ago

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

Deepthi
Deepthi
Reply to  Mouad EL Fakir
5 years ago

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.

Dagmara
Dagmara
5 years ago

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 »

Sivaprakas
Sivaprakas
5 years ago

I assume your articles are not for beginners, we need more information that where to create HelloWorldCronJob,HelloWorldJob, bean ?

Gabriel Gonzalez
Gabriel Gonzalez
4 years ago

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.

Nitish patidar
Nitish patidar
4 years ago

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

Sagar Bhambhani
Sagar Bhambhani
3 years ago

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

pqvazmi315@vigoneo.com
1 year ago

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

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