{{announcement.body}}
{{announcement.title}}

Spring Batch: Implement CompositeItemProcessor

DZone 's Guide to

Spring Batch: Implement CompositeItemProcessor

In this article, learn how to make the use of CompositeItemProcessor while reading data from MySQL DB to file-systems.

· Database Zone ·
Free Resource

Spring flowers

In this article, we'll learn how to make the use of CompositeItemProcessor while reading data from MySQL DB to file-systems. This is one of the decorators of Item Reader and Writer Implementations family.

Use case: In some cases, a user needs specialized behavior to be appended to a pre-existing ItemReader. Spring Batch offers some out-of-the-box decorators that can add additional behavior to to your ItemReader and ItemWriter implementations.

Spring Batch includes the following decorators:

ClassifierCompositeItemProcessor

The ClassifierCompositeItemProcessor is an ItemProcessor that calls one of a collection of ItemProcessor implementations based on a router pattern implemented through the provided Classifier. Spring Batch provides a ClassifierCompositeItemProcessorBuilder to construct an instance of the ClassifierCompositeItemProcessor.

The composite that passes the item through a sequence of injected ItemTransformers (return value of the previous transformation is the entry value of the next).

You might also be interested in: Spring Batch — Read From XML and Write to Mongo

Note that the user is responsible for injecting a chain of s that conforms to declared input and output types.

Maven Dependency: Project dependency needed to run this project.

XML


CustomLineAggregator: Interface used to create string representing object.

Java


CustomerRowMapper: An interface used by for mapping rows of a on a per-row basis. Implementations of this interface perform the actual work of mapping each row to a result object, but you don't need to worry about exception handling. It will be caught and handled by the calling JdbcTemplate. 

Java


Customer: Model class which holds Customer data.

Java


FilteringItemProcessor: Interface for item transformation. Given an item as input, this interface provides an extension point which allows for the application of business logic in an item oriented processing scenario. It should be noted that while it's possible to return a different type than the one provided, it's not strictly necessary. Furthermore, returning null indicates that the item should not be continued to be processed.

Here returning only even Customer Ids to be write into the file. 

Java


UpperCaseItemProcessor: Here we are making firstName and LastName as uppercase.

Java


JobConfiguration: This is the main configuration file.

Java


CompositeItemProcessApplication:

Java


application.properties:

Java


schema.sql:

SQL


data.sql:

Java


Output:

Java


Thanks for reading!

Further Reading

Spring Batch

Introduction to Spring Batch

Topics:
spring batch ,database ,tutorial ,compositeitemprocessor ,mysql

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}