How to configure catalog sync cronjob in Hybris?

 How to create custom catalog synchronization job in Hybris?

https://www.helphybris.com/2018/04/create-custom-catalog-sync-job-hybris.html

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!
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 ? * *  


Author Image

Ankitkumar Patel

Sr. SAP Hybris consultant, having 6+ years experience in Hybris, Java, J2EE. Extensive hands-on experience in SAP Hybris development, third-party integrations with Hybris, project architecture and design... Read more

Comments

Popular posts from this blog

Hybris flexible search query examples

How to remove or update all data records in Hybris?

Hybris backoffice customization