Using Simple Template
Learn how to use Simple Template for Distributed Process by following provided example.
-
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
Set Up Your Distributed Process
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
<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
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
-
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.