Top Spring Batch Interview Questions (2024)
Spring Batch Overview - Architecture
Spring Boot + Spring Batch + MySQL Simple Example
Spring Boot + Spring Batch Listener Example
Spring Boot + Spring Batch Job Scheduler Example
- Spring Batch Interview Questions and Answers
What is Spring Batch?
What is Spring Batch Admin?
What is the Basic Structure of Spring Batch?
Where batch processing is used?
What is batch processing architecture?
How to initialize a Spring Batch Database?
What is Job?
Why to use "incrementer(new RunIdIncrementer())" while configuring Job?
What is RunIdIncrementer in Spring Batch?
What is Step in Job?
What is JobLauncher?
What is ItemReader?
What is ItemProcessor?
What is ItemWriter?
How to configure the Job in Spring Batch?
What is spring batch listener?
What is difference between Step, Chunk and Tasklet?
What is execution context in Spring Batch?
What is StepScope in Spring Batch?
What is Job Repository in Spring Batch?
What is Step Partition in Spring Batch?
What is Spring Batch Job Launcher?
What is remote chunking in Spring Batch?
What is difference between Remote Partitioning and Remote Chunking in Spring Batch?
How do I prevent all jobs in the context from being executed by default when the application starts up?
What is Skip limit in Spring Batch?
What is JobBuilderFactory in Spring Batch?
What is Spring Batch Listener?
What is JobExecutionListener in Spring Batch?
What is StepExecutionListener in Spring Batch?
How to configure JobExecutionListener in Spring Batch?
How to configure StepExecutionListener in Spring Batch?
What is SkipListener in Spring Batch Listener
Spring Batch Tutorial :
Q: What is Spring Batch?
Ans:
Spring Batch is an open source,lightweight framework for batch processing by performing a series of jobs. It allows processing of bulk data in a transactional manner and execute day to day activity with precision and speed.
Q: What is Spring Batch Admin?
Ans:
Admin has a web based UI, and spring batch console. It is an application built on the web, open source based on Spring MVC.
Q: What is the Basic Structure of Spring Batch?
Ans:
The following figure shows the different components of Spring Batch and how they are connected with each other.
Checkout our related posts :
Q: Where batch processing is used?
Ans:
It is commonly used in enterprise applications where large volumes of data are used to perform. For example, large data are tested regularly in enterprise applications.
Q: What is batch processing architecture?
Ans:
Job has complete batch process and one or more steps are included in the job.
As shown in below diagram, black line represents Main processing flow and blue line represents the flow which persists job information.
Main processing flow
- JobLauncher is initiated from the job scheduler.
- The JobLauncher executes the job.
- Job performs the step.
- Step uses ItemReader to get input data.
- ItemProcessor is used to process input data in Step.
- ItemWriter is used by Step to output processed data.
A flow for persisting job information
- JobLauncher registers JobInstance in Database via JobRepository.
- JobLauncher registers that Job execution has begun in Database via JobRepository.
- JobStep updates miscellaneous details such as counts of I/O records and status in Database via JobRepository.
- JobLauncher registers that Job execution has successfully completed in Database via JobRepository.
Q: What is Job?
Ans:
A Job is the batch process to be executed in a Spring Batch application without interruption
from start to finish. This Job is further broken down into steps.
A Job is made up of many steps and each step is a READ-PROCESS-WRITE task or a single
operation task (tasklet).
/**
* A Job is made up of many steps and each step is
* a READ-PROCESS-WRITE task or a single operation task (tasklet).
* @return job
*/
@Bean
public Job simpleJob() {
return jobBuilderFactory.get("simpleJob")
.incrementer(new RunIdIncrementer())
.flow(step1())
.end()
.build();
}
Refer
Example to understand Spring Batch Job.
Q: Why to use "incrementer(new RunIdIncrementer())" while configuring Job?
Ans:
incrementer(new RunIdIncrementer())
You can notice while creating Job, we have used incrementer. Since jobs uses a database to maintain execution state, you'll need an incrementer in this job definition. After that, you create a list of each step (though this job has only one step). When the job is completed, the Java API generates a perfectly configured job.
Q: What is RunIdIncrementer in Spring Batch?
Ans:
We cannot rerun a Job if a JobExecution for this job has been COMPLETED, according to the Spring Batch guidelines. Since it updates the job parameters internally, a RunIdIncrementer instance can be used to run the same job multiple times.
Q: How to initialize a Spring Batch Database?
Ans:
spring.batch.initialize-schema=ALWAYS
Initialize a Spring Batch Database: Setting spring.batch.initialize-schema
to ALWAYS
value by default create the tables if you are
using an embedded database. For more information can visit
Spring Batch official documentation.
If you don't add this entry in application.properties, it will throw below exception.
INFO 15304 --- [nio-8080-exec-1] c.t.s.c.BatchJobLauncherController
: PreparedStatementCallback; bad SQL grammar [SELECT JOB_INSTANCE_ID,JOB_NAME
from BATCH_JOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ?];
nested exception is java.sql.SQLSyntaxErrorException: Table 'batchexampledb.batch_job_instance'
doesn't exist
Refer example to initialize a Spring Batch Database.
Q: What is Step in Job?
Ans:
Spring Batch Step is an independent part of a job. As per above Spring Batch structure diagram, each Step consist of an ItemReader, ItemProcessor (optional) and an ItemWriter. Note: A Job can have one or more steps.
/**
* Step consist of an ItemReader, ItemProcessor and an ItemWriter.
* @return step
*/
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<String, String> chunk(1)
.reader(new MessageReader())
.processor(new MessageProcessor())
.writer(new MessageWriter())
.build();
}
Refer
Example to understand Spring Batch Step.
Q: What is ItemReader?
Ans:
An ItemReader reads data into a Spring Batch application from a particular source.
public class MessageReader implements ItemReader<String> {
/**
* It read the data from the given source
*
* @return String
* @throws Exception
*/
@Override
public String read() throws Exception {
//read and pass message to processor to process the message
return ...;
}
}
Refer
Example to understand Spring Batch ItemReader.
Q: What is ItemProcessor?
Ans:
After reading the input data using itemReader, ItemProcessor applies business logic on that input data and then writes to the file / database by using itemWriter.
public class MessageProcessor implements ItemProcessor<String, String> {
/**
* Read input data from itemReader, and then ItemProcessor applies the business logic here
*
* @param content
* @return String
* @throws Exception
*/
@Override
public String process(String content) throws Exception {
// handle business logic here
}
}
Refer
Example to understand Spring Batch ItemProcessor.
Q: What is ItemWriter?
Ans:
ItemWriter writes data from the Spring Batch application to a particular destination.
public class MessageWriter implements ItemWriter<String> {
/**
* ItemWriter writes received data to destination.
*
* @param inputMessage
* @throws Exception
*/
@Override
public void write(List<? extends String> inputMessage) throws Exception {
// write data to destination eg to database MYSQL etc..
}
}
Refer
Example to understand Spring Batch ItemWriter.
Q: What is Job Repository in Spring Batch?
Ans:
Job Repository is used to persist all meta-data related to the execution of the Job.
JobRepository Configuration:
<job-repository id="jobRepository"
data-source="dataSource"
transaction-manager="transactionManager"
table-prefix="SAMPLE_BATCH_"
max-varchar-length="1500"/>
- id: Mandatory field to provide id value.
- data-source: Can be configured to point to the database to be used for storing batch meta-data entities.
- transaction-manager: Entity to handle transaction management
- table-prefix: The Spring Batch meta-data are stored in tables starting with 'SPRING_BATCH_' as the prefix.
Q: How to configure the Job in Spring Batch?
Ans:
There are many ways we can configure the Spring Batch Job. We are using here builder abstract way to
call the jobs. Job needs JobRepository to configure the job. If you see below Job has three steps to
load the notes, to load the task and to process those
tasks.
@Bean
public Job employeeJob() {
return this.jobBuilderFactory.get("notesJob")
.start(LoadNotes())
.next(LoadTasks())
.next(processTasks())
.end()
.build();
}
Q: What is difference between Step, Chunk and Tasklet?
Ans:
A Step is a domain object which contains an independent, sequential phase of a batch job and includes all of the information required to define and control the batch processing operations.
The following two ways can be used to process steps.
- Chunk
- Tasklet
Parameter |
Tasklet |
Chunk |
When to use |
Tasklet processing is utilised if the job is to be run as a single granular task. |
If the job to be performed is complicated and requires the execution of tasks involving reads, processing, and writing, chunk oriented processing is used. |
How it works |
There is no aggregation; only the task is executed. |
Reading an input, processing it according to business logic, aggregating it till the commit-interval is achieved, and ultimately sending out the chunk of data output to a file or database table are all part of the process. |
Usage |
It isn't often used. |
It's mostly used for executing a Step. |
Use Case |
Typically used in single task, such as removing a resource or executing a query. |
Typically utilised in cases where multiple aggregated steps, such as data copying, processing, and transferring, must be performed. |
Q: What is execution context in Spring Batch?
Ans:
An execution context is a data map with which batch components can interact throughout a job run, either by adding data to the map or reading data from the map. Each JobExecution has its own ExecutionContext, and each StepExecution has its own ExecutionContext. Simplified: one map for the entire project and one map for each step.
Q: What is StepScope in Spring Batch?
Ans:
The objects whose scope is StepScope, for those objects Spring Batch will use the spring container to create a new instance of that object for each step execution.
Q: What is Step Partition in Spring Batch?
Ans:
Spring batch can be handled in a single-process job, however to have a multi-process job, we can use Partitioning a Step. In Spring Batch Step Partitioning, Step is divided into a number of child steps, which may be used either as remote instances or as local execution threads.
Q: What is Spring Batch Job Launcher?
Ans:
Spring batch job launcher is an interface for running the jobs, which uses run method with two parameters.
Example:
import org.springframework.batch.core.launch.JobLauncher;
...
...
@Autowired
JobLauncher jobLauncher;
@Autowired
Job simpleJob;
....
....
JobParameters jobParameters = new JobParametersBuilder()
.addLong("time", System.currentTimeMillis())
.toJobParameters();
//job launcher is an interface for running the jobs
jobLauncher.run(simpleJob, jobParameters);
Refer
Example to understand Spring Batch Job Launcher
Q: What is remote chunking in Spring Batch?
Ans:
In spring batch remote chunking, Master Step reads the date and pass over to slaves for processing.
Q: What is difference between Remote Partitioning and Remote Chunking in Spring Batch?
Ans:
Both Partitioning is master-slave based process, however below is the difference between them.
- Remote Partitioning : In this it allows data to be partitioned and executed parallely. For example, we can say partition is divided into set of data, like if have 30 rows, so first data set would 1-10 rows, second data set will have 11-20 and so on.. Master Step have all meta data like all partition data sets and slave executes those meta data and send result back to master for aggregation.
- Remote Chunking : In Remote Chunking, Master Step read the data and has control to pass the data to its Slaves for processing. Once slaves process data,the result of the ItemProcessor is returned to the master for writing.
Q: How do I prevent all jobs in the context from being executed by default when the application
starts up?
Ans:
Add spring.batch.job.enabled=false
in the application.properties as given in this
example to disable this behavior on application
startup.
Q: What is Skip limit in Spring Batch?
Ans:
Spring batch skips the items,once it reaches the maximum skip limit. The will automatically failed the batch jobs based on spring batch skip limit.
Q: What is JobBuilderFactory in Spring Batch?
Ans:
It create job builder and initializes it's job repository
JobBuilderFactory(JobRepository jobRepository)
Q: What is Spring Batch Listner?
Ans:
Listeners are entities that help in intercepting the execution of a Job or Step and allowing the user to do certain functionality.
Refer Example to understand Spring Listener.Q: What is JobExecutionListener in Spring Batch?
Ans:
JobExecutionListener
offers interceptions and life-cycle methods for Spring
Batch Jobs.
There are two methods beforeJob()
and afterJob()
, which, as the names
indicate,
allow us to do whatever we want before the execution of a job begins and after the execution of
the job finishes.
public class SpringBatchJobExecutionListener implements JobExecutionListener {
public void beforeJob(JobExecution jobExecution) {
// code to execute before the job gets start
}
public void afterJob(JobExecution jobExecution) {
// code to execute afetr the job gets completed
}
}
Refer
Example to understand JobExecutionListener in Spring Batch Listener.
Q: What is StepExecutionListener in Spring Batch?
Ans:
StepExecutionListener
offers interceptions and life-cycle methods for Spring
Batch Steps.
There are two methods beforeJob()
and afterJob()
, which, as the names
indicate,
allow us to do whatever we want before the execution of a step begins and after the execution of
the step finishes.
The afterStep() method returns an ExitStatus, which indicates whether or not the step execution
was successful.
public class SpringBatchStepExecutionListener implements StepExecutionListener {
@Override
public void beforeStep(StepExecution stepExecution) {
// code to execute before the step gets start
}
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
// code to execute after the step gets completed
//ExitStatus indicate that step has completed
return ExitStatus.COMPLETED;
}
}
Refer
Example to understand StepExecutionListener in Spring Batch Listener.
Q: How to configure JobExecutionListener in Spring Batch?
Ans:
Configure JobExecutionListener in Job as given below:
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Bean
public Job job() {
return jobBuilderFactory.get("simpleJob")
.incrementer(new RunIdIncrementer())
// configure JobExecutionListener
.listener(new SpringBatchJobExecutionListener())
.flow(step1())
.end()
.build();
}
Refer
Example to configure JobExecutionListener in Spring Batch.
Q: How to configure StepExecutionListener in Spring Batch?
Ans:
Configure StepExecutionListener in Step as given below:
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
//configure StepExecutionListener
.listener(new SpringBatchStepExecutionListener())
.<String, String> chunk(1)
.reader(new MessageReader())
.processor(new MessageProcessor())
.writer(new MessageWriter())
.faultTolerant()
.retryLimit(3)
.retry(Exception.class)
.build();
}
Refer
Example to configure StepExecutionListener in Spring Batch.
Q: What is SkipListener in Spring Batch Listener?
Ans:
SkipListener is the listener for items that have been skipped, that is, items that did not apply through all three stages of step execution, which include, read, process, and write.
Interface:
public interface SkipListener < T, S > extends StepListener {
void onSkipInRead(Throwable var1);
void onSkipInWrite(S var1, Throwable var2);
void onSkipInProcess(T var1, Throwable var2);
}
Implementation:
public class TestSkipListener implements SkipListener < String, Number > {
Logger logger = LoggerFactory.getLogger(TestSkipListener.class);
@Override
public void onSkipInRead(Throwable t) {
logger.info("onSkipInRead");
}
@Override
public void onSkipInWrite(Number item, Throwable t) {
logger.info("onSkipInWrite");
}
@Override
public void onSkipInProcess(String item, Throwable t) {
logger.info("onWriteError");
}
}