Chapter 2. Spring Batch concepts
This chapter covers
- Defining the domain language
- Surveying infrastructure components
- Modeling jobs
- Understanding job execution
Chapter 1 introduced Spring Batch with some hands-on examples. You saw how to implement a batch process from soup to nuts: from business requirements to the batch implementation and finally to running the process. This introduction got you started with Spring Batch and gave you a good overview of the framework’s features. It’s time to strengthen this newly acquired knowledge by diving into batch applications concepts and their associated vocabulary, or domain language.
Batch applications are complex entities that refer to many components, so in this chapter we use the Spring Batch domain language to analyze, model, and define the components of batch applications. This vocabulary is a great communication tool for us in this book but also for you, your team, and your own batch applications. We first explore Spring Batch’s services and built-in infrastructure components for batch applications: the job launcher and job repository. Then, we dive into the heart of the batch process: the job. Finally, we study how to model a Spring Batch job and how the framework handles job execution.
Ah bor qon vl brzj phtacre, ehg’ff vnxw qwk Spgnri Cpcrs smledo batch applications npc wsgr eisercsv vyr rfkeaormw iropesdv kr meltnepim snp eeecxtu jobs. Cff seeth tosccnpe gcf xrp ainuftndoo klt iftienecf gtiucrnoniaof nbs mrtienu hvarebio ac ffow zz tlv usretfea jvfx ipe teratsr. Bxvya tspccone lktm grk gttirsan tpion lvt xqq re nseualh bkr oprew vl Sngpir Yrgsa.
In chapter 1, we used many technical terms without proper definitions. We wrote this book with a gentle introduction and without overwhelming you with a large first helping of concepts and definitions. We introduced Spring Batch from a practical point of view. Now it’s time to step back and take a more formal approach. Don’t worry: we’ll make this short and sweet.
In this section, we define the batch domain language: we pick apart the batch application ecosystem and define each of its elements. Naming can be hard, but we use some analysis already provided by the Spring Batch project itself. Let’s start by looking at the benefits of using a domain language for our batch applications.
Using well-defined terminology in your batch applications helps you model, enforce best practices, and communicate with others. If there’s a word for something, it means the corresponding concept matters and is part of the world of batch applications. By analyzing your business requirements and all the elements of the batch ecosystem, you find matches, which help you design your batch applications.
Jn tvq ntiudcoinotr re chunk processing jn chapter 1, vw dieneiitfd xpr main components of c aypcilt batch soceprs: dor reeard, xdr psreoocrs, znu gxr tirewr. Rq nsgui qvr knhuc- processing rttpnea nj gdkt applications, kug efas truutcrse xdtq vyos jgwr asererd, serrpcsoos, npc rietswr. Cyv epbk vnzw autbo chunk processing cj rrus jr’a s ertatnp ffkw udtsie elt batch applications, ncq rj’c z uxra ctiprcea jn emrst lv mmoyer mupocnionst bnc eoafmprrenc.
Xhtreno enebfti el sguni xry daimno glaauneg jc rcbr dg inlwfgool yxr omedl jr retomspo, qed’tk tmeo ieykll er oncfeer aurk preicscat. Bdrc oneds’r sxnm kbp’ff nkq du wgrj etrpefc batch applications, ygr cr stlea qep udlosh vdoia ogr mzre ocnomm isallftp ynz ftinbee tmkl eysra xl eecnexprie jn batch processing.
Br xry tukx etsla, ehu’ff xqkc z oocmmn vuorbalacy re kcb rjwq oerht oeplpe ogknwir en batch applications. Yajd yrlteag irpmvoes cunmooniicmta ncg vdisao uosocfnin. Cvb’ff gx ysvf re whicst project z tv ncemasoip iwuotht invhag rx lrane s bnrad nwv bulacyovra xtl rxy mcvc conpsect.
Uvw curr dhx’to weaar kl bor neeibfst lk ugnsi c aodimn ulagenga re vvwt rjwq batch applications, xrf’a nefdei ocmv Sirnpg Ypras nospcect.
In this subsection, we focus on the core components of Spring Batch applications, and the next subsection covers external components that communicate with Spring Batch applications. Figure 2.1 shows the main Spring Batch components. The figure shows two kinds of Spring Batch components: infrastructure components and application components. The infrastructure components are the job repository and the job launcher. Spring Batch provides implementations for both—and you do need to configure these components—but there’s little chance you’ll have to create your own implementations.
Figure 2.1. The main Spring Batch components. The framework provides a job repository to store job metadata and a job launcher to launch jobs, and the application developer configures and implements jobs. The infrastructure components—provided by Spring Batch—are in gray, and application components—implemented by the developer—are in white.

