How to configure catalog sync cronjob in Hybris?
How to create custom catalog synchronization job in Hybris?
Scenario
My client wants to have product catalog sync cronjob. So the first thing that came to my mind was, It's quite easy, by creating an instance of
CatalogSyncCronJob
and assign it to appropriate sync job(Product Sync stage to Online) and trigger.
But it won't work like that.
CatalogVersionSyncJob is designed to run only once with each instance. So if we create sync job instance by ImpEx/HMC, it will not get any newly / modified items in the second execution. because system needs a new instance for each sync execution!
CatalogVersionSyncJob is designed to run only once with each instance. So if we create sync job instance by ImpEx/HMC, it will not get any newly / modified items in the second execution. because system needs a new instance for each sync execution!
Wondering, how catalog synchronization works when we do it form Hybris HMC(Catalog Management Tool > Synchronization)?
If we execute catalog sync from Catalog Management Tool, then each time, Hybris internally creates a new instance of selected sync job. Hence, it can detect new or modified items to sync.
Solution
We can write the custom job, which basically does the same thing as HMC sync. creates new instance, assign sync job, and execute it.
package com.projectcore.job;
import de.hybris.platform.commerceservices.setup.SetupSyncJobService;
import de.hybris.platform.cronjob.model.CronJobModel;
import de.hybris.platform.servicelayer.cronjob.AbstractJobPerformable;
import de.hybris.platform.servicelayer.cronjob.PerformResult;
import de.hybris.platform.util.Config;
import javax.annotation.Resource;
public class CustomProductCatalogSyncJob extends AbstractJobPerformable<CronJobModel>
{
private static final String PRODUCT_CATALOG = Config.getString("productcatalogname", "yourProductCatalog");
@Resource
private SetupSyncJobService setupSyncJobService;
@Override
public PerformResult perform(final CronJobModel cronJob)
{
/**
* Implemented based on AbstractDataImportService > synchronizeProductCatalog
*/
setupSyncJobService.createProductCatalogSyncJob(PRODUCT_CATALOG);
return setupSyncJobService.executeCatalogSyncJob(PRODUCT_CATALOG);
}
}
*core-spring.xml
<bean id="customProductCatalogSyncJob" class="om.projectcore.job.CustomProductCatalogSyncJob">
<property name="modelService" ref="modelService"/>
<property name="flexibleSearchService" ref="flexibleSearchService"/>
<property name="sessionService" ref="sessionService"/>
</bean>
*.impex
#
create instance of CronJob and assign our custom job(customProductCatalogSyncCronJob) INSERT_UPDATE CronJob ; code[unique=true] ; job(code) ; singleExecutable ; sessionLanguage(isocode) ; customProductCatalogSyncCronJob ; customProductCatalogSyncJob ; false ; en
# Trigger to run the customProductCatalogSyncCronJob at 1:00 AM everyday
INSERT_UPDATE Trigger ; cronjob(code)[unique=true] ; cronExpression ; customProductCatalogSyncCronJob ; 0 0 1 ? * *
You forgot to import the job definition via impex
ReplyDeleteINSERT_UPDATE ServicelayerJob ; code[unique = true] ; springId[unique = true]
; customProductCatalogSyncJob ; customProductCatalogSyncJob