spring batch job

Spring job to use new service

Spring Batch to use new service

spring batch job

In the world of enterprise applications, data is everywhere. Whether it’s financial transactions, large CSV files, or legacy systems, businesses often need to process large amounts of data in a reliable and efficient way. That’s where Spring Batch comes in.

If you’re a Java developer or a student trying to understand what Spring Batch is, how it works, and when to use it, you’re in the right place. In this post, we’ll break down Spring Batch in a friendly, non-boring way — no overly complex theory, just real, practical knowledge.

 What is Spring Batch?

Spring Batch is a framework for batch processing in Java, built on top of the Spring Framework.

Let’s simplify that:
Spring Batch is a toolkit that helps you process huge amounts of data in chunks, step-by-step, automatically handling the boring stuff like logging, skipping errors, or restarting failed jobs.

It’s designed for batch jobs — these are programs that:

  • Run periodically (like daily or weekly)

  • Process large data sets (think millions of records)

  • Perform a series of steps (like read, process, write)

  • Are not real-time (they don’t need instant output)


Real-Life Examples of Batch Jobs

To understand Spring Batch, let’s look at real-world examples:

ScenarioDescription
Bank Statement GenerationA bank generates monthly statements for each customer by reading transaction data, calculating balances, and emailing or printing statements.
Data MigrationMoving millions of records from an old database to a new one in chunks.
Invoice ProcessingReading order details from a CSV, applying taxes, and generating invoices.
Report GenerationGenerating sales reports for every region and product for the past month.

These are classic use-cases for Spring Batch.


Core Components of Spring Batch (Explained Simply)

Spring Batch uses a few building blocks to define and run batch jobs. Here’s a breakdown in simple terms:

1. Job

A Job is the main task you want to run — like “process monthly bank statements.”

2. Step

Each Job is broken into Steps, which are smaller tasks. For example:

  • Step 1: Read transactions

  • Step 2: Calculate totals

  • Step 3: Send emails

Each Step follows a Read → Process → Write model.

3. ItemReader

Reads data — from a database, a file, a queue, etc.

Example: Read customer records from a CSV file.

4. ItemProcessor

Optional. Used to modify or validate the data.

Example: Skip invalid records or calculate tax.

5. ItemWriter

Writes the processed data to a destination — a file, DB, etc.

Example: Save results to another database or send emails.


Understanding the Flow: Read → Process → WriteLet’s take a simple use-case: reading employee records from a CSV, increasing salary by 10%, and writing back to a database.
CSV File ──▶ ItemReader ──▶ ItemProcessor ──▶ ItemWriter ──▶ Database

It’s like a mini production line:

  1. ItemReader picks up raw data

  2. ItemProcessor cleans or transforms it

  3. ItemWriter stores the final result

 How Does Spring Batch Handle Large Data?

Spring Batch doesn’t load all records at once. It processes data in chunks.

For example:

      Read 100 records

  • Process them

  • Write them

  • Repeat until done

This approach is memory-efficient and faster.

 Spring Batch Features (That Save Developers Time)

FeatureWhat it Does
Transaction ManagementAutomatically rolls back failed steps
Chunk ProcessingEfficient memory usage
Restart/RecoveryResume jobs from where they failed
Job ParametersRun same job with different input
Logging/MonitoringSee which steps passed/failed
Parallel ProcessingSpeeds up large jobs


A Sample Spring Batch Job

Let’s break down a basic Spring Batch configuration using Java code.

java
 

@Bean
public Job importUserJob(JobBuilderFactory jobBuilderFactory, Step step1) {
return jobBuilderFactory.get("importUserJob")
.incrementer(new RunIdIncrementer())
.flow(step1)
.end()
.build();
}

@Bean
public Step step1(StepBuilderFactory stepBuilderFactory,
ItemReader<User> reader,
ItemProcessor<User, User> processor,
ItemWriter<User> writer)
{
return stepBuilderFactory.get(“step1”)
.<User, User>chunk(10)
.reader(reader)
.processor(processor)
.writer(writer)
.build();
}

You can see:

  • We define a Job called importUserJob

  • The job contains one Step

  • That step processes chunks of 10 User items

  • Each chunk flows through reader → processor → writer


Common Readers and Writers

Spring Batch provides ready-made readers and writers for common use-cases:

ReaderUse
FlatFileItemReaderReads CSV or flat files
JdbcCursorItemReaderReads from relational databases
StaxEventItemReaderReads XML files
MongoItemReaderReads from MongoDB