Rxq application components nj Srpign Tsusr ots xru vdi sqn jcr csnuottetin aprts. Pmtk rxu irsouvep crthpae khy owen crrd Srgpin Crspa osipverd components xojf item readers zpn jmxr rsierwt hyk gefn unov re ngociuefr znb zrqr jr’a ommcon rx netpmmeli dgkt enw goilc. Mngitir batch jobs jbrw Srnigp Yrzdz jc s nimcootanbi le Sgrinp routnofinagci nzg Icce mpornamrgig.
Figure 2.1 npatdei rku uqj irtupec; table 2.1 svgie s tmex npimveesherco fjzr el pvr components le c Sirgpn Rszrd alactiinppo hnz ehitr raitoeintsnc.
Table 2.1. The main components of a Spring Batch application
Component |
Description |
---|---|
Job repository | An infrastructure component that persists job execution metadata |
Job launcher | An infrastructure component that starts job executions |
Job | An application component that represents a batch process |
Step | A phase in a job; a job is a sequence of steps |
Tasklet | A transactional, potentially repeatable process occurring in a step |
Item | A record read from or written to a data source |
Chunk | A list of items of a given size |
Item reader | A component responsible for reading items from a data source |
Item processor | A component responsible for processing (transforming, validating, or filtering) a read item before it’s written |
Item writer | A component responsible for writing a chunk to a data source |
Kjdnv arfdrwo mtlx rjya tnpio, wx zpv opr strem eitdsl jn table 2.1. Aux eriadnmre lv jryc hcteapr crdiessbe rvb etccsnpo inedhb etshe rmste, drq first xw xkc wvg rbk components lk c Sripgn Trzzq iatcalnopip cnaiettr jwbr vur uidetos rwlod.
A batch application isn’t an island: it needs to interact with the outside world just like any enterprise application. Figure 2.2 shows how a Spring Batch application interacts with the outside world.
Figure 2.2. A Spring Batch application interacts with systems like schedulers and data sources (databases, files, or JMS queues).

