Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Does Spring Batch Step Scope Work

I have a requirement where I need to process files based on the rest call in which I get the name of the file, I am adding it to the job parameter and using it while creating the beans.

I am creating step scope Beans for (reader,writer) and using the job parameter.I am starting the job in a new thread as I am using asynchronus task exceutor to launch the job and my question is how will the beans be created by spring when we define @StepScope

jobParametersBuilder.addString("fileName", request.getFileName()); jobExecution = jobLauncher.run(job, jobParametersBuilder.toJobParameters()); @Bean public JobLauncher jobLauncher() {     SimpleJobLauncher jobLauncher = new SimpleJobLauncher();     jobLauncher.setJobRepository(jobRepository());     jobLauncher.setTaskExecutor(asyncTaskExecutor());     return jobLauncher; }  @Bean @StepScope public ItemWriter<Object> writer(@Value ("#{jobParameters['fileName']}"String    fileName) {     JdbcBatchItemWriter<Object> writer = new JdbcBatchItemWriter<>();     writer.setItemSqlParameterSourceProvider(         new BeanPropertyItemSqlParameterSourceProvider<Object>());     writer.setSql(queryCollection.getquery());     writer.setDataSource(dataSource(fileName));     return writer; } 
like image 517
lakshmi kanth Avatar asked Aug 05 '16 03:08

lakshmi kanth


People also ask

What is the use of @StepScope?

Annotation Type StepScopeUse this on any @Bean that needs to inject @Values from the step context, and any bean that needs to share a lifecycle with a step execution (e.g. an ItemStream). E.g.

How does Spring Batch chunk work?

Spring Batch uses chunk oriented style of processing which is reading data one at a time, and creating chunks that will be written out within a transaction. The item is read by ItemReader and passed onto ItemProcessor, then it is written out by ItemWriter once the item is ready.

What are the different bean scope in Spring Batch?

As you probably know, the default bean scope in Spring is a singleton. But by specifying a spring batch component being StepScope means that Spring Batch will use the spring container to instantiate a new instance of that component for each step execution.

How does Spring Batch application work?

Spring Batch follows the traditional batch architecture where a job repository does the work of scheduling and interacting with the job. A job can have more than one step. And every step typically follows the sequence of reading data, processing it and writing it.


1 Answers

A spring batch StepScope object is one which is unique to a specific step and not a singleton. As you probably know, the default bean scope in Spring is a singleton. But by specifying a spring batch component being StepScope means that Spring Batch will use the spring container to instantiate a new instance of that component for each step execution.

This is often useful for doing parameter late binding where a parameter may be specified either at the StepContext or the JobExecutionContext level and needs to be substituted for a placeholder, much like your example with the filename requirement.

Another useful reason to use StepScope is when you decide to reuse the same component in parallel steps. If the component manages any internal state, its important that it be StepScope based so that one thread does not impair the state managed by another thread (e.g, each thread of a given step has its own instance of the StepScope component).

like image 60
Naros Avatar answered Sep 24 '22 23:09

Naros