Top 23 Spring Batch Interview Questions And Answers

 

1) What is Spring Batch?

Spring Batch is a lightweight framework which is used to develop batch related applications that are used in Enterprises.


2) What are the different functions provided by Spring Batch?

Apart from bulk processing, Spring Batch framework provides functions for following:

● Transaction management
● Including logging and tracing
● Job processing statistics
● Job restart
● Skip and Resource management


3) What are the different features of Spring Batch?

Following are the key features of Spring Batch:

● Spring Batch applications are easy to maintain.
● Spring Batch applications are flexible.
● Easily scalable.
● Automatic retry after failure.
● In case of any failure, you can easily restart the job from exactly where it was stopped.
● Can run concurrent jobs.


4) What are the core components in Spring Batch architecture?

Following are the core components in Spring Batch architecture:

Application - This component contains all the jobs and the code that developer writes using the Spring Batch framework.
Batch Core - This component contains all the API classes that are needed to control and launch the Batch Job.
Batch Infrastructure - This component contains the readers, writers, and services that are used by both application and Batch core components.


5) What are the different components of Spring Batch?

Following are the different components of Spring Batch:

● Job
● Step
● Readers, Writers, and Processors
● JobRepository
● JobLauncher
● JobInstance
● JobExecution and StepExecution


6) How to configure a batch job in Spring Batch application?

Batch job can be configured in Spring Batch application using XML. Below is the code snippet:

<job id = "jobid"> 
   <step id = "step1" next = "step2"/> 
   <step id = "step2" next = "step3"/> 
   <step id = "step3"/> 
</job>


7) How to configure a batch job in Spring Batch application which can not be restarted when other instance of job is already running?

To configure a batch job in Spring Batch application which can not be restarted when other instance of job is already running we have to set the restartable attribute value to false in job tag. Below is the code snippet:

<job id = "jobid" restartable = "false" >
....
</job>


8) What is step in Spring Batch job?

In Spring Batch a step is an independent part of a job which contains the necessary information to define and execute the job. A job may contain one or more steps. Each step in a spring batch job is composed of an ItemReader, ItemProcessor (optional) and an ItemWriter.


9) What are Readers, Writers, and Processors in Spring Batch application?

In spring batch application an item reader reads data from a particular source, whereas an item writer writes data from the Spring Batch application to a particular destination. An Item processor is a class which contains the processing code which processes the data read into the spring batch.


10) What is tasklet in Spring Batch?

In spring batch a tasklet acts as a processor which processes only a single task. Below is the code snippet:

<job id = "Job1"> 
   <step id = "step1"> 
      <tasklet> 
         <chunk reader = "mysqlReader" writer = "fileWriter" 
            processor = "CustomitemProcessor" ></chunk> 
      </tasklet> 
   </step> 
</ job>


11) What is Job repository in Spring Batch?

Job repository in Spring Batch provides Create, Retrieve, Update, and Delete (CRUD) operations for the JobLauncher, Job, and Step implementations. Below is the sample code:

<job-repository id = "jobRepository" 
   data-source = "dataSource" 
   transaction-manager = "transactionManager" 
   isolation-level-for-create = "SERIALIZABLE" 
   table-prefix = "BATCH_" 
   max-varchar-length = "1000"/>


12) What is In-Memory repository in Spring Batch?

In case if someone don't want to persist the domain objects of the Spring Batch in the database, then they can configure the in-memory version of the jobRepository.


13) What is JobLauncher in Spring Batch?

In spring batch JobLauncher is an interface which launces the Spring Batch job with the given set of parameters. Below is the sample code config:

<bean id = "jobLauncher" 
   class = "org.springframework.batch.core.launch.support.SimpleJobLauncher"> 
   <property name = "jobRepository" ref = "jobRepository" /> 
</bean>


14) What is JobInstance in Spring Batch?

In spring batch JobInstance represents the logical run of a job i.e. it is created when we run a job. Each job instance is differentiated by the name of the job and the parameters passed to it while running.


15) What are JobExecution and StepExecution in Spring Batch?

These are the representation of the execution of a job/step. They contain the run information of the job/step such as start time, end time.


16) What is datasource bean in Spring Batch?

The datasource bean is used to configure the Datasource. This bean is associated with the class of type org.springframework.jdbc.datasource.DriverManagerDataSource. Below is the sample code snippet:

<bean id = "dataSource" 
   class = "org.springframework.jdbc.datasource.DriverManagerDataSource"> 
   <property name = "driverClassName" value = "com.mysql.jdbc.Driver" /> 
   <property name = "url" value = "jdbc:mysql://localhost:3306/details" /> 
   <property name = "username" value = "myuser" /> 
   <property name = "password" value = "password" /> 
</bean> 


17) What is TransactionManager in Spring Batch?

The TransactionManager bean is used to configure the TransactionManager using a relational database. This bean is associated with the class of type org.springframework.transaction.platform.TransactionManager. Below is the sample code snippet:

<bean id = "transactionManager"
   class = "org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
</bean> 


18) What is chunk in Spring Batch?

A chunk is a child element of the tasklet. It is used to perform read, write, and processing operations.


19) Which reader we can use to read from XML in spring batch?

StaxEventItemReader is the reader we can use in spring batch to read data from XML documents.


20) Which reader we can use to read from CSV file in spring batch?

FlatFileItemReader is the reader we can use in spring batch to read data from CSV file.


21) Which reader we can use to read from MySQL database in spring batch?

JdbcCursorItemReader is the reader we can use in spring batch to read data from MySQL database.


