CronJobs

->Cronjob Describes scheduling the task at particular Timings,

->CronJobs provide a means for performing business logic at particular times and intervals. For example, an administrator might want to perform an inventory every Sunday at midnight, or notify the web administrator every hour between 9 and 5 from Monday to Saturday of the peak loads on the servers

Steps to create Cronjob

1)write  SampleCronjob extends with AbstractJobPerformable<CronJobModel>

2)override perform() method and write our logic on that

 SampleCronjob extends with AbstractJobPerformable<CronJobModel>
{
@Override
public PerformResult perform(final CronJobModel cronJob)

{

///logic

}

}

3)register SampleCronjob in  *-spring.xml

<bean id="sampleCronjob" class="de.hybris.platform.extension.jobs.SampleCronjob" autowire="byName"/>

 

4)build and run the server ,open hac->select ur extension->update

and Check it in hac->console->flexible Search->flexible query tab ->copy below code

select {code} from {servicelayerjob} where {code} = 'sampleCronjob'

5)run respected impex

INSERT_UPDATE CronJob; code[unique=true];job(code);singleExecutable;sessionLanguage(isocode)
;sampleCronCronjob;sampleCronjob;false;de
6)triggering cronjob though hmc
go to hmc->   System -> cronjob->  type “sampleCronCronjob” in Text Box code->click on search
 7) open sampleCronCronjob job and
    i) goto  Time Schedule ->set time in(Trigger text box)-> save,
Click “Start Cronjob now ” then our job will perform and display popup box with respected “CronJob performed” message

Example CronJob:

Requirement: Remove the B2BDocuments on some conditions.

cronJob

Here in the screen above we have to delete the documents which are of type Statement & status is Closed on every day.

Solution:

Create the item type:

<typegroup name=“DOCUMENT_REMOVAL_CRONJOB”>

<itemtype code=“DocumentRemovalCronJob” autocreate=“true”

generate=“true” extends=“CronJob” jaloclass=“com.xxx.b2b.core.jalo.DocumentRemovalCronJob”>

</itemtype>

</typegroup>

Create the CronJob and trigger in the impex file:

INSERT_UPDATE DocumentRemovalCronJob;code[unique=true];job(code)[default=documentRemovalJob1];sessionLanguage(isocode)[default=en];$siteUid-DocumentRemovalJob;

INSERT_UPDATE Trigger;cronJob(code)[unique=true];second;minute;hour;day;month;year;relative;active;maxAcceptableDelay;$siteUid-DocumentRemovalJob;0;5;4;-1;-1;-1;false;true;-1

Write the cronJob class by extending the AbstractJobPerformable and override perform():

 

package com.xxx.b2b.core.cronjob;

import de.hybris.platform.cronjob.enums.CronJobResult;

import de.hybris.platform.cronjob.enums.CronJobStatus;

import de.hybris.platform.servicelayer.cronjob.AbstractJobPerformable;

import de.hybris.platform.servicelayer.cronjob.PerformResult;

import de.hybris.platform.servicelayer.model.ModelService;

 

import java.util.ArrayList;

import java.util.List;

 

import javax.annotation.Resource;

 

import org.apache.log4j.Logger;

 

import com.xxx.b2b.core.model.DocumentRemovalCronJobModel;

import com.xxx.b2b.core.suggestion.dao.DocumentRemovalDao;

import com.xxx.b2b.xxxaccountsummaryb2baddon.model.B2BDocumentModel;

public class DocumentRemovalJob extends AbstractJobPerformable<DocumentRemovalCronJobModel>

{

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

@Resource

private ModelService modelService;

@Resource

private DocumentRemovalDao documentRemovalDao;

@Override

public PerformResult perform(final DocumentRemovalCronJobModel job)

{

try

{

List<B2BDocumentModel> documents = new ArrayList<B2BDocumentModel>();

documents = documentRemovalDao.getDocuments();

for (final B2BDocumentModel b2bDocumentModel : documents)

{

final String b2bDocumenttype = b2bDocumentModel.getDocumentType().getCode();

final String b2bDocumentStatus = b2bDocumentModel.getStatus().getCode();

if (b2bDocumenttype.equals(“Statement”) && b2bDocumentStatus.equals(“closed”))

{

modelService.remove(b2bDocumentModel);

}

}

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

}

catch (final Exception e)

{

LOG.error(“Exception occurred during document removal”, e);

return new PerformResult(CronJobResult.ERROR, CronJobStatus.ABORTED);

}

}

}

Write the Dao class to get the all documents list from DB:

 

package com.xxx.b2b.core.suggestion.dao.impl;

import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;

import de.hybris.platform.servicelayer.search.FlexibleSearchService;

import de.hybris.platform.servicelayer.search.SearchResult;

import java.util.List;

import javax.annotation.Resource;

import com.xxx.b2b.core.suggestion.dao.DocumentRemovalDao;

import com.xxx.b2b.xxxaccountsummaryb2baddon.enums.DocumentStatus;

import com.xxx.b2b.xxxaccountsummaryb2baddon.model.B2BDocumentModel;

import com.xxx.b2b.xxxaccountsummaryb2baddon.model.B2BDocumentTypeModel;

public class DefaultDocumentRemovalDao implements DocumentRemovalDao

{

private static final String FIND_ALL_DOCUMENTS = “SELECT {” + B2BDocumentModel.PK + “} FROM {”

+ B2BDocumentModel._TYPECODE + “}”;

@Resource

private FlexibleSearchService flexibleSearchService;

@Override

public List<B2BDocumentModel> getDocuments()

{

final SearchResult<B2BDocumentModel> result = flexibleSearchService.search(FIND_ALL_DOCUMENTS);

System.out.println(result.getResult());

System.out.println(result.getCount());

return result.getResult();

}

}

Configure the CronJob and Dao in the xml:

<bean id=“documentRemovalJob”                  class=“com.xxx.b2b.core.cronjob.DocumentRemovalJob” parent=“abstractJobPerformable”>    </bean>

<bean id=“documentRemovalDao”               class=“com.xxx.b2b.core.suggestion.dao.impl.DefaultDocumentRemovalDao”>

</bean>

Define the service layer job in impex file:The spring Id must be match with the bean id of the cronjob in impex file.

INSERT_UPDATE ServicelayerJob;code[unique=true];springId[unique=true]

;documentRemovalJob1;documentRemovalJob