List of Listings – Spring Batch in Action

List of Listings

Chapter 1. Introducing Spring Batch

Listing 1.1. Spring Batch interfaces for chunk processing

Listing 1.2. Spring configuration of the FlatFileItemReader

Listing 1.3. Implementing the ProductJdbcItemWriter

Listing 1.4. Spring configuration of the read-write step

Listing 1.5. Implementation of decompression tasklet

Listing 1.6. Spring configuration of the decompress tasklet

Listing 1.7. Spring configuration for the batch infrastructure

Listing 1.8. Referring to job parameters in the Spring configuration

Listing 1.9. Integration test for the import product test

Listing 1.10. Setting the skip policy when reading records from the flat file

Listing 1.11. Testing the job correctly skips incorrect lines with a new test method

Chapter 2. Spring Batch concepts

Listing 2.1. Configuration of a persistent job repository

Listing 2.2. Configuring a job with linear flow

Listing 2.3. Configuring a job with nonlinear flow

Chapter 3. Batch configuration

Listing 3.1. Spring Batch XML namespace and prefix

Listing 3.2. Using the Spring Batch namespace as the default namespace

Listing 3.3. Nested configuration of a job

Listing 3.4. Configuring a job parameter validator

Listing 3.5. Configuring tasklet attributes

Listing 3.6. Using tasklet configuration attributes

Listing 3.7. Configuring chunk retry, skip, and completion

Listing 3.8. Using child elements in the tasklet configuration

Listing 3.9. Configuring skippable exceptions

Listing 3.10. Configuring streams in a chunk

Listing 3.11. Configuring a transactional JMS item reader

Listing 3.12. Configuring an in-memory job repository

Listing 3.13. Configuring a persistent job repository

Listing 3.14. Configuring batch parameters with SpEL

Listing 3.15. Listening to job execution with a listener

Listing 3.16. Listening to job execution with annotations

Listing 3.17. Implementing an annotation-based step listener

Listing 3.18. Using configuration inheritance for steps

Listing 3.19. Merging two lists with configuration inheritance

Chapter 4. Running batch jobs

Listing 4.1. Writing an ExitCodeMapper to map job and system exit codes

Listing 4.2. Scheduling with Spring and XML

Listing 4.3. Configuring Spring in a web application

Listing 4.4. A Spring MVC controller job launcher

Listing 4.5. Declaring Spring MVC’s servlet in web.xml

Listing 4.6. Configuring the job operator in Spring

Listing 4.7. Setting the stop flag from a tasklet

Listing 4.8. An annotated listener to stop a job execution

Listing 4.9. Registering the stop listener on the step

Chapter 5. Reading data

Listing 5.1. Configuring a FlatFileItemReader

Listing 5.2. The Product bean

Listing 5.3. Custom FieldSetMapper for creating Product objects

Listing 5.4. Product data file using JSON

Listing 5.5. JSON data processing as Java code

Listing 5.6. A JsonLineMapper wrapper to create data objects

Listing 5.7. Reading multiline records with a custom RecordSeparatorPolicy

Listing 5.8. Configuring a composite LineMapper

Listing 5.9. XML product data converted from JSON

Listing 5.10. Configuring a StaxEventItemReader

Listing 5.11. Configuring a JdbcCursorItemReader

Listing 5.12. RowMapper implementation for Product

Listing 5.13. Setting SQL statement parameters in a JdbcCursorItemReader

Listing 5.14. Configuring a JdbcPagingItemReader

Listing 5.15. ORM mapping class

Listing 5.16. Configuring a HibernateCursorItemReader

Listing 5.17. Service adapter for the ProductService service

Listing 5.18. Configuring the ProductService as an ItemReader

Listing 5.19. Configuring a JmsItemReader class

Listing 5.20. Custom ItemReader implementation

Chapter 6. Writing data

Listing 6.1. A minimal FlatFileItemWriter configuration

Listing 6.2. A delimited pass-through field extractor

Listing 6.3. Configuring a BeanWrapperFieldExtractor

Listing 6.4. Computing fields in a FieldExtractor

Listing 6.5. Configuring a FieldExtractor

Listing 6.6. Configuring writing to a delimited file

Listing 6.7. Configuring a FormatterLineAggregator

Listing 6.8. Choosing a LineAggregator

Listing 6.9. Configuring multiple LineAggregators with a Map

Listing 6.10. Implementing a flat file footer and header

Listing 6.11. Configuring header and footer callbacks for a flat file

Listing 6.12. Example of XML file output

Listing 6.13. Implementing an XML header callback

