top of page

Cronjob Overview

​

In  SAP Hybris Commerce, cronjobs play a crucial role in automating various tasks and processes within the e-commerce platform. Cronjobs are used to schedule and execute recurring background jobs, which can range from product synchronization and order processing to data import and export. Here's an overview of cronjobs in SAP Commerce:

​

What is a Cronjob: A cronjob is a scheduled task or background job that runs at specified intervals or times. The name "cronjob" is derived from the Unix/Linux cron utility, which is used for scheduling tasks. In SAP Commerce, cronjobs are managed and scheduled through the platform's built-in cronjob mechanism.

 

Types of Cronjobs:  SAP Commerce provides a variety of pre-defined cronjobs for common e-commerce tasks, and developers can create custom cronjobs to meet specific business requirements. Some common types of cronjobs include:

​

1)  Product Sync Cronjobs: These are used to synchronize product data from external sources or systems.

2) Order Processing Cronjobs: These handle tasks related to order processing, such as order validation and order status updates.

3) Data Import/Export Cronjobs: Used for importing and exporting data to and from the e-commerce system.

4) Email Sending Cronjobs: Scheduled tasks for sending email notifications, newsletters, and marketing campaigns.

5) Cleanup and Maintenance Cronjobs: Responsible for routine maintenance tasks, such as clearing logs or removing expired data.

6) ​Custom Cronjobs: Developers can create custom cronjobs to automate specific business processes.

​

​

​

Cronjob Components

​

 Cronjob is an essential component used to automate various recurring tasks and processes. Understanding the important components of a cronjob is crucial for creating and managing these scheduled jobs effectively. Here are the key components of a cronjob in Hybris:

1) Cronjob Model:

Purpose: The cronjob model defines the metadata for a cronjob, such as its name, code, and configuration.
Components:

Code: A unique identifier for the cronjob.
Name: A descriptive name for the cronjob.
Job Configuration: Information about the task to be executed, including the task code, scheduling details, and other parameters.
Status: Indicates whether the cronjob is enabled or disabled.

2) Job Configuration:

Purpose: This part of the cronjob configuration specifies the actual task to be performed when the cronjob runs.
Components:
Task Code: The code that identifies the task to be executed. This code corresponds to a specific Java class.
Job Details: Additional configuration related to the task, such as input parameters or settings required for the task's execution.
Scheduling: Defines when and how often the cronjob should run. This includes the cron expression, which specifies the execution schedule, and options like single-execution or recurring execution.

 

3) Task Implementation:

Purpose: The task implementation is the Java class responsible for executing the actual work when the cronjob runs.
Components:
Java Class: The Java class that implements the Performable<CronJobModel> interface or extends AbstractJobPerformable<CronJobModel>. This class contains the logic for the task.
Service Dependencies: If the task relies on services or components, they should be injected into the task implementation.

​

​

​

Cronjob Creation Steps

​

1) Define Your Cronjob Task:  Decide on the specific task that your cronjob will perform. This could be a data synchronization, email sending, cleanup operation, or any other recurring task.

2) Create a Custom CronJob Model: Cronjobs are defined as model in Hybris. We have to add some additional parameter in the out of the box cronjob model then we create the custom cronjob model as below: 

​

  1. <itemtypes>

  2. <itemtype generate="true" code="CustomCronJob"extends="CronJob" autocreate="true">

  3. <attributes>

  4. <attribute qualifier="noofdaysold" type="java.lang.Integer">

  5. <modifiers optional="false"/> 

  6. <persistence type="property" />

  7. </attribute>

  8. </attributes>

  9. </itemtype>

  10. </itemtypes>

​

3) Define the  Service class to Perform the Business Task: Write a Java service or task that performs the job you want your cronjob to do. This service will be invoked when the cronjob runs. Make sure this service follows Hybris service conventions.

​

public interface MyCustomCronJobService {

void performTask();

}

​

4) Create the Service bean in xml : We will create the bean class for the above service class. 

​

<bean id="myCustomJobService " class="de.hybris.cronjob.service.MyCustomCronJobService"/>

​

5) Define the Job class : We will define the job class  where we will inject the cronjobservice and perform the business logics. We will implement the abstractJobPerformable class and override the perform method. 

​

  1. public class MyCustomJob extends AbstractJobPerformable<CustomCronJob>

  2. {

  3. private static final Logger LOG = Logger.getLogger(MyCustomJob.class);

  4. @Resource (name="myCustomJobService")

  5. private  MyCustomCronJobService myCustomCronJobService;

  6. @Override

  7. public PerformResult perform(final CustomCronJob cronJobModel)

  8. {

  9. myCustomCronJobService..performTask();

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

  11. }

  12. }

​​

6) Define the Job in XML: We will define the job class bean where we will inject the cronjobservice property and perform the business logics. We will implement the abstractJobPerformable class and override the perform method. 

 

<bean id="myCustomJob " class="de.hybris.cronjob.job.MyCustomJob" parent="abstractJobPerformable">

<property name="myCustomJobService" ref="myCustomJobService"/>

</bean>

​

7) Define the Cronjob instance and the Trigger via impex: We will create the instance of the customCronjob model and link it to the respective job bean. We will create the customCronJob trigger to schedule the job. 

​

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

;customCronJob;myCustomJob;false;en

 

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

; customCronJob; 0 0 0 * * ?

​

​

About the Author

 

Piyush Singh is a seasoned technology enthusiast and educator with a passion for making complex concepts accessible to everyone. With over 10 years of experience in the tech industry working as consultant,  Piyush specializes in Java, Sap Hybris technology and has a knack for breaking down intricate topics into easy-to-follow tutorials.

​​​​​​​

   Connect with Author

LinkedIn

Email

​

​

bottom of page