Cron Jobs Overview
Cron job functionality is used whenever we want to execute some tasks at some point of time.
It’s like an Alarm we set before we sleep 🙂 .
We set Alarm at 5AM, 6AM, 7AM and so on, so at that point of time, Alarm gets executed.
We can set the alarm for daily, alternate days, or any specific day as well.
Similarly Cron jobs can be set up to run only on one specific day or every day or every month, or every hour etc.
Now the main point here is Cron job basically do some work at specified time.
In Hybris, Cron job executes some business logic at specified time.
The business logic which gets executed by the cron job is called as Job.
What time it executes is defined by the Trigger.
What parameters and configuration required for job to execute is defined by the CronJob.
Let’s understand each of these terms in detail
Cron Job :
Its an Item Type we define in the Items.xml with all the attributes required for the Job to be executed.
So we can say that CronJob provides the configuration details for the Job.
Hybris Out of the Box(OOTB) provides CronJob item Type which already has many attributes defined inside it like
Code,active,status.logText,startTime,endTime etc.
Check CronJob itemtype under below file for more details
/platform/ext/processing/resources/processing-items.xml
All these attributes are considered as a configuration for the Job.
In case our Job needs some additional configuration parameters other than what is provided with CronJob Item Type, then we should define a new CronJob Item Type in the *items.xml by extending the CronJob Item Type.
Example:
- <itemtype code="SampleCronJob" generate="true" autocreate="true"
- jaloclass="de.hybris.cronjob.jalo.SampleCronJob"
- extends="CronJob">
- <attributes>
- <attribute type="java.lang.String" qualifier="sampleValue">
- <persistence type="property"/>
- <modifiers optional="false"/>
- </attribute>
- </attributes>
- </itemtype>
<itemtype code="SampleCronJob" generate="true" autocreate="true" jaloclass="de.hybris.cronjob.jalo.SampleCronJob" extends="CronJob"> <attributes> <attribute type="java.lang.String" qualifier="sampleValue"> <persistence type="property"/> <modifiers optional="false"/> </attribute> </attributes> </itemtype>
Job:
Job defines the logic to be executed.
We create a new class and extend AbstractJobPerformable class or implement JobPerformable interface.
Inside this newly created class, we provide the definition for perform(CronJobModel cronJobModel) method.
What we define inside this method is called as Business logic of the cron job.
And this class which contains the business logic is called as Job.
Note: This class should be defined as a Spring bean in order to consider this as a Job.
Example:
- public class SampleJob extends AbstractJobPerformable<SampleCronJobModel>
- {
- @Override
- public PerformResult perform(final CartRemovalCronJobModel job)
- {
- //Logic goes here
- }
- }
public class SampleJob extends AbstractJobPerformable<SampleCronJobModel> { @Override public PerformResult perform(final CartRemovalCronJobModel job) { //Logic goes here } }
Job will have 2 main things,
1) Status: it is of type CronJobStatus enum
It gives the status of a Running cron Job which indicates whether it’s in Progress,or completed the execution.
possible values for the same can be checked in the CronJobStatus.java enum.
2) Result: it is of type CronJobResult enum
It gives the information about the last execution of the cron job
Like whether cron job has ended with Success or Failure
possible values for the same can be checked in the CronJobResult.java enum.
We can say that Cron Job is completed successfully with the below combination of CronJobStatus and CronJobResult
CronJobStatus.FINISHED and CronJobResult.SUCCESS
So PerformResult is returned by perform() method after the Job completes Business logic execution, which then can be used to determine the overall result and status of the Job.
Trigger:
It defines the scheduling of a Job which helps to decide when the Job has to be executed.
It defines scheduling using the attributes like seconds,minutes,hours etc or it can define it using Cron Expression.
see below link for details on how to define the Cron Expression
Cron Expression Guide
Trigger is actually linked to the Job indirectly which means Trigger will always be defined for CronJob and CronJob holds the actual Job to be executed.
So whenever we define Trigger, we define it for CronJob not for Job.
CronJob in turn identify the corresponding Job and makes sure it gets executed.
We will get more clarity on this with code in the next Article.
In Simple words,
Job defines what should be done, a Trigger says when it has to be done, and a CronJob defines the setup/configuration required for a Job.
Im stuck at here :
https://stackoverflow.com/questions/71464381/java-runtime-execution-not-working-in-cron-job-reboot-showing-error-unable-to
can you please help me to come out from this issue?
hi
Hi Karibasappa G C (KB)
I am working in production issue, In production they push price for few of products,and then try to sync staged to online it is not syncing.
From log i am getting only below mention information.I am not Getting any log from dumpmedia attribute from adminstaration tab of same jobID
0;31mERROR [0000HDO1:de.hybris.platform.catalog.jalo.synchronization.CatalogVersionSyncJob] (0000HDO1) [Job] Cronjob 0000HDO1(8822658447813) cannot be performed yet
few things I haved checked for sync failed like
1.duplicate identifiers
2.same job instance is running stage or not
3.restriction etc
everything is fine.but still why cronjob is not started I am not getting
Can you please give suggest how i will resolve this issue
Hello Lipta,
Go to Cron Job and search for status running. When one synchronization job is running you cannot trigger the same job another time. Until the other job status is changed to either Finished or Aborted you cannot trigger another job.
Hope this helps.
Thank you.
Vinay Kumar Samudrala
Haii sir will u give training on hybris i came from poor background it is necessary to get job please consider sir
Hi Manikanta,
I got many requests on the same and based on all requests, I am considering to start it in 1-2 months.
I will let you know once i start it.
All the best!!