Thursday 14 July 2016

Hybris Cronjob :

The cronjob functionality is used for executing tasks, called cron jobs, regularly at a certain point of time. You may set up cron jobs to run just once, once an hour, every 35 minutes and 17 seconds, every day, every week and so on. Typically cron jobs can be used for creating data for backups, updating catalog contents, or recalculating prices. The key idea of applying cron jobs is to start a long or periodic process in a background, having the possibility to protocol each run and to easily check its result. The concept of cron jobs in the hybris Commerce Suite is explained in detail below.



General Concept
The concept consists of three types that interact with one another: CronJob, Job, and Trigger, as well as the JobPerformable class which is tightly related to the Job type.

==> Job type describes the logic to be executed, defined by an associated JobPerformable

==>CronJob type holds the configuration for a single run of a Job, as well as the protocol information, like logs

==>Trigger type is used for scheduling when to run a Job.
Summarizing, a Job defines what should be done, a Trigger says when, and a CronJob specifies the setup used by Job.
The division into CronJob, Job/JobPerformable, and Trigger types allows to reuse the code. For example, you may create a database backup as a Job several times using Trigger at different locations as CronJob without coding the actual backup logic twice.


1) ant extgen

     --yempty
     --cronjobtutorial
     --de.hybris.cronjobtutorial

and a extension will be built inside custom folder of your bin





2) add <extension name="cronjobtutorial" />  into your localextension.xml and do "ant clena all" and  

import cronjobtutorial   in your eclipse


3) Declare a custom CronJob type called HelloWorldCronJob. Do this in the "cronjobtutorial-items.xml" file located in resources
   folder in your new extension. Add there a subtype of CronJob type like in the following example:



and do an "ant" in the ${HYBRIS_BIN_DIR} \platform directory.Refresh platform, HelloWorldCronJobModel is generated in the bootstrap/gensrc directory.

Why? ---- what ever you define in hybris needs to get stored somewhere in the data model or in virtual relational db tables. So we need to describe a itemtype for that, where we can store our job. Even in real project scenarios, when we need to add various property attributes or modifiers to your cronjob, the only one way we can do it is to define it under an itemtype we want it to. So to distinguish the job from others and store it in backend giving a unique id or key we need to mention it in items.xml





4) To define the logic for a job, in the de.hybris.cronjobtutorial package create a new class extending AbstractJobPerformable and override the perform
method. In this tutorial, the job should create log information. You may also implement the JobPerformable interface directly. The AbstractJobPerformable class is
used to define the job as performable and non-abortable by default and to provide access to some services. It implements the JobPerformable interface.

*** abstractJobPeformable class and the abstract method Perform are extended and overridden respectively fro already present cronjob extension which comes with your hybris suite and which you extend in your cronjob and customize.



5) do an "ant" in the ${HYBRIS_BIN_DIR} \platform directory and start the server. The new MyJobPerformable has to be defined as a Spring bean in the cronjobtutorial-spring.xml file

Why???--- basically to integrate with spring framework and use it further in business logic we need to tell spring framework about our job. This can only be define in beans, so that the bean factory container in spring and handle it further along with the unique bean id given to it.
located in the resources folder of the cronjobtutorial extension:


6) After changing the Spring configuration, restart the hybris Platform and perform a system update.A system update is needed, because during the phase of essential data creation, for each Spring definition of a class implementing the JobPerformable interface, a
ServicelayerJob instance gets created and the code attribute of the job is set to the name of the Spring bean. Hence, using an instance of ServicelayerJobModel
with the code attribute set to the myJobPerformable job will call the new implemented myJobPerformable.





7) You can check in the FlexibleSearch console: http://localhost:9001/console/flexsearch that the new item was created by executing the following query:
select {code} from {servicelayerjob} where {code} = 'myJobPerformable'

Also check for

select {code} from {HelloWorldCronjob} where {code} = 'myJobPerformable'
select {job} from {HelloWorldCronjob} where {code} = 'myJobPerformable'


8)  do an initialize

9)  import impex

INSERT_UPDATE HelloWorldCronJob;code[unique=true];job(code);singleExecutable;sessionLanguage(isocode)
;myJobPerformable;myJobPerformable;false;en

INSERT_UPDATE Trigger;job(code)[unique=true];cronExpression
;myJobPerformable; 0/10 * * * * ?


or write it in essentialdataJobs.impex and then check for

select {code} from {HelloWorldCronjob} where {code} = 'myJobPerformable'
select {job} from {HelloWorldCronjob} where {code} = 'myJobPerformable'

if still notyou can use
INSERT_UPDATE ServicelayerJob;code[unique=true]
;myJobPerformable;myJobPerformable

INSERT_UPDATE HelloWorldCronJob;code[unique=true];job(code);singleExecutable;sessionLanguage(isocode)
;myJobPerformable;myJobPerformable;false;en

INSERT_UPDATE Trigger;job(code)[unique=true];cronExpression
;myJobPerformable; 0/10 * * * * ?








10) go to hmc->system->cronjob->search



  select myJobPerformable and click "startcronjob"








3 comments:

  1. I understood easily this topic nice tutorail

    ReplyDelete
  2. Its not efficient to create a new extension just for cron job

    ReplyDelete