Listing 6.14. Implementing an XML footer callback

Listing 6.15. Configuring a StaxEventItemWriter

Listing 6.16. StaxEventItemWriter output

Listing 6.17. Configuring multiple output files

Listing 6.18. Configuring a JdbcBatchItemWriter with named parameters

Listing 6.19. Implementing an ItemPreparedStatementSetter

Listing 6.20. Configuring a SQL statement with ? parameter markers

Listing 6.21. Annotating a domain class for ORM

Listing 6.22. HibernateItemWriter implementation

Listing 6.23. Configuring a PropertyExtractingDelegatingItemWriter

Listing 6.24. Configuring a JmsItemWriter

Listing 6.25. Converting a Customer to SimpleMailMessage

Listing 6.26. Configuring a SimpleMailMessageItemWriter

Listing 6.27. Creating a custom JDBC ItemWriter

Listing 6.28. Configuring a CompositeItemWriter

Listing 6.29. Configuring a routing BackToBackPatternClassifier

Chapter 7. Processing data

Listing 7.1. Implementation of a filtering item processor

Listing 7.2. Configuring an item processor in a chunk-oriented step

Listing 7.3. Mapping partner IDs with store IDs in a business component

Listing 7.4. Configuring the dedicated item processor to map product IDs

Listing 7.5. Using the ItemProcessorAdapter to plug in an existing Spring bean

Listing 7.6. A dedicated item processor to call the partner product mapper

Listing 7.7. Configuring an item processor to map partner products to store products

Listing 7.8. Configuring an item reader to execute the driving query

Listing 7.9. Implementing a DAO to load a product from its ID

Listing 7.10. Implementing an item processor to call the DAO

Listing 7.11. Configuring a driving query

Listing 7.12. Filtering existing products with an item processor

Listing 7.13. Configuring the filtering item processor

Listing 7.14. Configuring a validating item processor

Listing 7.15. Embedding validation logic in the configuration with Valang

Listing 7.16. Embedding validation constraint in the Product class

Listing 7.17. A Spring Batch validator for Bean Validation

Listing 7.18. Chaining item processors with the composite item processor

Chapter 8. Implementing bulletproof jobs

Listing 8.1. Configuring exceptions to skip in a chunk-oriented step

Listing 8.2. Implementing a skip policy with no skip limit

Listing 8.3. Plugging in a skip policy in a chunk-oriented step

Listing 8.4. Logging skipped items with a skip listener

Listing 8.5. Registering a skip listener

Listing 8.6. Configuring retryable exceptions

Listing 8.7. Combining retry and skip

Listing 8.8. Using a retry policy for different behavior with concurrent exceptions

Listing 8.9. Implementing a retry listener to log retried operations

Listing 8.10. Registering a retry listener

Listing 8.11. Programmatic retry in a tasklet

Listing 8.12. Calling the web service without retry logic

Listing 8.13. Configuring transparent retry with Spring AOP

Listing 8.14. Implementing ItemStream to make an item reader restartable

Chapter 9. Transaction management

Listing 9.1. Avoiding a rollback for an exception

Listing 9.2. Using transaction synchronization to avoid losing messages

Listing 9.3. A JMS message containing an Order object

Listing 9.4. Processing and tracking orders in an item writer

Listing 9.5. Detecting and filtering out duplicate messages with an item processor

Listing 9.6. Configuring the duplicates detection job

Listing 9.7. Updating the shipped order (idempotent processing)

Chapter 10. Controlling execution

Listing 10.1. Configuring a nonlinear flow

Listing 10.2. Choosing the exit status for a step with a step execution listener

Listing 10.3. Conditional flow with custom exit status using a step execution listener

Listing 10.4. Controlling job flow with a job execution decider

Listing 10.5. Configuring a conditional flow with a job execution listener

Listing 10.6. Implementing a listener interface to access the execution context

Listing 10.7. Writing data in the job execution context from a tasklet

Listing 10.8. Reading data from the job execution context in a tasklet

Listing 10.9. Writing data in the step execution context from a tasklet

Listing 10.10. Promoting data from the step to the job execution context

Listing 10.11. A tasklet reading data from its own property

Listing 10.12. Tasklets communicating through a holder

Listing 10.13. Configuring tasklets to use a holder

Listing 10.14. Referring to the holder using late binding

Listing 10.15. Externalizing a flow definition for reuse

Listing 10.16. Reusing a job definition in another job

Listing 10.17. Choosing how to end a job after a step execution

Chapter 11. Enterprise integration

Listing 11.1. Java representation of a job launch request