WriterUse
FlatFileItemWriterWrites to a CSV file
JdbcBatchItemWriterWrites to databases using JDBC
MongoItemWriterWrites to MongoDB
JpaItemWriterUses JPA for persistence


Monitoring and Restart

Let’s say your job fails after processing 1000 out of 10,000 records. You don’t want to start from scratch.

Spring Batch maintains JobRepository which stores metadata about job status. When you restart the job, it resumes from where it left off.


Testing Spring Batch Jobs

Spring Batch provides test utilities like:

  • JobLauncherTestUtils

  • JobRepositoryTestUtils

You can test:

  • Whether your job completes successfully

  • Whether the processor is transforming data correctly

  • Whether bad data is skipped


Error Handling and Skipping

What if a record has a missing field? Or the processor throws an error?

Spring Batch allows you to skip faulty records or retry failed ones.

java
 
.skip(FlatFileParseException.class)
.skipLimit(10)

This means: Skip up to 10 bad records. Beyond that, fail the job.

 Advanced: Parallel Steps and Partitioning

Once you get comfortable with basics, Spring Batch lets you:

  • Run steps in parallel (multithreading)

  • Partition jobs into smaller chunks handled by separate threads

  • Remote chunking – process chunks on different servers

This is useful for huge datasets and high performance requirements.


Spring Batch vs Spring Boot: What’s the Difference?

Spring Boot is like the framework to build modern Spring apps.

Spring Batch is a specialized tool for batch processing.

However, they work great together. You can run Spring Batch jobs using Spring Boot’s simplicity and auto-configuration.

java
 
@SpringBootApplication
@EnableBatchProcessing
public class MyBatchApp {
public static void main(String[] args) {
SpringApplication.run(MyBatchApp.class, args);
}
}


Scheduling Spring Batch Jobs

Use Spring’s @Scheduled annotation or tools like Quartz to schedule your batch jobs.

java
 
@Scheduled(cron = "0 0 0 * * *") // Every day at midnight
public void runBatchJob() throws Exception {
jobLauncher.run(job, new JobParameters());
}

You can also trigger jobs via REST endpoints, Command Line, or scripts.


Best Practices

Here are some expert tips when working with Spring Batch:

TipWhy It Matters
Use chunk size wiselyLarge size = better performance, small size = safer rollback
Always set JobParametersHelps with re-runs and uniqueness
Log everythingEasier debugging and monitoring
Handle exceptions clearlyKnow which data caused failures
Use database for job metadataEnables restart and monitoring


Spring Batch and Microservices

Can Spring Batch run in a microservice world?

Yes!

In modern architecture:

  • Each microservice can run a Spring Batch job

  • Use remote partitioning or message queues (like RabbitMQ) for coordination

  • Use Spring Cloud Data Flow for orchestration

This way, even your batch processes can scale horizontally.


Learning Resources

If you want to go deeper:


 Conclusion

Spring Batch is an extremely powerful and battle-tested solution for handling large, repeatable, and robust data-processing jobs in Java.

Here’s a quick recap:

  • Spring Batch helps process large data sets reliably.

  • It follows the Read → Process → Write pattern.

  • It supports chunking, parallel steps, restart, skipping, and more.

  • You can combine it with Spring Boot and schedule or trigger jobs easily.

  • It’s ideal for banking, e-commerce, healthcare, government, or any data-heavy field.

Whether you’re a student learning backend systems, or a professional building enterprise apps, Spring Batch is a must-have tool in your toolkit.

Option 1: Scheduled Job (Simpler)

If you’re not using Spring Batch and just want a periodic job:

Step 1: Create the Service

java
 
@Service
public class MyNewService {
public void performTask() {
// Your logic here
System.out.println("Performing task in MyNewService...");
}
}

Step 2: Create a Scheduled Job that Uses It

java
 

@Component
public class MyScheduledJob {

private final MyNewService myNewService;

public MyScheduledJob(MyNewService myNewService) {
this.myNewService = myNewService;
}

@Scheduled(fixedRate = 60000) // Runs every 60 seconds
public void runJob() {
System.out.println(“Scheduled job started…”);
myNewService.performTask();
System.out.println(“Scheduled job finished.”);
}
}

Step 3: Enable Scheduling

In your Application class:

java
 
@SpringBootApplication
@EnableScheduling
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}

Option 2: Spring Batch Job (More complex, if needed)

Let me know if you’re specifically using Spring Batch with Job, Step, and JobLauncher—and I can provide a tailored Spring Batch setup (e.g., with Tasklet or ItemReader/ItemProcessor/ItemWriter).


👉 Let Me Know

 

Posted In :

Leave a Reply

Your email address will not be published. Required fields are marked *