Home » CronJob

CronJob

For creating a functionality of cron Job, 3 things are important to do :

crojob

Job:

It’s a Java Class, where business Logic is defined.

CronJob:

It’s performing business Logic in Job at particular times and intervals.

Trigger:

Times and Intervals are defined by triggers, If you want to start your cron job automatically, you need to define a trigger.

Explained by Image:

Create a Cron Job:

  • Create CronJob Itemtype:Create an itemtype
<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="items.xsd">
<itemtypes>
<itemtype generate="true" code="DemoCronJob" jaloclass="de.hybris.core.jalo.DemoCronJob "extends="CronJob" autocreate="true">
</itemtype>
</itemtypes>
</items>
  • Run ant clean all

Create a Job:

  • Create a new class extending AbstractJobPerformable and override the perform method.
  • You may also implement the JobPerformable interface directly.
public class MyJobPerformable extends AbstractJobPerformable
{
private static final Logger LOG = Logger.getLogger(MyJobPerformable.class.getName());

@Override
public PerformResult perform(final DemoCronJobModel cronJobModel)
{
LOG.info("**********************************");
LOG.info("Greeting from MyJobPerformable!!!");
LOG.info("**********************************");

return new PerformResult(CronJobResult.SUCCESS, CronJobStatus.FINISHED);
 }
}

Note: 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.

  • Run ant all in the ${HYBRIS_BIN_DIR} \platform
  • The new MyJobPerformablehas to be defined as a Spring bean in the <extesionName>-spring.xml file located in the resources folder of the cronjobtutorial extension:
<bean id="myJobPerformable" class="de.hybris.cronjobtutorial.MyJobPerformable" parent="abstractJobPerformable"/>
  • Restart the Platform and perform a system update.


Creating Trigger

  • Create a impex:

INSERT_UPDATE Trigger; cronJob(code)[unique=true]; cronExpression
; myDemoCronJob; 0 30 10-11 ? * WED,FRI

Perform the following steps to run the new cron job:

  1. Get the job instance of ServicelayerJob having springid attribute set to myJobPerformable.
  2. Create and configure an instance of myDemoCronJob and Assign the job to the cron job and set attributes for the cron job.

Above steps are presents in below example of test class that you can implement in your cronjobtutorial extension:


Create Impex For CronJob:

#1.Create Service Layer Entry:
INSERT_UPDATE ServicelayerJob;code[unique=true];springId[unique=true]
; myJobPerformable; myJobPerformable

#2.Create CronJob Instance Entry:
INSERT_UPDATE DemoCronJob;code[unique=true];job(code);singleExecutable;sessionLanguage(isocode)
;myDemoCronJob; myJobPerformable;true;en

#3.Create trigger Entry:
INSERT_UPDATE Trigger; cronJob(code)[unique=true]; cronExpression
; myDemoCronJob; 0 30 10-11 ? * WED,FRI

Note: # is used to comment the entire line.


How to run a Cronjob Manually:

This is the process to start cronJob manually.
Steps:

  • Go to HMC
  • Expand the system Tab in left Nav and open CronJob tree structure.
  • Search the cronjob by its ID “myJobPerformable” and click on “start CronJob”.