Cronjob

Cronjobs is most powerful role in any software development project. Cronjobs is used to create automated tasks responsible for execute code in specific time , regularly at a certain point of time. Basically, cronjobs are used for data backups, update catalog, import or export data file or recalculate prices etc. The key concept of cronjobs, it is to start a long or periodic process in the background and execute all regulars activity in off business hours.

For Understand concept of Cronjob, We need to know about of three types that interact with each another: Cronjob, Job, Trigger and JobPerformable .

  • JobPerformable: It is interface that contain perform method and tightly related to the Job type. Hybris recommend Generics type when implementing JobPerformable interface and perform method.

  • AbstractJobPerformable: It provides a basic functionality for a common JobPerformable implementation like request abort handling.

  • CronJob: It contains the configuration for a single run of a Job and other information, like session settings, for a specific execution or logging configuration.

  • Job: It describes one kind of execution logic in the system and each CronJob should reference one instance. Although the Job instance does not contain any logic.

  • ServicelayerJob: it is extends to Job class and provides the springId attribute that references a Spring bean definition of the execution logic. The Spring bean has to implement the JobPerformable interface.

  • Trigger : It is basically used for Job schedule when Job will run. It could be either on a regular basis or only once.

Step to Create Cronjob

1) if Job needs some additional configuration parameters apart from CronJob Item Type provided, then we need to define a Custom CronJob Item Type in the *items.xml by extending the CronJob Item Type.

<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="items.xsd">

<itemtypes>

<itemtype generate="true"

code="JavaOopsCustomCronJob"

extends="CronJob"

autocreate="true">

<!--If required then define otherwise optional->

<attributes>

<attribute qualifier="sequenceId" type="java.lang.Integer">

<modifiers optional="false"/>

<persistence type="property" />

<defaultvalue>Integer.valueOf(1)</defaultvalue>

</attribute>

</attributes>

</itemtype>

</itemtypes>

</items>

2) To define the logic for a job, We will create Java class which will extends AbstractJobPerformable and implement perform method.

public class MyJavaOopsCustomJob extends AbstractJobPerformable<JavaOopsCustomCronJobModel>

{

private static final Logger LOG = Logger.getLogger(MyJavaOopsCustomJob.class.getName());

@Override

public PerformResult perform(final MyJavaOopsCustomJob cronJobModel)

{

LOG.info("**********************************");

LOG.info("Hello JavaOops Custom CronJob!!!");

LOG.info("**********************************");

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

}

}

3) Now, we will define to configuration of spring bean for custom java class in *-spring.xml file.

<bean id="myJavaOopsCustomJob " class="de.hybris.cronjob.javaops.MyJavaOopsCustomJob" parent="abstractJobPerformable"/>

4) To create the Cronjob and the Trigger via impex. Cronjob trigger point will be define by cron expression

INSERT_UPDATE CronJob; code[unique=true];job(code);singleExecutable; sessionLanguage(isocode)

;myJavaOopsCustomCronJob;myJavaOopsCustomJob;false;en


INSERT_UPDATE Trigger;cronjob(code)[unique=true];cronExpression

; myJavaOopsCustomCronJob; 0 0 0 * * ?

Cron Expression

A cron expression is a string comprised of 6 or 7 fields separated by white space. Fields can contain any of the allowed values, along with various combinations of allowed special characters for that field.

Field Name Mandatory Allowed Values Allowed Special Characters

Seconds YES 0-59 , - * /

Minutes YES 0-59 , - * /

Hours YES 0-23 , - * /

Day of month YES 1-31 , - * ? / L W

Month YES 1-12 or JAN-DEC , - * /

Day of week YES 1-7 or SUN-SAT , - * ? / L #

Year NO empty or 1970-2099 , - * /