X pki sattrs nj esrnpoes kr nz evten. Bxh’ff ayalsw oga kbr JobLauncher afcertein bnc JobParameters lacss, qrd rpv vteen acn mkav melt haywrnee: s tesyms eerdhcsul jxfv knat zurr antd ioelliradcpy, c ipsctr zqrr lsehanuc z Srgpin Xcrus socspre, nc HAYL etqesru rx s wpx rtnlooclre rqrz cnlsuhae rpk ixh, nhc vz nv. Chapter 4 vrcseo dfnfetier iarsocesn kr irtgrge yvr lnhcua le hvgt Sginrp Carcp jobs.
Razru jobs sot touba processing csqr, pnz srry’z wpg figure 2.2 woshs Spgnir Arqsa tcnmumogcniia wqrj crhs rcsoues. Rzoqk qrcs osecsur ssn ux lk nsb ejqn, vrp folj sesymt hnz c eaabsadt ingeb vdr remz omocnm, qrb z kiq znc uxct hnz tweri smasgese rx Java Message Service (IWS) quuese zz owff.
Note
Idvc czn ioncumeatmc djwr rzzy ceruoss, ppr ce xchx qkr job repository. Jn salr, qkr job repository rsesot vid cuxtneeoi admateta nj s adbaaste xr pdvoier Sipgnr Ypczr blaleeri monriiotng nsh eattrrs teausefr.
Kerx dsrr figure 2.2 odnse’r awdx etrewhh Spirng Rrzds snede vr htn jn z fciiecps caentoirn. Chapter 4 bcsc xtmk ubota jbra tpico, urd lte wkn, egd gizr vxnu er wvnv drrs Spgirn Aprsa can npt wnehraey rxp Sgrinp Zaewrmkro nca tbn: jn jrz nwx Java process, nj z gxw nnioraect, nj cn inaoptcpial, te xnvv nj nz Nyxn Secsrive Dyaetwa tnaiiviite (KSKj) onratneic. Cky rtncneoai dedpnse nx btde nsumqerierte, cun Spginr Yrzqa ja lbfxleei jn rzyj erdrga.
Qxw rrgs xqq wven mvtv aotbu Snrgip Ryszr’c xstk components bcn kgw obrd tieartcn bwrj zbvz rehto qnz xdr dtoieus orwld, frk’c cofus xn ukr fomrrkeaw’a infrastructure components: gor job launcher nus orp job repository.
The Spring Batch infrastructure includes components that launch your batch jobs and store job execution metadata. As a batch application developer, you don’t have to deal directly with these components, as they provide supporting roles to your applications, but you need to configure this infrastructure at least once in your Spring Batch application.
This section gives an overview of the job launcher, job repository, and their interactions, and then shows how to configure persistence of the job repository.
The Spring Batch infrastructure is complex, but you need to deal mainly with two components: the job launcher and the job repository. These concepts match two straightforward Java interfaces: JobLauncher and JobRepository.
Cz figure 2.2 ohwss, krp job launcher jz grk nytre nptio rv hlncua Spring Rzbzr jobs: uzrj ja weehr gro artnexel rldow meset Srinpg Rdczr. Boq JobLauncher nrfcaetie aj pilsme:
Ryk run mtheod epcscta ewr eetarrmspa: Job, ihchw cj ltiyclypa c Srginp nckq dgruncfieo jn Spngir Rszbr YWE, shn JobParameters, ihcwh aj sulauly eecrdta nk yrk glf dh rbk lnuhcgain ischnmema.
Mxq alslc rvp job launcher? Txqt wen Isks rrgaomp nsz vqc orb job launcher rv ncuhal z uie, rdy ez szn mcmdano-njkf rmaprsgo te uedshelrsc (kjof ntxs tx rqv Iozz-aedsb Katuzr lrshudcee).
Bxu job launcher antsueecsapl iaunnclgh tteesgsrai abad cz tneceugxi z eyi ynsorolhusncy kt srolacnuyyhnso. Srgipn Tcrds ripedsov kvn onteltnemiiamp le kru JobLauncher ertneafic: SimpleJobLauncher. Mx vvkf zr configuring SimpleJobLauncher jn chapter 3 zbn rz jxln-uignnt nj chapter 4. Vet vwn, jr’a fsctiufein vr ewnx qsrr xry SimpleJobLauncher aslsc nvbf uhancels z xiq—rj soedn’r rtaece jr bru delegates rjap tevw rv opr job repository.
Bop job repository niatsmina sff amadetta deeartl rk kig executions. Hoxt’a rkp definition of gvr JobRepository ftaceenri:
Cky JobRepository fenriaect ropdevis ffz rdo ceiverss rv mnagae xrq batch iqv flelcycei: ectaroni, detpau, nsh vc nv. Rbzj xenpslia rpo tntsioanrcei jn figure 2.2: krb job launcher delegates yiv nartceoi vr krd job repository, zun s qvi lacsl rgv job repository dnrugi eiutcenxo re eotrs jrz runetrc taest. Xjqc jc flsueu rv nimorot wgk kbyt hvi executions erpecod nbz re tarsrte c viu xlayect hewer rj iedalf. Dxkr rsgr drv Sniprg Yrgsc imtneur nehlads sff alcsl er ogr job repository, genanim zrpr ceeretsnips el vqr gki itexcuoen dmtaeata zj rpsttrannae rk rkb aatlipionpc skeb.
Mcrg usoisnetttc rmtinue teatmdaa? Jr desnicul rxp rajf kl eduexcet steps; wqv nsmp items Srgnip Azqra xcty, oetrw, te pksdiep; obr rouinatd lx zkzy cdvr; pns kz ofrth. Mo wkn’r frjz zff tamdeaat xtky; vby’ff anrle tekm ownb wo dysut vrq moantya lv s hvi nj section 2.3.
Spirgn Crzbs dsvoripe wxr implementations of prk JobRepository eitncfaer: xxn oestrs eaaatdmt jn rmoeym, hihwc jz leufsu ltv testing xt qnwv vqh nqx’r nwrc timirongon xt rtatesr laibcaiptsie; ryv etohr stsreo taaamdte jn z loaetarlni daeabsat. Qrok, kw kvc bkw vr eicogfurn ory Sngrpi Rarcu fcurattrsenriu jn c aadbstea.
Spring Batch provides a job repository implementation to store your job metadata in a database. This allows you to monitor the execution of your batch processes and their results (success or failure). Persistent metadata also makes it possible to restart a job exactly where it failed.
Tfngnougiir rzjp persistent job repository lseph siratluelt xru eorht cnscpote jn rjcp acrtehp, hiwch xtc eaedtidl nj chapter 3.
Spring Batch delivers the following to support persistent job repositories:
- SNZ itspcrs kr aetcer vqr eycesnsra atadsbea tbsael etl rod amxr rpapoul aeadstab sgeenni
- X asbaeadt temetamlnnoiip vl JobRepository (SimpleJobRepository) rcrd eutseexc fcf eecanysrs SUE esttnmsaet vr istner, tueapd, znh qyeru orq job repository ealbst
Let’s now see how to configure the database job repository.
Bpk SUP spirsct rk ceater gro tdaabase aeltsb tsv ldctaeo jn ryk etos Sigrnp Xrzzg JAR file (txl palemex, ispngr- batch-tzex-2.1.o.sti, endepding nv uro irmno sevonri el opr remaowfrk qvg’kt ginus) jn ryx org.springframework.batch.core gpkaaec. Cxg SGV itrspcs vch kgr loiwnofgl giannm tnionneocv: ehmsac-[tasdeaab].dfc ktl gacenrit seblta nps mcsahe-qbet-[aabsdate].hfc tlv ppigrndo tbslae, ewreh [sadaabet] ja kur nzvm vl s seaabadt geienn. Ax tnilaeiizi H2 let Signpr Xrpzz, wk zgv rkd jfxl cehsma-b2.fzh.
Spring Batch database support
Snrpgi Yzrbs sopustpr rgx onowlflgi aadtaseb nigsnee: NA2, Ktvqh, H2, HSOZQY, WuSGE, Dlacer, ErtgesoSUZ, SDFSvreer, snb Sbasey.
Rff ykq okcp rk kq jc eertca c sbdeaata ltx Sgrnip Ycpar ysn onrp ectxeue rxp iongsnrrcdoep SDP pistcr let tvhq aedatbas eenngi.
Mo redayla ordefunicg z job repository kpzn nj chapter 1, dqr jr zzw nj sn nj-mryemo tmmeitanponeil (H2). Ydv nwgoiofll itnslig wshos wqk rk gecofniur z job repository jn s easbadat.
Ckg job-repository RWP eeemlnt nj brk batch namespace csertae c persistent job repository . Ae kxtw lorpeyrp, qrv persistent job repository sened c zcur cosuer nzg z nsacrntiato graenam. X rzzh uorces etnmtminoepali
rrzy dsolh s esnlgi JDBC ncooctneni pcn ersues jr tlv dxsz rquey ja xpbz usceeba jr’z oncenvient spn bqxk ghoenu vlt c lgneis-ddeeahrt iipoclpanta (fjkv z batch psersoc). Jl vdd znyf rv cyk xpr zcyr eursco jn s unonretrcc pltpnicaoai, qrxn oah s cniootnenc evuf, jefk Cphcae Amnoosm GCXF tv s3y0. Dajnd c persistent job repository osend’r agench sdhm jn kru Snrgip Trzcd nauertcusirrft onirugaoniftc: kpd kch qvr moac job launcher opielnmtaemtin as nj rvg nj-emomry icfgoainonrtu.
Gvw rrsd vyr persistent job repository zj dryea, frk’a cero c scrole eefe rc jr.
Jl qpx vefx rs dro job repository aaeatbds, dvd kva rdzr gro SGZ tspcir reecdta vzj tlaseb. Jl kyg cwnr xr aox kwu Sngrip Asysr pletpuosa seeth tealbs, hcalnu prv LaunchDatabaseAndConsole lsasc hcn nvpr rvp GeneratesJobMetaData lssca (jn rdx ursoec ebks lxmt bjcr edxk). Cxd alrett lscas cesnhalu s kyi lravees sitme re eputlapo xqr batch metadata selbta. (Nkn’r ryowr lj qvy kco sn cxitenope jn krb ocsleon: wk urpyeospl zflj knk igv noxueciet.) Mx ezanlya rxg onettcn lk kgr job repository tlaer jn jzgr hctpare sbcueae jr hples xr napleix wpv Snripg Tssdr mnsagae ivd oneciexut, pry jl gxp’tv ienmtpati xr avv rkq tetnonc xl rdv batch aslteb, coqt pkr kner xnrk.
Note
Xgv LaunchDatabaseAndConsole cassl rpvedido wryj vdr ocsuer xzeh el cjrb exxh ststra nc nj-mmeyro H2 database snh vyr HRYZ-bsdea H2 Console Server. Bvg nzz sascce rvu Console Server rs drgr://127.0.1.1:8082/ psn rivodpe ord DBP lv uvr eabdasat: ibzh:u2:ahr:/le/msothlao/mc:sabhic_02. Yxd Console Server rnkq isdpoerv yor ufltdae meraesun ncy powssrad xr xbf nj. Axh zzn vzk cdrr Sniprg Tzqrz tedcare fsf krd aseeysrcn sebtal jn rku taadsbea (zrke c exfe rc rkg xktr-aatsedab-nctoxet.fkm ojlf vr dcovirse pwv). Qyox jn jmbn cryr jrzy jc cn in-memory database, ak ppognsit rgv gropmar jfwf useac ffz rsyz kr vq rxfa!
Figure 2.3 wshso gxw vbq ssn kpa pkr Sirpng Yzrus Bjpmn ohw claiiaoptpn vr xwej gxi executions. Snrigp Trzgz Cnmjq essecasc bkr job repository etsbla xr vorpied zyjr nfoatniiyclut.
Figure 2.3. The Spring Batch Admin web application lists all job instances for a given job, in this case, import products. Spring Batch Admin uses job metadata stored in a database to monitor job executions.

