Class SimpleChunkProcessor<I,O>
java.lang.Object
org.springframework.batch.core.step.item.SimpleChunkProcessor<I,O>
- All Implemented Interfaces:
ChunkProcessor<I>,org.springframework.beans.factory.InitializingBean
- Direct Known Subclasses:
FaultTolerantChunkProcessor
public class SimpleChunkProcessor<I,O>
extends Object
implements ChunkProcessor<I>, org.springframework.beans.factory.InitializingBean
Simple implementation of the
ChunkProcessor interface that handles basic item
writing and processing. Any exceptions encountered will be rethrown.- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected io.micrometer.core.instrument.MeterRegistry -
Constructor Summary
ConstructorsConstructorDescriptionSimpleChunkProcessor(ItemProcessor<? super I, ? extends O> itemProcessor, ItemWriter<? super O> itemWriter) SimpleChunkProcessor(ItemWriter<? super O> itemWriter) -
Method Summary
Modifier and TypeMethodDescriptionvoidCheck mandatory properties.protected final voiddoAfterWrite(Chunk<O> items) Call the listener's after write method.protected final voiddoOnWriteError(Exception e, Chunk<O> items) Call listener's writerError method.protected final Oprotected final voidSurrounds the actual write call with listener callbacks.getAdjustedOutputs(Chunk<I> inputs, Chunk<O> outputs) Extension point for subclasses that want to adjust the outputs based on additional saved data in the inputs.protected intgetFilterCount(Chunk<I> inputs, Chunk<O> outputs) Extension point for subclasses to calculate the filter count.protected MulticasterBatchListener<I,O> protected voidinitializeUserData(Chunk<I> inputs) Extension point for subclasses to allow them to memorise the contents of the inputs, in case they are needed for accounting purposes later.protected booleanisComplete(Chunk<I> inputs) Extension point for subclasses that want to store additional data in the inputs.final voidprocess(StepContribution contribution, Chunk<I> inputs) voidregisterListener(StepListener listener) Register a listener for callbacks at the appropriate stages in a process.voidsetItemProcessor(ItemProcessor<? super I, ? extends O> itemProcessor) voidsetItemWriter(ItemWriter<? super O> itemWriter) voidsetListeners(List<? extends StepListener> listeners) Register someStepListeners with the handler.voidsetMeterRegistry(io.micrometer.core.instrument.MeterRegistry meterRegistry) Set the meter registry to use for metrics.protected voidstopTimer(io.micrometer.core.instrument.Timer.Sample sample, StepExecution stepExecution, String metricName, String status, String description) transform(StepContribution contribution, Chunk<I> inputs) protected voidSimple implementation delegates to thedoWrite(Chunk)method and increments the write count in the contribution.protected voidwriteItems(Chunk<O> items)
-
Field Details
-
meterRegistry
protected io.micrometer.core.instrument.MeterRegistry meterRegistry
-
-
Constructor Details
-
SimpleChunkProcessor
public SimpleChunkProcessor(@Nullable ItemProcessor<? super I, ? extends O> itemProcessor, ItemWriter<? super O> itemWriter) -
SimpleChunkProcessor
-
-
Method Details
-
setItemProcessor
- Parameters:
itemProcessor- theItemProcessorto set
-
setItemWriter
- Parameters:
itemWriter- theItemWriterto set
-
setMeterRegistry
public void setMeterRegistry(io.micrometer.core.instrument.MeterRegistry meterRegistry) Set the meter registry to use for metrics.- Parameters:
meterRegistry- the meter registry- Since:
- 5.0
-
afterPropertiesSet
Check mandatory properties.- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Throws:
Exception- See Also:
-
InitializingBean.afterPropertiesSet()
-
setListeners
Register someStepListeners with the handler. Each will get the callbacks in the order specified at the correct stage.- Parameters:
listeners- list ofStepListenerinstances.
-
registerListener
Register a listener for callbacks at the appropriate stages in a process.- Parameters:
listener- aStepListener
-
getListener
- Returns:
- the listener
-
doProcess
- Parameters:
item- the input item- Returns:
- the result of the processing
- Throws:
Exception- thrown if error occurs.
-
doWrite
Surrounds the actual write call with listener callbacks.- Parameters:
items- list of items to be written.- Throws:
Exception- thrown if error occurs.
-
doAfterWrite
Call the listener's after write method.- Parameters:
items- list of items that were just written.
-
doOnWriteError
Call listener's writerError method.- Parameters:
e- exception that occurred.items- list of items that failed to be written.
-
writeItems
- Parameters:
items- list of items to be written.- Throws:
Exception- thrown if error occurs.
-
process
- Specified by:
processin interfaceChunkProcessor<I>- Throws:
Exception
-
initializeUserData
Extension point for subclasses to allow them to memorise the contents of the inputs, in case they are needed for accounting purposes later. The default implementation sets up some user data to remember the original size of the inputs. If this method is overridden then some or all ofisComplete(Chunk),getFilterCount(Chunk, Chunk)andgetAdjustedOutputs(Chunk, Chunk)might also need to be, to ensure that the user data is handled consistently.- Parameters:
inputs- the inputs for the process
-
getFilterCount
Extension point for subclasses to calculate the filter count. Defaults to the difference between input size and output size.- Parameters:
inputs- the inputs after transformationoutputs- the outputs after transformation- Returns:
- the difference in sizes
- See Also:
-
isComplete
Extension point for subclasses that want to store additional data in the inputs. Default just checks if inputs are empty.- Parameters:
inputs- the input chunk- Returns:
- true if it is empty
- See Also:
-
getAdjustedOutputs
Extension point for subclasses that want to adjust the outputs based on additional saved data in the inputs. Default implementation just returns the outputs unchanged.- Parameters:
inputs- the inputs for the transformationoutputs- the result of the transformation- Returns:
- the outputs unchanged
- See Also:
-
write
protected void write(StepContribution contribution, Chunk<I> inputs, Chunk<O> outputs) throws Exception Simple implementation delegates to thedoWrite(Chunk)method and increments the write count in the contribution. Subclasses can handle more complicated scenarios, e.g.with fault tolerance. If output items are skipped they should be removed from the inputs as well.- Parameters:
contribution- the current step contributioninputs- the inputs that gave rise to the outputsoutputs- the outputs to write- Throws:
Exception- if there is a problem
-
transform
- Throws:
Exception
-
stopTimer
protected void stopTimer(io.micrometer.core.instrument.Timer.Sample sample, StepExecution stepExecution, String metricName, String status, String description)
-