Use cleanup job in SAP Hybris
1. Overview
Orphaned and unused objects may make you Hybris sluggish and bloated, such as old carts, orphaned media, jobs log, etc. So you should think absolutely about a database cleanup (remove or archive).
The best way to do clean up in Hybris is by using a scheduled cron job. In this article, I will show you how to create cleanup cronJobs.
Hybris comes already with some useful cleanup cronJobs for saved values, Jobs log, old carts, etc
This is an overview of how maintenance cleanup job works in Hybris.
- Cleanup Cronjob : is an instance of the
MaintenanceCleanupJobModel
. AbstractMaintenanceJobPerformable
: contains all the logic of clean up inside theperform()
method, this a simple code snippet :
public abstract class AbstractMaintenanceJobPerformable extends AbstractJobPerformable<CronJobModel> implements TypeAwareJobPerformable {
@Override
public final PerformResult perform(final CronJobModel cronJob)
{
// generate flexible search to retrive items in question
final FlexibleSearchQuery createFetchQuery = this.getFetchQuery(cronJob);
// do retrieve items from database
final SearchResult<PK> items = flexibleSearchService.search(createFetchQuery);
// process items : remove or archive
this.process(items.getResult(), cronJob);
// return ok
return new PerformResult(SUCCESS, FINISHED);
}
// implemented by GenericMaintenanceJobPerformable
public abstract FlexibleSearchQuery getFetchQuery(final CronJobModel cronJob);
// implemented by GenericMaintenanceJobPerformable
public abstract void process(final List<ItemModel> elements, final CronJobModel cronJob);
}
GenericMaintenanceJobPerformable
: extendsAbstractMaintenaceJobPerformable
and implementsgetFetchQuery()
andprocess()
, see code snippet :
public class GenericMaintenanceJobPerformable extends AbstractMaintenanceJobPerformable {
// injected with Spring
private MaintenanceCleanupStrategy maintenanceCleanupStrategy;
// call createFetchQuery() of the maintenanceCleanupStrategy
@Override
public FlexibleSearchQuery getFetchQuery(final CronJobModel cronJob)
{
return maintenanceCleanupStrategy.createFetchQuery(cronJob);
}
// call createFetchQuery() of the maintenanceCleanupStrategy
@Override
public void process(final List list, final CronJobModel cronJob)
{
maintenanceCleanupStrategy.process(list);
}
}
MaintenaceCleanupStrategy
: we need to have a strategy per cleanup job and implementcreateFetchQuery()
to generate a flexible search to retrieve objects in question andprocess()
to remove/archive objects.
public interface MaintenanceCleanupStrategy<T extends ItemModel, C extends CronJobModel>
{
// generate flexible search of the items to be removed
FlexibleSearchQuery createFetchQuery(C cjm);
// the remove logic for each item element here
void process(List<T> elements);
}
2. Implementation
To be continued 🙂
Software Craftsmanship, Stackextend author and Full Stack developer with 6+ years of experience in Java/Kotlin, Java EE, Angular and Hybris…
I’m Passionate about Microservice architectures, Hexagonal architecture, Event Driven architecture, Event Sourcing and Domain Driven design (DDD)…
Huge fan of Clean Code school, SOLID, GRASP principles, Design Patterns, TDD and BDD.
Hi Mouad EL Fakir,
May you please write an article for OCC, Oauth 2 etc for Hybris?
Hi Mouad EL Fakir,
Is there a way to clean up orphaned CMS componets, pages etc?