Search code examples
javaspring-bootspring-batchchunks

Best way to implement multiple business logic in ItemProcessor


I am using only one step that used chunk oriented processing (ItemReader, ItemProcessor, ItemWriter), to read the data from a file, process the data (there is many business logic behind with conditions) and the last step which is to write the data into the database.

The part that I found little bit tricky is the processor part, I cannot really add conditional flow since I use chunk processing neither the composite processor since there are conditions that needs to be met to process certain flow (after doing many checks on the data - which leads sometimes on a direct data return).

Is there a better way to implement all this business logic with many conditions and checks or a way to call an ItemProcessor in another ItemProcessor under a certain condition using chunk processing in spring batch?

Thank you


Solution

  • There is no best way for that. It depends on the use case. So I will try to give some guidance.

    The chunk-oriented processing model is designed to have a single item processor with the following contract: given an input item, the item processor can have one of the following outcomes:

    • Return the same item (or a transformed one): in this case, the item will be passed to the writer
    • Return null: in this case, the item will be filtered (ie not passed to the writer)
    • Throw an exception: in which case, the step will fail if it is a simple chunk-oriented step, or the item will be retried/skipped according to the retry/skip policy if the step is a fault-tolerant chunk-oriented step.

    Now how you implement the logic inside an item processor is up to you. You can for example:

    • Compose multiple item processors into a single one (like the CompositeItemProcessor provided by Spring Batch)
    • Delegate the processing to other item processors
    • Adapt an existing class to act as an item processor (like the ItemProcessorAdapter or the FunctionItemProcessor provided by Spring Batch)
    • Etc

    There are several use cases that can be implemented with an item processor, like data transformation, filtering, validation, etc. You can find more details in the Item processing section of the reference documentation.

    What you really need to clearly define is what is the outcome of the entire validation process (as a black box), no matter how complex it is and only then choose the best pattern to implement it in your case (note how I said "the best in your case", because there is no absolute best way).