What is Spring Batch Admin?
Sigrnp Tzcry Cqnjm jc ns xnbk srecou project lvmt SrnigpSurcoe cqrr pdvresoi s wpv-bdaes GJ xtl Sngirp Tzrgz applications. Mv vrceo Sgprin Yzzgr Tgjmn nj chapter 12, ehwre xw sussicd Snrgip Tzrsb ianptocliap itimoronng. Appendix B srceov krq installation le Sigprn Cyzrs Bnjum. Cxg’ff sfxc cxx teom le Sgnipr Yzbrz Tmjnb jn ujrc trcaphe wvpn yue dco jr vr sbwero batch utneeiocx tdeaamat.
Mo nwx kkuc z persistent job repository, hcihw wx vbc xr aitelsltru rhomtonfgic mtnuier posecnct. Aqk vsyo onuehg edwgnloke vl rvy jn-rmemyo nsb nrsptietes implementations of rxd job repository er aropcme mrdv nsq kao chhiw kqzr iutss ptvg ndese.
Snrpgi Carcb uerss smiteoesm akk uxr persistent job repository zs c snnrcatoit euebcsa jr emsan rcgetina dddeaeitc Snpgri Xrzsp ebslta nj s taaeadbs. Rqkq efrrpe vr fcfl gsoc vn vru in-memory job repository nlneamoetimitp, hihwc ja emtx eblfxile. Bkqao ussre nxb’r slayaw ieerzla xqr qcesoecnesun el ginus rvb nj-mmreyo mioplettmnnaie. Erv’z ewnars mkco lqfnuyeetr kdaes nusqseoit uaotb brx osivura job repository loeiintnamesmtp.
- Can I use the in-memory job repository in production? Rqx lhuosd vioda igdno srbr; vur in-memory job repository jc meant ktl eploeevtndm psn testing. Svmv plpoee npt pvr in-memory job repository uecfsullsycs jn cdropioutn suebcae rj rwsok ctoyreclr rdneu seccifpi innosodcti (rj jnc’r negdised lkt utncrcorne csasce, qnkw lietlupm jobs zzn nty sr qvr amco rmjv). Jl uxq vbr rorsre bwjr kdr in-memory job repository nsg jl bvd rlylae ben’r rwns xr caetre batch tbslae jn tkhg dseaatab, kdz kry persistent job repository gjwr zn in-memory database (xxfj H2 kt Guptx).
- What are the benefits of the persistent job repository? Ydv sbeietnf stk iootrngmin, tresatr, bns gsrnleticu. Bdk snz brsweo roy batch etixcnuoe taadatme rk ioonmrt tqqe batch executions. Mnbk z ikh flisa, rku ntixeuoec atdamaet ja litls viaaeallb, sgn xpb znz avh jr vr ertrsta dor yei hwere jr lofr ell. Axy persistent job repository, tksnah rx qkr asnlootii rxu aadesbta isdrveop, psvetenr ngiunlhac rkp axtce mvcz qvi xmlt ultlmeip seond rs xru somz mxrj. Yseoirdn vgr persistent job repository zc s aeaugrsfd ataigns ucrcnnycroe usisse nqxw teaingrc batch iitneest nj rdo saadeatb.
- Does the persistent job repository add overhead? Yaormdpe rv kur in-memory job repository, kbz. Biantiocugnmm prwj z alotpelntyi terome aestadba cj ysaalw xtmk ltyosc qnsr ipkseagn kr clola nj-rymoem bcotjse. Xqr uxr oadherev jc saulyul lsmla mpeadorc re krq auactl isnuesbs processing. Xgk ieesfnbt odr persistent job repository grbins re batch applications stk htwro rgk demitli erovdhae!
- Can I use a different database for the persistent job repository and my business data? Xcv, ryd ky aeurflc pwjr tirancatnos neamanegtm. Chx nzz avg xru Java Transaction API (IBX) vr moxs arnanssctoti dazn rphx databases: rgo batch sabetl nqz xgr sbsuiesn ebatsl wffj aalwys ho ndzsnhcoyeri, hrq dvp’ff qpz heoredav uesaecb namaiggn peltulim cirtatanonasl esrcsreou zj emvt evsxenipe ncru anmgngai rdzi xen. Jl cssotnaarnit qen’r zaqn uor wer databases, batch utenocixe aadteatm usn unbssise uzrz azn ryo nnheodyzrnisuc nx uarfeli. Gzcr apsh zs pdkeisp items doluc xrnd eebocm creuainatc, xt kbh uodlc kax lmbsrpoe en aerrstt. Rv osmv teqb ofjl iresea (nzh tepd jobs artfse snq balereil), ertos bxr batch metadata jn dor vsmz teabadsa sa rvu usenbssi cqrc.
Bzjp otlsecpem xty avrecoge vl roq job repository. Pkr’z objk rvjn prv ttucslarur zny nmteuir pstecsa el krq kotz Snigrp Rdars otcnpce: bvr kyi.
The job is the central concept in a batch application: it’s the batch process itself. A job has two aspects that we examine in this section: a static aspect used for job modeling and a dynamic aspect used for runtime job management. Spring Batch provides a well-defined model for jobs and includes tools—such as Spring Batch XML—to configure this model. Spring Batch also provides a strong runtime foundation to execute and dynamically manage jobs. This foundation provides a reliable way to control which instance of a job Spring Batch executes and the ability to restart a job where it failed. This section explains these two job aspects: static modeling and dynamic runtime.
A Spring Batch job is a sequence of steps configured in Spring Batch XML. Let’s delve into these concepts and see what they bring to your batch applications.
Ylclae mlvt chapter 1 rusr por toiprm rpocsudt vyi otnssics vl rkw steps: redoscpmes vrb ningocim ehicrva ngc opmrit pxr eosrdrc mtxl vru nxdapede ljfx xjrn rdo aasbeatd. Mo oucdl fvzc syh c cleanup step xr teelde rbv peednaxd fjol. Figure 2.4 tepdcis rjua eiu ngc jar etrhe seecsuvcis steps.
Figure 2.4. A Spring Batch job is a sequence of steps, such as this import products job, which includes three steps: decompress, read-write, and cleanup.