22) How to define the beans like job repository, job launcher, and transaction manager in context.xml of Spring Batch application?

Below is the code snapshot of context.xml of Spring Batch application having beans like job repository, job launcher, and transaction manager.

<beans xmlns = " http://www.springframework.org/schema/beans" 
   xmlns:jdbc = "http://www.springframework.org/schema/jdbc" 
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
   xsi:schemaLocation = "http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
      http://www.springframework.org/schema/jdbc 
      http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd "> 
   
   <!-- stored job-meta in database --> 
   <bean id = "jobRepository"  
      class = "org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"> 
      <property name = "dataSource" ref = "dataSource" /> 
      <property name = "transactionManager" ref = "transactionManager" /> 
      <property name = "databaseType" value = "mysql" /> 
   </bean>  
   
   <bean id = "transactionManager" 
      class = "org.springframework.batch.support.transaction.ResourcelessTransactionMana ger" />  
   <bean id = "jobLauncher"
      class = "org.springframework.batch.core.launch.support.SimpleJobLauncher"> 
      <property name = "jobRepository" ref = "jobRepository" /> 
   </bean> 
  
   <!-- connect to MySQL database --> 
   <bean id = "dataSource" 
      class = "org.springframework.jdbc.datasource.DriverManagerDataSource"> 
      <property name = "driverClassName" value = "com.mysql.jdbc.Driver" /> 
      <property name = "url" value = "jdbc:mysql://localhost:3306/details" /> 
      <property name = "username" value = "myuser" /> 
      <property name = "password" value = "password" /> 
   </bean> 
  
   <!-- create job-meta tables automatically --> 
   <jdbc:initialize-database data-source = "dataSource">   
      <jdbc:script location = "org/springframework/batch/core/schema-drop-mysql.sql" />   
      <jdbc:script location = "org/springframework/batch/core/schema-mysql.sql" /> 
   </jdbc:initialize-database> 
</beans>


23) What will be the sample pom.xml of Spring Batch application?

Below is the sample pom.xml of Spring Batch application:

<project xmlns = "http://maven.apache.org/POM/4.0.0" 
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
   xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/maven-v4_0_0.xsd"> 
   <modelVersion>4.0.0</modelVersion> 
   <groupId>com.testfry</groupId> 
   <artifactId>SpringBatchSample</artifactId> 
   <packaging>jar</packaging> 
   <version>1.0-SNAPSHOT</version> 
   <name>SpringBatchExample</name>
   <url>http://maven.apache.org</url>  
 
   <properties> 
      <jdk.version>1.8</jdk.version> 
      <spring.version>4.3.8.RELEASE</spring.version> 
      <spring.batch.version>3.0.7.RELEASE</spring.batch.version> 
      <mysql.driver.version>5.1.25</mysql.driver.version> 
      <junit.version>4.11</junit.version> 
   </properties>  
   
   <dependencies> 
      <!-- Spring Core --> 
      <dependency> 
         <groupId>org.springframework</groupId> 
         <artifactId>spring-core</artifactId> 
         <version>${spring.version}</version> 
      </dependency>  
      
      <!-- Spring jdbc, for database --> 
      <dependency> 
         <groupId>org.springframework</groupId> 
         <artifactId>spring-jdbc</artifactId> 
         <version>${spring.version}</version> 
      </dependency>  
      
      <!-- Spring XML to/back object --> 
      <dependency> 
         <groupId>org.springframework</groupId> 
         <artifactId>spring-oxm</artifactId> 
         <version>${spring.version}</version> 
      </dependency>  
   
      <!-- MySQL database driver --> 
      <dependency> 
         <groupId>mysql</groupId> 
         <artifactId>mysql-connector-java</artifactId>
         <version>${mysql.driver.version}</version> 
      </dependency>  
  
      <!-- Spring Batch dependencies --> 
      <dependency> 
         <groupId>org.springframework.batch</groupId> 
         <artifactId>spring-batch-core</artifactId> 
         <version>${spring.batch.version}</version> 
      </dependency> 
  
      <dependency> 
         <groupId>org.springframework.batch</groupId> 
         <artifactId>spring-batch-infrastructure</artifactId> 
         <version>${spring.batch.version}</version> 
      </dependency>  
  
      <!-- Spring Batch unit test --> 
      <dependency> 
         <groupId>org.springframework.batch</groupId> 
         <artifactId>spring-batch-test</artifactId> 
         <version>${spring.batch.version}</version> 
      </dependency>  
  
      <!-- Junit --> 
      <dependency> 
         <groupId>junit</groupId> 
         <artifactId>junit</artifactId> 
         <version>${junit.version}</version> 
         <scope>test</scope> 
      </dependency> 
   </dependencies> 
 
   <build> 
      <finalName>spring-batch</finalName> 
      <plugins> 
         <plugin> 
            <groupId>org.apache.maven.plugins</groupId> 
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version> 
            <configuration> 
               <downloadSources>true</downloadSources> 
               <downloadJavadocs>false</downloadJavadocs> 
            </configuration> 
         </plugin> 
      
         <plugin> 
            <groupId>org.apache.maven.plugins</groupId> 
            <artifactId>maven-compiler-plugin</artifactId> 
            <version>2.3.2</version> 
            <configuration> 
               <source>${jdk.version}</source> 
               <target>${jdk.version}</target> 
            </configuration> 
         </plugin> 
      </plugins> 
   </build> 
</project>


 

Related links:

 

Related #tags: