EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Spring Tutorial Spring Batch Processing
 

Spring Batch Processing

Updated March 30, 2023

Spring Batch Processing

 

 

Introduction to Spring Batch Processing

Spring batch processing is characterized by non-interactive, frequently long-running background execution, and bulk-oriented is utilized in almost every industry and for a wide range of tasks. Batch processing can be data or computationally heavy, run sequentially or in parallel, and be started using different invocation models, such as ad hoc, scheduled, or on-demand. Spring Batch is a lightweight, feature-rich framework that makes it easier to create reliable batch applications.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

What is spring batch processing?

  • Any developer should be knowledgeable and comfortable with the fundamental notions of batch processing.
  • It introduces spring Batch’s fundamental concepts and phrases in batch processing.
  • A Job, which is made up of numerous Steps, usually encapsulates a batch operation. ItemProcessor, ItemReader, and ItemWriter are usually present in each Step.
  • A JobLauncher executes a job, while a JobRepository store’s metadata about jobs configured and executed.
  • Each Job can have several JobInstances, each of which is defined by its own set of job parameters. For example, a JobExecution is the name for a single run of a Job Instance.
  • Every Job is made up of one or more steps, each of which is an independent, specific portion of a batch Job.
  • A single StepExecution, similar to a Job, represents a single attempt to perform a Step. StepExecution keeps track of current and exit statuses, start and finish times, and pointers to the corresponding Step and JobExecution instances.

Spring batch processing Application setup

Below examples shown to set up the project of spring batch processing are as follows.

  • Create project template of spring batch processing by using spring initializer
  • In the below step, we have provided project group name as com.example, artifact name as SpringBatchProcessing, project name as SpringBatchProcessing, and selected java version as 8. Also, we have defined the spring boot version as 2.6.0, defined the project as maven.
  • We have selected spring web, spring batch, spring data JPA, and PostgreSQL driver dependency in the below project to implement the spring batch processing project.

Group – com.example
Artifact name – SpringBatchProcessing
Name – SpringBatchProcessing
Spring boot – 2.6.0
Project – Maven
Java – 8
Package name – com.example.SpringBatchProcessing
Project Description – Project for SpringBatchProcessing
Dependencies – spring web, PostgreSQL driver, spring batch, Spring data JPA.

Spring Batch Processing output 1Spring Batch Processing output 1

  • After generating project extract files and open this project by using spring tool suite –

After generating the project using the spring initializer in this step, we extract the jar file and open the project using the spring tool suite.

Spring Batch Processing output 2

  • After opening the project using the spring tool suite, check the project and its files –

In this step, we are checking all the project template files. We also need to check maven dependencies and system libraries.

Spring Batch Processing output 3

  • Add dependency packages –

In this step, we are adding the spring batch admin dependency in the spring batch project.

Code –

<dependency>   -- Start of dependency tag.
<groupId>org.springframework.batch</groupId>   -- Start and end of groupId tag.
<artifactId>spring-batch-test</artifactId>  -- Start and end of artifactId tag.
</dependency>    -- End of dependency tag.
<dependency>   -- Start of dependency tag.
<groupId>org.postgresql</groupId>   -- Start and end of groupId tag.
<artifactId>postgresql</artifactId>  -- Start and end of artifactId tag.
</dependency>    -- End of dependency tag.

Spring Batch Processing output 4

  • Create data layer –

Code –

@Entity(name = "student")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@EntityListeners (AuditingEntityListener.class)
public class Stud
{@Id
@Column(name = "id")
@GeneratedValue (strategy = GenerationType.AUTO)
private int stud_id;
@Override
public String toString() {
return "stud_id: " + stud_id;
}
}

Spring Batch Processing output 5

  • Create repository layer –

Code –

public interface StudentRepo extends JpaRepository<Stud, Long> {
}

output 6

  • Create processor –

Code –

public class StudProcessor implements ItemProcessor {
private static final Logger l = (Logger) LoggerFactory.getLogger(StudProcessor.class);
public String process(final Stud st) throws Exception {
final String firstName = st.getStud_ID ().toUpperCase();
String student = null;
l.info("Convert (" + st + ") into (" + student + ")");
return student;
}
@Override
return null;
}
}

output 7

  • Create configuration layer –

Code –

@Configuration
@EnableBatchProcessing
public class Config {
@Autowired
public JobBuilderFactory jbf;
@Autowired
public StepBuilderFactory sbf;
@Bean
public FlatFileItemReader<Stud> reader() {
return new FlatFileItemReaderBuilder<Stud>()
.name("studReader")
.resource(new ClassPathResource("stud.csv"))
.delimited()
.names(new String[]{"Stud_id"})
.fieldSetMapper(new BeanWrapperFieldSetMapper<>() {{
setTargetType(Stud.class);
}})
.build();
}
@Bean
public RepositoryItemWriter<Stud> writer()
{
RepositoryItemWriter<Stud> iwriter = new RepositoryItemWriter<>();
CrudRepository<Stud, ?> studRepo = null;
iwriter.setRepository(studRepo);
iwriter.setMethodName("Save");
return iwriter;
}

output 8

  • Create controller layer 

Code –

@RestController
@RequestMapping(path = "/batch")// Root path
public class Controller
{
@Autowired
private JobLauncher jl;
@Autowired
private Job j;
@GetMapping(path = "/start")
public ResponseEntity<String> startBatch () throws org.springframework.batch.core.repository.JobRestartException
{
JobParameters p = new JobParametersBuilder()
.addLong("started", System.currentTimeMillis()).toJobParameters();
return new ResponseEntity<>("Process of batch is started", HttpStatus.OK);
}
}

 output 9

  • Configure application.properties file –

Code 

server.port=8080
spring.batch.job.enabled=false

output 10

  • Run the application –

output 11

Spring batch processing Framework

  • Spring Batch is a lightweight, feature-rich batch framework that enables the creation of dependable batch applications that are critical to enterprise systems’ day-to-day operations.
  • Batch operations that handle large amounts of data can take advantage of the framework in a highly scalable way.
  • Spring Batch is based on the Spring Framework; we should be familiar with the capabilities and functions of spring.
  • Spring batch has the benefit of having few project dependencies, making it easier to get up and running quickly.

Key Concepts and Terminology

  • The ExecutionContext is saved by Spring Batch, which is useful if you need to restart a batch job.
  • All of this persistence is made possible through the JobRepository mechanism in Spring Batch. In addition, it offers CRUD operations for JobLauncher, Job, and Step instantiations.
  • A JobExecution is retrieved from the repository after a Job is launched, and StepExecution and JobExecution instances persist during the execution process.
  • To develop a spring boot processing application, we need to add spring web, spring batch, spring data JPA and PostgreSQL driver dependency in the pom.xml file.

Conclusion

Any developer should be knowledgeable and comfortable with the fundamental notions of batch processing. Spring Batch is a lightweight, feature-rich batch framework. Spring batch is characterized by non-interactive, frequently long-running background execution, and bulk-oriented is utilized in almost every industry and for a wide range of tasks.

Recommended Articles

This is a guide to Spring Batch Processing. Here we discuss the Spring batch processing Framework along with the Key Concepts and Terminology. You may also have a look at the following articles to learn more –

  1. Spring Boot Docker
  2. Spring Boot Maven
  3. What is Spring Boot?
  4. Spring Boot Interview Questions
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW