Thursday, May 16, 2019

CronJob and Triggers

Cron job and Triggers


CronJob and Triggers :
Cronjob is a job to be performed based on Triggers (Triggers:in simple Triggers uses Cron Expressions to be create  a time bound to execute a cronjob.)
Ex : In real world scenario a software update on mobile when server load is low mostly in
midnights

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

2. Trigger type is used for scheduling when to run a Job. Summarizing, a Job defines what should be done, a Trigger says when.

Create an item type extending from CronJob. In trainingcore-items.xml

<itemtype generate="true"
code="UpdatingProductPrices"
jaloclass="de.hybris.training.core.jalo.UpdatingProductPrices"
extends="CronJob"
autocreate="true">
</itemtype>

3. Job: Where the actual cronjob logic will be written.

4. For this Create a class extending from AbstractJobPerformable<CronJobModel>
And override the perform() method.

Here perform method will contain actual job logic.
public class UpdatingProductPricesCronJob
extends AbstractJobPerformable<CronJobModel>
{
private static final Logger LOG = Logger.getLogger(MyJobPerformable.class.getName());

@Override
public PerformResult perform(final CronJobModel cronJobModel)
{
 LOG.info("**********************************");
 LOG.info("Custom Business Logic");
 LOG.info("**********************************");
 return new PerformResult(CronJobResult.SUCCESS, CronJobStatus.FINISHED);
}
}

5. Define the above Job class as a bean in trainingcore-spring.xml.
<bean id="cronJobForUpdatingProductPrices"
class="de.hybris.training.core.job.UpdatingProductPricesCronJob "
parent="abstractJobPerformable">
</bean>

6. Run the command ant clean all

7. Go to Hac-->platform-->update  trainingcore module

8. Go to Hac-->console-->Impeximport perform impex file.
You probably want this cronJob to perform a custom action,

for this you need to link up the cronJob with an actual action/task.
INSERT_UPDATE
CronJob;code[unique=true];job(code);singleExecutable;sessionLanguage(isocode)
;UpdatingProductPrices;cronJobForUpdatingProductPrices;false;en


9. Go to backoffice-->System-->Background Process--> Cronjobs and select cronjob.
cronJobForUpdatingProductPrices

10. Trigger: Holds cron expression when to fire cronjob.
Add the trigger conditions through TimeSchedule tab.
Cron Expression : 0 0/5,10 0  * *

Above cron expression dipicts          0 set for Seconds “0“ means at 0 seconds
      0/5,10 set for  Minutes “0 0/5,10” means every 10 min, 5 min
 0 set for Hours “0 0/10 0 ” means every hour
 * set for day “0 0/10 0 *” means every day
 * set for month       “0 0/10 0 **” means every month

You can also use impex script to create trigger.
INSERT_UPDATE
Trigger;cronJob(code)[unique=true];cronExpression
;myCronJob;0 0/10 0 * *



Click StartCronJob

12. Output:
May 08, 2019 10:32:14 PM de.hybris.training.core.job.MyJobPerformable perform
INFO: **********************************
INFO: Custom Business Logic

INFO: **********************************