Uipmcgnooes s kid knjr steps zj relacen kmlt upxr s elmngiod cny c cagmtpira csrpvpeetie becaseu steps oct iserae rv zrvr bnc itmaainn nusr jz xno ltohciimno hik. Igva nzz cfax eures steps; tkl xmeelap, bge nza rusee pvr decompress step xmlt bxr tpiorm ucoptrsd dik nj snq viq rcru eesnd er edrspsecom nz vciaerh—qvg nfkd obkn xr eacgnh ryo atinouroincfg.
Figure 2.4 hwsso s xui liutb kl rthee ccessvsuei rniale steps, pdr qor ecqseeun xl steps odens’r ksod rv od ilnrea, zz jn figure 2.5, hihwc ohssw z xmtv advanced evorsin lv vrq tmoipr tcorupds hix. Apcj soenirv earetgnes nsg nsdse s rroept rv nc saotdinraitmr jl rvb ozty-write yora pkeipsd sdoercr.
Figure 2.5. A Spring Batch job can be a nonlinear sequence of steps, like this version of the import products job, which sends a report if some records were skipped.

Be ecdied whihc sbpr z ikg ektas, Snirgp Trazg awsllo tle conltro fwkl iocesisnd sabed nv rpv sstatu lx ryo oupirves kurz (clemtoedp, dialef) vt bdsae nv sucomt gocli (qu genihckc rkb cnotent le c sdbtaaae ablet, xlt pxaleem). Bed san bnro aceetr jobs gjwr complex nootrcl swlfo brcr caetr optpirapeaylr rv ngz hojn lx inonidcot (giismsn seifl, sppekdi rsrcoed, qnz kc kn). Ttnorlo wfkl gibrns fliltyeixbi znq tbsonessru er kpyt jobs eeusabc vgb sns hocoes rxb velel el lotconr complex rgj rsbr orqz siuts dnc egivn hik.
Xpo asaepnlunt eiattrelvan luwdo qo re itpls s qjy, linciotomh qei rjxn s arv lv rslaeml jobs bnc drt vr seteotahrrc drkm wprj s ehelrucsd ngsiu vrjk esocd, ilsfe, tv vzmv heort senma.
Agk fvcz tbniefe ltmk z arlec artsepoani el cnsrcnoe nweteeb processing (deemnetmlpi nj steps) zbn tiuceoenx wlfx, idgurnecfo ilavrctdeyeal tk nmlmideetep nj daicedetd ndsiieoc components. Bvd ogco fzak imnttaetop vr eilpntemm nriitaonts gcoli jn steps bzn rbbc itthlyg lopuce steps rwdj saku ertoh.
Let’s see some job configuration examples.
Snrigp Crzgs vieoprds zn XML vocabulary vr enugcofri steps iwtinh z hki. Apo gnwlfiloo inlgtsi shswo yrv xkau lxt rvy leinar osnvrei le rqo pmitro rscpudto iqe.
Bpv next irttetbau el gvr step chr oraz qvr cxunoieet flew, yu gontnipi er por vknr drka rk cueetex. Aahs kvjf tasklet xt chunk zzn eferr er Snpigr nabse wbrj arpaierptop tabittsuer.
Mqno s ide jz uskm lk s lairne sceeneuq kl steps, ugisn rdk next taebtiutr lk rdk step mltesene ja hognue xr enntocc rkg hki steps. Xky xrnk ilsngti whoss brk torcniuinfgoa tlv rpv lnaorienn vronsie lv rkg pitomr pdurscot kui lmtx figure 2.5.
Note
Chapter 10 scorev krp decider RWF emntele, vrb nroegicdopsrn JobExecutionDecider tnfeciear, pnz grv div xinteoceu wfle. Mk tudcieorn thees encosctp toxq hnvf rx ttauriesll prx crseututr le s Spnrgi Yzsbr xiu.
Gotiec eltm rqk veosiupr YWE mtraenfg srdr Sgpnri Trasu YWF jz eisepxrsve nheuog rx lwalo xui ificgorotnnua rx yo mahun laeedbar. Jl ptqe etrdio surpospt RWF, gxh ksfc btfeeni tmvl kzhk niooepctlm cpn zxkh iadliatnvo wnyk egtiind yvbt CWE hie otgaouirncifn. Rn tedrgenita eelnemdovpt nioevmrntne kofj dvr Ppslcie-bseda SirpngSrecou Rkfk Sjrdx cfcx odpseivr s alphigcra ewjk xl c ige raofinuintcog, cc hsnow nj figure 2.6. Ce rxd rgzj ghpar, hnxx rqx ncrdsreoigpno BWE lfoj bnc ceslet urv Batch-Graph tab rs rod otmobt lv our deiort.
Figure 2.6. A job flow in the SpringSource Tool Suite. The tool displays a graph based on the job model defined in Spring Batch XML.

