Wednesday 11 May 2016

Create Cron Job and Run Manually from HMC

CronJob:


Cron job is basically a piece of business logic which you may set torun just once, once an hour, every 35 minutes and 17 seconds, every day, every week and so on. 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 a background, having the possibility to protocol each run and to easily check its result.

The concept consists of three types that interact with one another:CronJob, Job, and Trigger, as well as the JobPerformable class which is tightly related to the Job type.

· 1.)Job type describes the logic to be executed, defined by an associated JobPerformable

· 2.)CronJob type holds the configuration for a single run of a Job, as well as the protocol information, like logs

· 3.)Trigger type is used for scheduling when to run a Job.
Summarizing, a Job defines what should be done, a Trigger says when, and a CronJob specifies the setup used by Job


The division into CronJob, Job/JobPerformable, and Trigger types allows to reuse the code. For example, you may create a database backup as a Job several times using Trigger at different locations as CronJob without coding the actual backup logic twice.














Steps to create Cronjob
Step 1: Create an extension using ant extgen and mentioned the below details
  • extension.name=cronjobtutorial
  • extension.package=de.hybris.cronjobtutorial

Add the entry of this extension in localextensions.xml

run ant clean all

Step 2:Declare a custom CronJob type called HelloWorldCronJob. Add the below entry in cronjobtutorial-items.xml file located in resources folder in your new extension

            xsi:noNamespaceSchemaLocation="items.xsd">
    <itemtypes>
        <itemtype generate="true"
           code="HelloWorldCronJob"
           jaloclass="de.hybris.cronjobtutorial.jalo.HelloWorldCronJob"
           extends="CronJob"
           autocreate="true">     
        </itemtype>
    </itemtypes>
</items>

Step 3: Create a class which defines your logic. The class extending AbstractJobPerformable 
and override the perform method

public class MyJobPerformable extends AbstractJobPerformable<CronJobModel>
{
private static final Logger LOG = Logger.getLogger(MyJobPerformable.class.getName());

@Override
public PerformResult perform(final CronJobModel cronJobModel)
{
LOG.info("**********************************");
LOG.info("Greeting from MyJobPerformable!!!");
LOG.info("Yehhh maja aa gaya!!!");
LOG.info("**********************************");

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

}

}
Step 4: Define MyJobPerformable  as a Spring bean in the cronjobtutorial-spring.xml file located in the resources folder of the cronjobtutorial extension 


<bean id="myJobPerformable" class="de.hybris.cronjobtutorial.MyJobPerformable"

parent="abstractJobPerformable"/>


Step 5: Run the command ant clean all
Step 6: Start Hybris server using hybrisserver.bat

Step 7: Open Hybris admin console. Go to platorm>update. Select Cronjobtutorial check box 
and click update

Step 8: Go in the hybris Admin Console to the Console tab select ImpEx Import and execute the following impex-script there by clicking on the Import Content button. Using this query we are defining the kind of key value pair. Which means a cronjob with code HelloWorldCronJob" is mapped with "myJobPerformable" having all business logic written


INSERT_UPDATE CronJob; code[unique=true];job(code);singleExecutable;sessionLanguage(isocode)
;HelloWorldCronJob;myJobPerformable;false;de


Step 9: Open HMC using (http://localhost:9001/hmc/hybris). 
Navigate to System > Cronjobs and start the cronjob. 
Refer snap shot below.




















Step 10: Once cronjob finished message is displayed on HMC. 
Check you hybris console and you can see the results.


4 comments:

  1. I executed this cron job in 6.3 version but in the console i didn't get any display message

    ReplyDelete
  2. Can anybody solve this Question and mail to rekha.amiable@gmail.com....
    Create a CMSLinkComponent(Eg:UpdatePhone) and show it in Profile page

    ReplyDelete