Using Simple Template

Learn how to use Simple Template for Distributed Process by following provided example.

In the example, we want to:
  • Query the database for a list of some Items

  • Execute some complicated computation on data that comes with each instance of that Item

  • Remove all the Items we found

We should expect the query to return millions of records. Searching the database in our case would be an expensive process. This is why we'd like to leverage the whole cluster to do the work fast.

Set Up Your Distributed Process

First, we decide on how to query an Item, and what is the safest and minimum set of data we want for a single batch execution. Remember that between turns, a distributed process stores data in a Blob object in a database, so it must be serializable. For simplicity, we use a List of PKs, and the title item. The QueryBasedCreationData class helps us to set up the whole process. By default, QueryBasedCreationData produces exactly a List of PKs as a result of the query. First, we implement the logic that is to be executed on a single instance of our TitleModel. Since SimpleBatchModel has a context attribute, we can get it and simply cast to the List of PKs, and finally convert it to the TitleModel. Then we can play with it. This is an example implementation of the SimpleBatchProcessor interface we have to provide:
public class ItemProcessingTestBatchProcessor implements SimpleBatchProcessor
{
    private ModelService modelService;
 
    @Override
    public void process(final SimpleBatchModel inputBatch)
    {
        final List<PK> pks = asListOfPks(inputBatch.getContext());
        pks.stream().map(pk -> modelService.<TitleModel> get(pk)).forEach(title -> {
            // Do some very complicated logic here with a TitleModel
            modelService.remove(title);
        });
    }
 
    private List<PK> asListOfPks(final Object ctx)
    {
        Preconditions.checkState(ctx instanceof List, "ctx must be instance of a List");
        return (List<PK>) ctx;
    }
 
    @Required
    public void setModelService(final ModelService modelService)
    {
        this.modelService = modelService;
    }
}

Register a SimpleDistributedProcessHandler Bean

Next, we register a new bean of the SimpleDistributedProcessHandler class in a Spring context, and inject our newly created implementation of the SimpleBatchProcessor interface into it:
<bean id="titlesProcessing" class="de.hybris.platform.processing.distributed.simple.SimpleDistributedProcessHandler" parent="abstractSimpleDistributedProcessHandler">
    <constructor-arg>
        <bean class="de.hybris.example.ItemProcessingTestBatchProcessor" >
            <property name="modelService" ref="modelService"/>
        </bean>
    </constructor-arg>
</bean>

Prepare and Run the Process

To prepare the process, we provide a concrete instance of SimpleAbstractDistributedProcessCreationData. We use the QueryBasedCreationData class as our implementation. This class comes with a Builder that allows for an easy setup. Since the QueryBasedCreationData class needs FlexibleSearchService to work, we make sure we have it injected into our class. We start the whole process via DistributedProcessService, and wait for a result:
final QueryBasedCreationData processData = QueryBasedCreationData.builder() //
				.withQuery("SELECT {PK} FROM {Title}") //
				.withHandlerId("titlesProcessing")
				.build();
				
				final DistributedProcessModel process = distributedProcessService.create(processData);
				distributedProcessService.start(process.getCode());
				distributedProcessService.wait(process.getCode(), 100);
We have now created an instance of the QueryBasedCreationData class. We instructed it to use FlexibleSearch to query for all existing title items in the system. We instructed it also to use the previously registered SimpleDistributedProcessHandler under Bean ID titlesProcessing. That Bean was set up to use our custom implementation of SimpleBatchProcessor. By default, the batch size is set to 100, and the number of retries is 3.

If you think your case is so complicated that QueryBasedCreationData is not enough for your needs, you can extend the SimpleAbstractDistributedProcessCreationData class and implement your batching strategy yourself. All classes and methods provided by Simple Template for Distributed Process have public or protected access so you can have more control over the whole process. If you think that the problem is even more complicated than just a turn-based process with retries, go for the pure Distributed Process framework and implement it yourself.

Builder Methods

You can use the following QueryBasedCreationData's Builder methods to have more control over your distributed process:
  • withProcessId(String) provides a custom process Id

  • withNodeGroup(String) specifies on which cluster node group you want to execute your process

  • withBatchSize(int) changes the default batch size

  • withNumOfRetries(int) changes the number of retries for an individual batch

  • withQueryParams(Map<String, Object>) passes query parameters for FlexibleSearch

  • withResultClasses(List<Class<?>>) sets up custom result class list; you may find it handy when querying for a few item attributes

  • withProcessModelClass(Class<? extends SimpleDistributedProcessModel>) sets up a custom process model

  • useDatabasePaging() instructs QueryBasedCreationData to use database level paging

  • withBeforeQueryHook(QueryHook) provides a custom before-query logic.

See the de.hybris.platform.processing.distributed.simple.SimpleDistributedProcessIntegrationTest test class for examples of using QueryBasedCreationData.