Simple Template for Distributed Process
The Distributed Process framework allows you to process some of your workload on the whole cluster. Simple Template for the Distributed Process framework allows you to easily implement and set up workload processing, and use it for your custom applications. It supports processing of workload that can be split into batches and may require possible retries for failed tasks.
What Simple Template Supports
Simple Template provides a set of technical classes and interfaces that must be extended, implemented, or just declared in a Spring context to work. You can also control the Simple Template default behavior using properties it comes with.
Crucial Classes and Interfaces
- SimpleAbstractDistributedProcessHandler is a main class that controls the whole flow of a distributed process execution. It is an abstract class that implements DistributedProcessHandler. Simple Template provides two concrete classes that extend SimpleAbstractDistributedProcessHandler:
- SimpleDistributedProcessHandler provides implementation that operates directly on the SimpleBatchProcessor interface. SimpleBatchProcessor executes logic for individual batches. Register this class under a unique ID in a Spring context to set up a distributed process.
- SimpleScriptingDistributedProcessHandler provides implementation that executes logic for individual batches from an existing Script via Scripting Engine. It doesn't require any interaction with a Spring context. For details, see Scripting Engine
- SimpleBatchProcessor is an interface that you must implement and inject into SimpleDistributedProcessHandler via Spring. It provides logic responsible for executing a single batch of workload.
- SimpleAbstractDistributedProcessCreationData is an abstract class and is the basis for the creation of the whole distributed process. It provides information on batch size, number of retries, and actual data for individual batches. Implement at least one method that returns a stream of SimpleBatchCreationData objects. Simple Template comes with two default concrete classes that extend SimpleAbstractDistributedProcessCreationData:
- QueryBasedCreationData provides implementation that operates on a FlexibleSearch query and builds batches from the result.
- CollectionBasedCreationData provides implementation that operates on any Collection and builds batches from elements of that Collection.
- SimpleBatchCreationData is a simple POJO class that is a wrapper around the batch context Object. A context Object is a real input data required for a particular, single batch execution logic. Since that Object is persisted in a technical Batch item in a database, it is required that it implements Serializable interface.
Items
- SimpleDistributedProcess provides the additional batchSize attribute
- SimpleBatch provides the additional resultBatchId, retries, and context attributes
Spring Beans
- abstractSimpleDistributedProcessHandler
By default, Simple Template has a registered abstract Bean of the SimpleDistributedProcessHandler class. It enables you to set concrete Beans of the SimpleDistributedProcessHandler class in the most convinient way. All concrete Beans must refer to this Bean as a parent Bean.
- simpleScriptingDistributedProcessHandler is responsible for handling execution of a distributed process with use of existing Script that contains business logic for individual batches.
Configuration
- The distributed.process.simple.template.max.batch.retries property sets the number of retries for single Batch execution logic. Each time execution throws an exception, a particular batch is scheduled for a next turn until the max number of retries is reached. After that, the whole process is considered as failed. The property default value is 3.
- The distributed.process.simple.template.batch.size property sets a batch size. The default value is 100.
QueryBasedCreationData
final QueryBasedCreationData processData = QueryBasedCreationData.builder(flexibleSearchService) //
.withQuery("SELECT {PK} FROM {Title} ORDER BY {code}") //
.useDatabasePaging() //
.withHandlerId("titlesProcessing") //
.build();
Hook for QueryBasedCreationData
final QueryBasedCreationData processData = QueryBasedCreationData.builder(flexibleSearchService) //
.withQuery("SELECT {PK} FROM {Title}")
.withHandlerId("titlesProcessing") //
.withBeforeQueryHook(() -> sessionService.setAttribute("foo", "bar")) //
.build();
CollectionBasedCreationData
// assuming Title PKs are obtained somewhere earlier in a code
final List<PK> myTitles;
final CollectionBasedCreationData testProcessData = CollectionBasedCreationData.builder() //
.withElements(myTitles) //
.withHandlerId("titlesProcessing") //
.build();
Custom Process Model
// Assuming CustomSimpleDistributedProcess is a name of a custom process item
final QueryBasedCreationData testProcessData = QueryBasedCreationData.builder() //
.withQuery("SELECT {PK} FROM {Title}") //
.withHandlerId("testSimpleDistributedProcessHandler") //
.withProcessModelClass(CustomSimpleDistributedProcessModel.class) //
.build();
final CustomSimpleDistributedProcessModel process = distributedProcessService.create(processData);
// now you have possibility to set some custom attributes before process start
process.setCustomField("foo");
// you need to save it
modelService.save(process);
// run as usual
distributedProcessService.start(process.getCode());