Table of Contents
Cronjob
A cronjob in hybris is a task which can be triggered by user manually or it can be triggered automatically using trigger. Apart of cronjob hybris provides Scripting jobs and task services.
The cronjob functionality is used for executing tasks, called cron jobs, regularly at a certain point of time. 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 the background, with the possibility to log each run and to easily check its result. The concept of cron jobs in SAP Commerce is explained in detail here.
Below is the diagram which depicts relation between Cronjob, Job and Trigger.
Overview
Cronjob: holds the Information for a single run of a Job, as well as the other information, like logs.
Job: It contains the logic to be executed, defined by an associated JobPerformable.
Trigger: is used for when to run a Job.
Example:
Most of the time cronjob model provides sufficient attributes to setup a cronjob. If you want to add your custom attributes, then you can add in your items.xml.
<itemtype code="ExampleCustomCronJob" extends="CronJob" autocreate="true" generate="true">
<attributes>
<!-- Generic Attribute to add in your custom cronjob -->
<attribute type="java.lang.String" qualifier="sampleValue">
<persistence type="property"/>
<modifiers optional="false"/>
</attribute>
<!-- Media Attribute to add in your custom cronjob from where you can upload the file to process further -->
<attribute type="media" qualifier="uploadCSV">
<persistence type="property"/>
</attribute>
</attributes>
</itemtype>
public class CustomSampleCronJob extends AbstractJobPerformable<CustomSampleCronJobModel>
{
/** The Constant LOG. */
private static final Logger LOG = Logger.getLogger(CustomSampleCronJob.class);
/** The model service. */
private ModelService modelService;
/** The configuration service. */
private ConfigurationService configurationService;
private CronJobService cronJobService;
@Override
public PerformResult perform(final CartRemovalCronJobModel cron)
{
LOG.info(String.format("%s: Sample Custom Cronjob STARTed", cron.getCode()));
try
{
}
catch(Exception ex)
{
}
}
}
Trigger
There is often a need to trigger an execution of a specific logic again, either on a regular basis or only once. To enable it, Trigger type references a Job that needs to be executed and a cron expression to define the schedule. If the current time fits to the defined cron expression, the referenced CronJob or Job gets executed automatically.
INSERT_UPDATE Trigger; cronjob(code)[unique=true]; cronExpression[unique=true]
;CustomSampleCronJob; 0 0 9 1/1 * ? *
Service Layer Entry
INSERT_UPDATE ServicelayerJob;code[unique=true];springId[unique=true]
;cartRemovalJob;cartRemovalJob
Spring XML Entry
<bean id="ExampleCustomCronJob" class="com.example.core.cronjob.ExampleCustomCronJob" parent="abstractJobPerformable">
<property name="exampleCustomDao" ref="exampleCustomDao"/> <!-- if you want to insert any dependecy -->
</bean>
Composite Cronjob
A composite cronJob is a composition of multiple cronJobs used when we need to run a bundle of cronJobs successively. It is often referred as an ordered all or nothing execution.
Example: If you want to load daily product data and after that you want to perform some operations on products you loaded. in last you want to sync those to Online catalog.
Composite Jobs Entries For Common Data Setup
INSERT_UPDATE CompositeEntry;code[unique=true];compositeCronJob(code);compositeCronJobPOS[unique=true];executableCronJob(code)[unique=true];triggerableJob(code)
;productMasterFeedCronJob;productCompositeJob;0;productMasterFeedCronJob;productFeedJob;
;productListPriceCronJob;productCompositeJob;1;productListPriceCronJob;productFeedJob
;productSyncCronJob;productCompositeJob;2;productStaged->productOnline;productStaged->productOnline
;productFullIndexCronJob;productCompositeJob;3;productFullIndexCronJob;solrIndexerJob
Scripting Job
INSERT_UPDATE Script;code[unique=true];scriptType(code)[default=GROOVY];content
;UserDataProcessingGroovy;GROOVY;"Your Grovvy Content"
INSERT_UPDATE ScriptingJob;code[unique=true];scriptURI
;UserDataProcessingGroovyJob;model://UserDataProcessingGroovy
INSERT_UPDATE CRonjob;code[unique=true];job(code);sessionLanguage(isocode)[mode=append]
;UserDataProcessingGroovyCronJob;UserDataProcessingGroovyJob;en,de