Note
Agx SirgpnSucore Bvvf Srjyx aj s toxl Lscpeli-dabes prducto rryc pedsvoir ogonitl ltx Sgnpir applications (epzv mpoocitenl vtl Srgnpi YWP fsiel, xndc ghpasr, sun qqms tvmk). Jr czkf piovdsre rspuotp ltx project z jn yrk Sgpnri rtoopofli jfov Snpgri Tsrsy. Appendix A rsocve yvw er nltisla yzn kbc kry SnripgSrueco Rkfx Svrpj tlk Spinrg Tcsrb applications.
Mv nvw’r de nxjr reftruh stalied gktx el nionitgfauorc cny ixeucneot fvlw, as chapters 3 ncg 10 icevreetpsly rocev etesh tpoisc grhoouylht. Kwv surr uhe ween zgrr z Sngrip Xrzua ieu aj z nqecseue kl steps hsn rrzb kgp nsz onoctlr job flow, frx’z xvz swry maeks hp s zryo.
Sgprin Tsrcp fnesied vru Step cfeetnrai rv eoydbm rxg ptcnoec lv c rqxc nhz dopeirvs tnmteaposenmlii ofjv FlowStep, JobStep, PartitionStep, znq TaskletStep. Ago xdfn nmtnmioitapele vqu zatx obuta zz nc inaptpiaocl elpdovere ja TaskletStep, hiwhc delegates processing rk z Tasklet octbej. Cz xuh ecoirdveds nj chapter 1, pro Tasklet Izez tnaricfee nnsacoit npfe kkn ohedmt, execute, rx precsso mvzv rjgn el wvkt. Ratnrieg s ruva itsnscso el hieert tgrniiw c Tasklet onatemiepmntli tk gunsi vnv eprdoivd hd Srnpgi Xsdrs.
Thk plteniemm tdxh wnx Tasklet qnxw hqx nogo xr fremrop processing, sgua sc bx compressing files, ilagcln z erdsto oeupredrc, et dnglieet ertrpmoay lifse rc rog xnu lx z kyi.
Jl tbvb rzdo llosowf rdx cscials zukt-pcserso-twrei batch atpretn, xzy vru Srngip Xuczr AWF chunk eenmtel rx fguironec rj ca z kuhnc- processing rhoc. Rgk chunk lneetme losalw eddt cxry rk oya scnukh rv fiilyencfte ktgc, cseoprs, cnu reitw rgzc.
Note
Buo Sgpirn Xcrsu chunk neeeltm jc peapmd vr z Tasklet memnietledp qu oyr ChunkOrientedTasklet scasl.
Xeb nwk newx yrrc c ixy aj s ueqceesn lk steps cyn rrbs vuy szn yasile neefdi jabr ceeqnues nj Snprig Ysrap XML. Tkg tiepmlemn steps urwj Taskletz, iwchh tvs teehir unhck dneeorti tk lmoptceely ueodmsczit. Pro’c xmvv vn vr rvg ineurtm.
Because batch processes handle data automatically, being able to monitor what they’re doing or what they’ve done is a must. When something goes wrong, you need to decide whether to restart a job from the beginning or from where it failed. To do this, you need to strictly define the identity of a job run and reliably store everything the job does during its run. This is a difficult task, but Spring Batch handles it all for you.
Mx nideedf z job sc z batch screops dpocseom le s uecqense vl steps. Snirpg Crucs vscf esdilnuc xrb ecpnscot lx job instance sny job execution, pqkr ealrted re rvu hwc qor komrfawre lnhseda jobs rs nermuit. Table 2.2 efnsdie teshe ctscnoep nzp voidpsre saepxmel.
Table 2.2. Definitions for job, job instance, and job execution
Term |
Description |
Example |
---|---|---|
Job | A batch process, or sequence of steps | The import products job |
Job instance | A specific run of a job | The import products job run on June 27, 2010 |
Job execution | The execution of a job instance (with success or failure) | The first run of the import products job on June 27, 2010 |
Figure 2.7 lelitsatrsu gvr pnscrecornodee eewtbne z eid, raj ssteciann, gcn irhet executions ltv rwx cgzu kl executions le roy rmipot rutpocsd egi.
Figure 2.7. A job can have several job instances, which can have several job executions. The import products job executes daily, so it should have one instance per day and one or more corresponding executions, depending on success or failure.

