Use cleanup job in SAP Hybris

Hybris Logo

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 job in hybris

  • Cleanup Cronjob : is an instance of the MaintenanceCleanupJobModel.
  • AbstractMaintenanceJobPerformable : contains all the logic of clean up inside the perform() 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 : extends AbstractMaintenaceJobPerformable and implements getFetchQuery() and process(), 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 implement createFetchQuery() to generate a flexible search to retrieve objects in question and process() 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 🙂

 

4.6 10 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Nick
Nick
5 years ago

Hi Mouad EL Fakir,

May you please write an article for OCC, Oauth 2 etc for Hybris?

Joe
Joe
4 years ago

Hi Mouad EL Fakir,
Is there a way to clean up orphaned CMS componets, pages etc?

2
0
Would love your thoughts, please comment.x
()
x