ebook img

The Definitive Guide to Spring Batch: Modern Finite Batch Processing in the Cloud PDF

474 Pages·2019·7.688 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview The Definitive Guide to Spring Batch: Modern Finite Batch Processing in the Cloud

The Definitive Guide to Spring Batch Modern Finite Batch Processing in the Cloud — Second Edition — Michael T. Minella Foreword by Dave Syer, Spring Batch Project Founder The Definitive Guide to Spring Batch Modern Finite Batch Processing in the Cloud Second Edition Michael T. Minella Foreword by Dave Syer, Spring Batch Project Founder The Definitive Guide to Spring Batch: Modern Finite Batch Processing in the Cloud Michael T. Minella Chicago, IL, USA ISBN-13 (pbk): 978-1-4842-3723-6 ISBN-13 (electronic): 978-1-4842-3724-3 https://doi.org/10.1007/978-1-4842-3724-3 Copyright © 2019 by Michael T. Minella This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Steve Anglin Development Editor: Matthew Moodie Coordinating Editor: Mark Powers Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected]; for reprint, paperback, or audio rights, please email [email protected]. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book's product page, located at www.apress.com/9781484237236. For more detailed information, please visit http://www.apress.com/source-code. Printed on acid-free paper To my daughter, Addison. If Daddy can do this, you can do anything. Contents About the Author ���������������������������������������������������������������������������������������������������xiii About the Technical Reviewers �������������������������������������������������������������������������������xv Acknowledgments �������������������������������������������������������������������������������������������������xvii Foreword ����������������������������������������������������������������������������������������������������������������xix ■ Chapter 1: Batch and Spring ���������������������������������������������������������������������������������1 A History of Batch Processing ������������������������������������������������������������������������������������������2 Batch Challenges �������������������������������������������������������������������������������������������������������������3 Why Do Batch Processing in Java? ����������������������������������������������������������������������������������4 Other Uses for Spring Batch ���������������������������������������������������������������������������������������������5 The Spring Batch Framework �������������������������������������������������������������������������������������������8 Defining Jobs with Spring ����������������������������������������������������������������������������������������������������������������������9 Managing Jobs �������������������������������������������������������������������������������������������������������������������������������������10 Local and Remote Parallelization ���������������������������������������������������������������������������������������������������������10 Standardizing I/O ����������������������������������������������������������������������������������������������������������������������������������10 The Rest of the Spring Batch Ecosystem ����������������������������������������������������������������������������������������������10 And All the Features of Spring ��������������������������������������������������������������������������������������������������������������11 How This Book Works �����������������������������������������������������������������������������������������������������11 Summary ������������������������������������������������������������������������������������������������������������������������12 ■ Chapter 2: Spring Batch 101 �������������������������������������������������������������������������������13 The Architecture of Batch �����������������������������������������������������������������������������������������������13 Examining Jobs and Steps �������������������������������������������������������������������������������������������������������������������14 Job Execution ���������������������������������������������������������������������������������������������������������������������������������������15 v ■ Contents Parallelization ���������������������������������������������������������������������������������������������������������������������������������������17 Documentation �������������������������������������������������������������������������������������������������������������������������������������20 Project Setup ������������������������������������������������������������������������������������������������������������������21 Obtaining Spring Batch �������������������������������������������������������������������������������������������������������������������������21 It’s the Law: Hello, World! �����������������������������������������������������������������������������������������������24 Running Your Job �����������������������������������������������������������������������������������������������������������27 Summary ������������������������������������������������������������������������������������������������������������������������28 ■ Chapter 3: Sample Job ����������������������������������������������������������������������������������������29 Understanding Agile Development ���������������������������������������������������������������������������������29 Capturing Requirements with User Stories ������������������������������������������������������������������������������������������30 Capturing Design with Test-Driven Development ���������������������������������������������������������������������������������32 Using a Version-Control System �����������������������������������������������������������������������������������������������������������32 Working with a True Development Environment �����������������������������������������������������������������������������������32 Understanding the Requirements of the Statement Job ������������������������������������������������32 Designing a Batch Job ���������������������������������������������������������������������������������������������������38 Job Description �������������������������������������������������������������������������������������������������������������������������������������39 Understanding the Data Model �������������������������������������������������������������������������������������������������������������40 Summary ������������������������������������������������������������������������������������������������������������������������41 ■ Chapter 4: Understanding Jobs and Steps ����������������������������������������������������������43 Introducing a Job �����������������������������������������������������������������������������������������������������������43 Tracing a Job’s Lifecycle ����������������������������������������������������������������������������������������������������������������������44 Configuring a Job �����������������������������������������������������������������������������������������������������������46 Basic Job Configuration ������������������������������������������������������������������������������������������������������������������������46 Job Parameters ������������������������������������������������������������������������������������������������������������������������������������47 Working with Job Listeners ������������������������������������������������������������������������������������������������������������������61 ExecutionContext ����������������������������������������������������������������������������������������������������������������������������������64 Manipulating the ExecutionContext ������������������������������������������������������������������������������������������������������65 Working with Steps ��������������������������������������������������������������������������������������������������������69 Tasklet vs� Chunk Processing ���������������������������������������������������������������������������������������������������������������69 Step Configuration ��������������������������������������������������������������������������������������������������������������������������������70 vi ■ Contents Understanding the Other Types of Tasklets�������������������������������������������������������������������������������������������71 Step Flow ����������������������������������������������������������������������������������������������������������������������������������������������88 Summary ����������������������������������������������������������������������������������������������������������������������105 ■ Chapter 5: JobRepository and Metadata �����������������������������������������������������������107 What Is the Job Repository?�����������������������������������������������������������������������������������������107 Using a Relational Database ���������������������������������������������������������������������������������������������������������������107 The In-Memory Job Repository ����������������������������������������������������������������������������������������������������������111 Configuring the Batch Infrastructure ����������������������������������������������������������������������������112 The BatchConfigurer Interface ������������������������������������������������������������������������������������������������������������112 Customizing the JobRepository ����������������������������������������������������������������������������������������������������������112 Customizing the TransactionManager ������������������������������������������������������������������������������������������������114 Customizing the JobExplorer ��������������������������������������������������������������������������������������������������������������115 Customizing the JobLauncher ������������������������������������������������������������������������������������������������������������116 Database Configuration ����������������������������������������������������������������������������������������������������������������������117 Using Job Metadata �����������������������������������������������������������������������������������������������������118 The JobExplorer ����������������������������������������������������������������������������������������������������������������������������������118 Summary ����������������������������������������������������������������������������������������������������������������������122 ■ Chapter 6: Running a Job ����������������������������������������������������������������������������������123 Starting a Job with Spring Boot �����������������������������������������������������������������������������������123 Launching a Job via REST ��������������������������������������������������������������������������������������������125 Scheduling with Quartz ����������������������������������������������������������������������������������������������������������������������130 Stopping a Job �������������������������������������������������������������������������������������������������������������134 The Natural End ����������������������������������������������������������������������������������������������������������������������������������134 Programmatic Ending �������������������������������������������������������������������������������������������������������������������������135 Error Handling �������������������������������������������������������������������������������������������������������������������������������������148 Controlling Restart �������������������������������������������������������������������������������������������������������150 Preventing a Job from Being Rerun ����������������������������������������������������������������������������������������������������150 Configuring the Number of Restarts ���������������������������������������������������������������������������������������������������151 Rerunning a Complete Step ����������������������������������������������������������������������������������������������������������������152 Summary ����������������������������������������������������������������������������������������������������������������������153 vii ■ Contents ■ Chapter 7: ItemReaders �������������������������������������������������������������������������������������155 The ItemReader Interface ���������������������������������������������������������������������������������������������155 File Input ����������������������������������������������������������������������������������������������������������������������156 Flat Files ���������������������������������������������������������������������������������������������������������������������������������������������156 XML �����������������������������������������������������������������������������������������������������������������������������������������������������183 JSON�����������������������������������������������������������������������������������������������������������������������������188 Database Input �������������������������������������������������������������������������������������������������������������191 JDBC ���������������������������������������������������������������������������������������������������������������������������������������������������191 Hibernate ��������������������������������������������������������������������������������������������������������������������������������������������198 JPA ������������������������������������������������������������������������������������������������������������������������������������������������������202 Stored Procedures ������������������������������������������������������������������������������������������������������������������������������204 Spring Data �����������������������������������������������������������������������������������������������������������������������������������������206 Existing Services ����������������������������������������������������������������������������������������������������������211 Custom Input ����������������������������������������������������������������������������������������������������������������213 Error Handling ��������������������������������������������������������������������������������������������������������������218 Skipping Records ��������������������������������������������������������������������������������������������������������������������������������218 Logging Invalid Records ���������������������������������������������������������������������������������������������������������������������220 Dealing with No Input �������������������������������������������������������������������������������������������������������������������������222 Summary ����������������������������������������������������������������������������������������������������������������������223 ■ Chapter 8: ItemProcessors ��������������������������������������������������������������������������������225 Introduction to ItemProcessors ������������������������������������������������������������������������������������225 Using Spring Batch’s ItemProcessors ��������������������������������������������������������������������������227 ValidatingItemProcessor ���������������������������������������������������������������������������������������������������������������������227 ItemProcessorAdapter ������������������������������������������������������������������������������������������������������������������������234 ScriptItemProcessor ���������������������������������������������������������������������������������������������������������������������������236 CompositeItemProcessor ��������������������������������������������������������������������������������������������������������������������237 Writing Your Own ItemProcessor ����������������������������������������������������������������������������������242 Filtering Items �������������������������������������������������������������������������������������������������������������������������������������242 Summary ����������������������������������������������������������������������������������������������������������������������244 viii ■ Contents ■ Chapter 9: ItemWriters ��������������������������������������������������������������������������������������245 Introduction to ItemWriters ������������������������������������������������������������������������������������������246 File-Based ItemWriters �������������������������������������������������������������������������������������������������247 FlatFileItemWriter �������������������������������������������������������������������������������������������������������������������������������247 StaxEventItemWriter ���������������������������������������������������������������������������������������������������������������������������259 Database-Based ItemWriters ���������������������������������������������������������������������������������������263 JdbcBatchItemWriter ��������������������������������������������������������������������������������������������������������������������������263 HibernateItemWriter ���������������������������������������������������������������������������������������������������������������������������270 JpaItemWriter �������������������������������������������������������������������������������������������������������������������������������������275 Spring Data ItemWriters �����������������������������������������������������������������������������������������������279 MongoDB ��������������������������������������������������������������������������������������������������������������������������������������������279 Neo4J �������������������������������������������������������������������������������������������������������������������������������������������������282 Pivotal Gemfire and Apache Geode �����������������������������������������������������������������������������������������������������286 Repository ������������������������������������������������������������������������������������������������������������������������������������������291 Alternative Output Destination ItemWriters ������������������������������������������������������������������294 ItemWriterAdapter ������������������������������������������������������������������������������������������������������������������������������294 PropertyExtractingDelegatingItemWriter ��������������������������������������������������������������������������������������������297 JmsItemWriter ������������������������������������������������������������������������������������������������������������������������������������299 SimpleMailMessageItemWriter �����������������������������������������������������������������������������������������������������������304 Multipart ItemWriters ���������������������������������������������������������������������������������������������������310 MultiResourceItemWriter ��������������������������������������������������������������������������������������������������������������������310 CompositeItemWriter ��������������������������������������������������������������������������������������������������������������������������319 ClassifierCompositeItemWriter �����������������������������������������������������������������������������������������������������������322 Summary ����������������������������������������������������������������������������������������������������������������������326 ■ Chapter 10: Sample Application ������������������������������������������������������������������������327 Reviewing the Statement Job ��������������������������������������������������������������������������������������327 Setting Up a New Project ���������������������������������������������������������������������������������������������328 Importing Customer Updates ���������������������������������������������������������������������������������������330 Validating Customer ID �����������������������������������������������������������������������������������������������������������������������338 Writing Customer Updates ������������������������������������������������������������������������������������������������������������������339 ix ■ Contents Importing Transactions �������������������������������������������������������������������������������������������������343 Reading Transactions �������������������������������������������������������������������������������������������������������������������������345 Writing Transactions ���������������������������������������������������������������������������������������������������������������������������346 Applying Transactions to Current Balance ��������������������������������������������������������������������347 Reading the Transaction Data �������������������������������������������������������������������������������������������������������������348 Updating the Account Balance ������������������������������������������������������������������������������������������������������������349 Generating Monthly Statement�������������������������������������������������������������������������������������349 Reading the Statement Data ���������������������������������������������������������������������������������������������������������������350 Enrich the Statement with Accounts ��������������������������������������������������������������������������������������������������353 Writing Statements �����������������������������������������������������������������������������������������������������������������������������355 Summary ����������������������������������������������������������������������������������������������������������������������359 ■ Chapter 11: Scaling and Tuning �������������������������������������������������������������������������361 Profiling Your Batch Process ����������������������������������������������������������������������������������������361 A Tour of VisualVM ������������������������������������������������������������������������������������������������������������������������������362 Profiling Spring Batch Applications ����������������������������������������������������������������������������������������������������366 Scaling a Job ����������������������������������������������������������������������������������������������������������������374 Multithreaded Steps ���������������������������������������������������������������������������������������������������������������������������374 Parallel Steps ��������������������������������������������������������������������������������������������������������������������������������������376 AsyncItemProcessor and AsyncItemWriter �����������������������������������������������������������������������������������������381 Partitioning �����������������������������������������������������������������������������������������������������������������������������������������384 Remote Chunking �������������������������������������������������������������������������������������������������������������������������������399 Summary ����������������������������������������������������������������������������������������������������������������������405 ■ Chapter 12: Cloud Native Batch �������������������������������������������������������������������������407 Twelve Factor Applications �������������������������������������������������������������������������������������������408 Codebase ��������������������������������������������������������������������������������������������������������������������������������������������408 Dependencies �������������������������������������������������������������������������������������������������������������������������������������408 Config �������������������������������������������������������������������������������������������������������������������������������������������������409 Backing Services ��������������������������������������������������������������������������������������������������������������������������������409 Build, Release, Run �����������������������������������������������������������������������������������������������������������������������������409 Processes �������������������������������������������������������������������������������������������������������������������������������������������409 x

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.