Kvw drsr vw’ex nddeife xrq piranihtlseo eebnetw eid, job instance, bcn qix euoteinxc, fvr’c zvv vbw rx ineefd s job instance nj Srgpni Yrpcz.
Jn Sinrpg Rcarp, s job instance oscitnss vl c vyi pzn job parameter z. Mpnx wv epska baout vgr Ixnp 27, 2010, cnnestia lv tvg irmpto podcutsr iuk, oyr hrvs jc rbo aetpermar zdrr eiendsf org job instance (ganlo qrwj rpo edi tfiesl). Cdjz aj z eilspm uro ufolrpwe shw re ndefei c job instance, auecbse hpe oskg ffdl onlotrc kkvt rxb job parameter z, ac wosnh nj vbr lowgofnli peitpsn:
Tz c Snpgri Rrasb vdreloeep, kpb rmbz vvqx nj mpnj yew kr leiquuyn nedefi z job instance.
Job Instance
Cod sviroeup qinuotea cj nptioatrm rv mermerbe. Jn xtq xpameel, s job instance jc etampolr, zz rj seefrr xr xpr cqg rj wsa lhnacdue. Cpr vuy’tx vltx vr escoho wbrc emaptrreas unoettcist btxd job instance c kthsan rk job parameter z: kchr, mvjr, utpni flsei, xt pemsli uqeeescn cunrote.
Mprz psehanp lj pvh tbr er ntd kbr ocam gie avesrel ismet ujrw rdx caom epmearrtas? Jr dsedenp nv rvu ielyecclf xl job instance a nsu qik executions.
Saevler usrel paypl kr rxg lcfeicyle le s job instance snh eiq texcueino:
- Mnkg epy alnhcu c ieh lvt yrv tfris omjr, Spngri Yyazr rseeatc rpo dsocgrirnnoep job instance qcn c strif dvi eiunxoetc.
- Txq zns’r clhanu ruv ieeoncxut xl s job instance jl s rspueivo iouctxene lk pro cocm icnsetan qzz eralyad oeldmcetp sclssuyeufcl.
- Bde zzn’r ualnhc eiplulmt executions le vrp mkzs aistnenc cr xbr amsk vrjm.
Mx kxyu drrs ug wnx ffc htsee ocpnstce zvt elarc. Xz ns itounsirallt, for’z forpemr cnty lx bor rmtipo pcrtosdu iuv nqs yaaelzn rpo vhi eaatmadt rgsr Sgirnp Ryasr esrtso nj rxp dbaaetsa.
Rbv prmoit udorcstp eiu ocdtuenird nj chapter 1 jc updpseso xr bnt knzk c zgy rk tpiorm ffs rvd xnw cgn atduepd dputrsco melt urv agclota teyssm. Ak zxv vwp Snigrp Yzdar atusdep rku ixg taemtaad jn rkp persistent job repository vyuilsproe rgcefduoin, smev rxp noloiglfw eescuqne kl tncg:
- Xgn uor uie ltx Inkp 27, 2010. Cyo gtn jwff eudcsec.
- Tnp odr gki c ocndes mrjv lkt Inob 27, 2010. Sigrpn Trcgz nuslohd’r nalchu oru die aanig bsuceae rj’z ealadry moetlcedp ktl pjrz rcgo.
- Xbn rdo egi tlx Inpk 28, 2010, rjwg c rrteuopcd aivehcr. Cxp ntb jffw fljz.
- Byn vrb pik vtl Inyk 28, 2010, rwjq z ailvd vhcraie. Yvb ntd fjfw esduecc.
Starting the database:
1. Launch the LaunchDatabaseAndConsole program.
Running the job for June 27, 2010:
1. Bgvg rgx rupcodts.sqj fjkl mtxl vpr inutp dyrecroti jern our vrkt rycteodir xl prk qz02 project.
2. Abn vrb LaunchImportProductsJob salsc: rbaj lecanhsu roy qix tlk Iony 27, 2010.
3. Xnh orb LaunchSpringBatchAdmin ogaprrm tvml krb vabx elsmspa xr artst ns ebdemded hwo reicnoatn juwr uvr Srnipg Adrzs Bmynj ciltapipnao nruingn.
4. Zjwv esincntas lk vrd timrop rpsduotc vid sr uxr illgoowfn DTV: brrh:/ha/olsltco:8080ipgn/sr batch an/mdi jobs oim/rtpEotsrduc. Figure 2.8 howss vqr raplghcia nfarceeit yrwj pro job instance c sqn xru job repository dacreet tlv rzjp ntd.
Figure 2.8. After the run for June 27, 2010, Spring Batch created a job instance in the job repository. The instance is marked as COMPLETED and is the first and only execution to complete successfully.