Listing 11.2. Launching a Spring Batch job from a JobLaunchRequest object

Listing 11.3. The echo Tasklet

Listing 11.4. Spring Integration configuration to launch jobs

Listing 11.5. Launching a job with Spring Integration

Listing 11.6. Receiving job submissions with a REST web controller

Listing 11.7. Tracking job submissions with a JDBC-based repository

Listing 11.8. Declaring the gateway and repository in the root application context

Listing 11.9. Implementing the filename generation strategy

Listing 11.10. Updating the repository to map the import with the job instance

Listing 11.11. Tasklet to map the import with the job instance

Listing 11.12. Configuring Castor to map XML to the Product class

Listing 11.13. Configuring the import products job

Listing 11.14. Representing the status of an import in the ProductImport class

Listing 11.15. Retrieving the status of imports from the repository

Listing 11.16. Communicating the status of an import with the web controller

Listing 11.17. Completing the Castor configuration to map the ProductImport class

Listing 11.18. Using the XML message converter with Spring MVC

Chapter 12. Monitoring jobs

Listing 12.1. The JobRepository interface

Listing 12.2. The JobExplorer interface

Listing 12.3. The JobOperator interface

Listing 12.4. Detecting failed job instances

Listing 12.5. Getting exceptions that cause job execution failure

Listing 12.6. Getting descriptions of problems that cause job execution failure

Listing 12.7. Implementation of the monitoring execution listener

Listing 12.8. Configuring the monitoring listener

Listing 12.9. JavaMail failure notifier

Listing 12.10. Configuration of the JavaMail failure notifier

Listing 12.11. Spring messaging notifier

Listing 12.12. Exporting a JobOperator through JMX

Listing 12.13. Configuring a JMX server connector

Chapter 13. Scaling and parallel processing

Listing 13.1. Implementing the Runnable interface

Listing 13.2. Configuring a multithreaded step

Listing 13.3. Console output when importing products using threads

Listing 13.4. Setting the throttle limit of a multithreaded step

Listing 13.5. Implementation of a synchronized reader

Listing 13.6. Configuring a thread-safe JdbcCursorItemReader with an indicator

Listing 13.7. Implementing a JDBC ItemWriter with a SQL indicator

Listing 13.8. Configuring parallel steps to import products

Listing 13.9. Configuring a task executor

Listing 13.10. Configuring Spring Integration for a remote chunking master

Listing 13.11. Configuring a master for remote chunking

Listing 13.12. Configuring Spring Integration for a remote chunking slave

Listing 13.13. Configuring a slave for remote chunking

Listing 13.14. Configuring step partitioning

Listing 13.15. Configuring step partitioning with a partition handler

Listing 13.16. Configuring partitioning with a MultiResourcePartitioner

Listing 13.17. Configuring a master for remote partitioning

Listing 13.18. Configuring a slave for remote partitioning

Listing 13.19. Custom ColumnRangePartitioner class

Chapter 14. Testing batch applications

Listing 14.1. Basic JUnit test case with annotations and assert methods

Listing 14.2. Testing a product validator for a non-null price

Listing 14.3. Testing a product validator for a positive price

Listing 14.4. ProductFieldSetMapper implements FieldSetMapper

Listing 14.5. Testing a FieldSetMapper with and without Mockito

Listing 14.6. A Product ItemListener implementation

Listing 14.7. Testing an ItemSupportListener

Listing 14.8. Testing a JobParametersValidator with Mockito’s spy feature

Listing 14.9. Writing to a database with the ProductItemWriter

Listing 14.10. Advanced testing of an ItemWriter

Listing 14.11. Testing batch workflow with JobExecutionDecider

Listing 14.12. Testing a simple tasklet with Spring Batch domain mocks

Listing 14.13. Testing an ItemProcessor with the Spring TestContext Framework

Listing 14.14. Testing an ItemReader with the Spring Batch Test module

Listing 14.15. Testing an ItemReader with StepScopeTestUtils' doInStepScope

Listing 14.16. Functional testing of the product step

Listing 14.17. Compute and write the product average into a file from a step

Listing 14.18. Functional testing of a step with a database

Listing 14.19. Testing a whole job

Appendix A. Setting up the development environment

Listing A.1. The pom.xml file Spring Batch dependencies

Listing A.2. Configuring Logback in logback-test.xml

Appendix B. Managing Spring Batch Admin

Listing B.1. Maven dependencies for Spring Batch Admin

Listing B.2. Configuring the database connection in

Listing B.3. Overriding infrastructure beans