Spring Batch fundamental concepts

Spring Batch fundamental concepts

Every Spring Batch developer should be aware of the Spring Batch fundamental concepts such as Job & Step, Chunk oriented processing, launching of a job, jobContext, step context, job repository and job metadata.

SpringBatch

  • Job and step

    • Each job typically has one or more steps, where we process certain data, for example upload data from a flat file into a database and iterate over that data perform some business action at the end.
Spring Batch fundamental concepts

Spring Batch fundamental concepts

  • Chunk Oriented processing

    • Spring batch for performance activity groups more than one activity into one transaction. For example if we go on and commit on every single row then it would be very slow. So Spring Batch allows some level of chunking.
      • Mean read the 100 rows from the table then process it and them commit them at once as a Chunk. Spring batch provide first class support for doing this and is one of the prominent feature to gain the performance.
Spring Batch fundamental concepts

Spring Batch fundamental concepts

 

  • Launching a Job – JobLauncher

    • Spring Batch has a concept of a Job Launcher
    • Launcher takes the definition of the Job which is spring application context and the job invokes some business logic to perform specific tasks.
Spring Batch fundamental concepts

Spring Batch fundamental concepts

 

  • Example of a Spring Batch Job

    • Simple File load
      • We start with a flat file from another system and we read that data in and process it, where some transformation is done and write the result into database. Like a simple ETL job.
Spring Batch fundamental concepts

Spring Batch fundamental concepts

 

  • Complex Use cases

    • Spring batch provides number of inbuilt off the self ItemReaders and ItemWriters, in which case we just need to do the configuration and need not write any java code
      • Example – FlatFileItemReader, JDBCPagingItemWriter
    • More complex job requires custom item reader and writers.
    • Use ItemProcessors used to delegate to some existing business logic to be processed for each item.
      • Some time we can do it in processor, but it is always better if we can move it to ItemWriter, where we can process on a chunk for more efficiency.

 

  • Job and Step in Context

    • To maintain the state, spring Batch provides context – JobContext and StepContext
    • This is the big picture
      • Job has got one or more steps
      • Job with a set of job parameters, make a instance of a job called as JobInstance
      • Job has a JobExecution and StepExecution
      • Lighter color boxes denote the State maintained about the batchJob while processing or for later evaluation

 

 

Spring Batch fundamental concepts

Spring Batch fundamental concepts

Spring Batch fundamental concepts

Spring Batch fundamental concepts

  • JobRepository and Job Metadata

    • The state information is store in a JobRepository. Typically it is going to be the database and the same database where the application data resides in.
    • For Example
      • If we have to use different database, then we have to use XATransactions
    • JobRepository has number of tables that come along with SQL scripts to create those DDLs, tables and load this in same database instance as your application data.

JobRepository

  • Execution Context

    • As mentioned earlier we have job execution and step execution
    • ExecutionContext object is where we use to maintain the state of the job so that we can restart if necessary
    • JobRepository metadata is used to determine the step at which the failure occurred
    • This support / feature are there in mainframe; hence all the batchjobs are still on mainframe.
      • To store the row id (100) in the database that failed and recover from there
    • Application code in the reader and writer need to maintain the state within a step. Eg. Current chunk
    • SpringBatch can supply that data during restart to facilitate repositioning.

 

  • BatchJob Common Idioms

    • Repeat
    • Retry
    • Skip

——————————————————————————–

You may also like this

——————————————————————————–

About TechnoStixs.com

TechnoStixs

TechnoStixs.com is a dedicated weblog for Java/J2EE and web developers. We take pride in our work. Every publication is carefully analyzed, written and tested to ease the understanding of the topic or subject.

We cover Java Core Technologies, eCommerce, J2EE Frameworks, Web Service, Build Tools, Unit Test Frameworks and Others.

1,593 total views, 1 views today

Add comment