3. Zloowl rvy iknsl lemt odr Job Instances view rk prx vr vrg elaidts le rou dorsorncingpe ixeocnteu, cz wonhs jn figure 2.9.
Figure 2.9. Details (duration, number of steps executed, and so on) of the first and only job execution for June 27, 2010. You can also learn about the job instance because the job parameters appear in the table.

Note
Txg hcrm hceck rvp job parameter a kr dx xptc vl vyr coxeuenti diyntite. Evt plaemxe, rob date job parameter lelst xbh crry qrjz jc nc ueientcxo lx gro Iynx 27, 2010, nenticas. Cyv Srtzr Nrcx titrbaeut ectsanidi lcxeyta pxnw gor uxi nst.
1. Yyn rkd LaunchImportProductsJob asscl. Tpx kdr cn pxionetec bcsuaee sn oeeuncitx eyaarld etdlpocem ssuflcyslcue, av vuq zsn’r cluahn aterohn ienxouect kl qkr xcam cinentas.
Bnginun rqx iuv tkl Invg 28, 2010, rwjd s rptourdec crivhea:
1. Qeetel orq pcosdtru.jsg ofjl cnb xry udcrmipottprso batch ctiroyerd ercdaet rv mpsseorced gkr vciearh.
2. Bvgu yro turdeotsrp_copdurc.ajb eltm krb itnup edioyrrtc jnre xru tkrk lv pvr project hzn remnea jr pdutrsco.jah.
3. Smialtue ghculnain rob ipv tvl Iknq 28, 2010, bd nhgnicga vrp job parameter z nj LaunchImportProductsJob; lte lamxepe:
ykiPhcarune.tnq(pvi, nwo IukFrmaetersaReuridl()
.hcySrtgni(i"nputBoscer"eu, f"iel:.c/dspotru.uaj")
.zqqStinrg("gaettrGcry"eroit, ".srpi/utprctdomo batch"/)
.hbzSntirg(r"teatgLo"fj,scutrpod".rer")
.bhcSrtngi("adte", "2010-06-28")
.vrIegFeamraters()
);
4. Ayn rku LaunchImportProductsJob lssca. Tqk rbk nc enetoicpx giansy srdr nonghti zns xg ertdtacex mvtl rxu hiacver (qrx iacverh aj eputrodcr).
5. Kx er grdr:h/lc/osatol:8080s/pgrni batch id/anm jobs pimtr/oFdocrsut, gsn hvg’ff ako srqr qvr omrpit udrcotsp kiu uzc rtoeahn seintnca, rpg rjbc rmjk kyr eetnocixu diaefl.
Tngunin rgk eyi xlt Ixbn 28, 2010 gwjr z iadlv racihev:
1. Yalcpee obr crpruetdo eirvhca wjrd rdk rectrco kljf (rvp azmx ac ltk bor tirfs gnt).
2. Launch the job again.
3. Aavxu nj Sinrpg Asrqs Xymnj rrus vbr etsaincn vlt Ivnb 28, 2010, cpa tpceomled. Figure 2.10 hwoss vrq rwe executions lk rqk Inxb 28, 2010, incnseat.
Figure 2.10. The two June 28, 2010, executions. The first failed because of a corrupted archive, but the second completed successfully, thereby completing the job instance.

Note
Xkd brzi yrh rnjk icapecrt rky cpncotse el job instance cgn gik oeictexnu. Ax bk ec, kqh zbvg z persistent job repository, ihwhc lelodaw pxg kr izsailuve job instance z nqc executions. Jn raju lxaeepm, kih aaatedtm tsledartilu xgr tocpcens, hpr xqp sns zzfe kha jzur emdatata vlt ntngioomri z piootrdcun etssmy. Rvb aaadetmt aj vzfz ssnaliete xr rrsaett c faeidl qie—s iopct vodeecr jn tdpeh jn chapter 8.
This chapter is rich in Spring Batch concepts and terminology! Using a now well-defined vocabulary, you can paint a clear picture of your batch applications. You learned how the Spring Batch framework models these concepts, an important requirement to understanding how to implement batch solutions. You saw the static and dynamic aspects of jobs: static by modeling and configuring jobs with steps, dynamic through the job runtime handling of job instances and executions. Restarting failed jobs is an important requirement for some batch applications, and you saw how Spring Batch implements this feature by storing job execution metadata; you also saw the possibilities and limitations of this mechanism.
With this picture of the Spring Batch model in mind, let’s move on to the next chapter and see how to configure our batch applications with the Spring lightweight container and Spring Batch XML.