Spring Batch is often used for data processing jobs that don't run continuously. Instead, they start, process, and stop, which makes them a perfect candidate for GraalVM Native Image. Unlike traditional Java applications that require a long JVM startup time, Native Images execute almost instantly, giving a significant performance boost.
In this article, you'll learn how to build a Spring Batch application that reads a CSV file, logs its content, and writes it to a PostgreSQL database. We'll see how to compile it into a Native Image using GraalVM, and most importantly, weโll benchmark the difference between running it as a traditional JAR and as a native binary.
- Definitions
- Requirement
- Create a Spring Batch app
- Compile it into a Native Image
- Benchmark
๐ตโชโชโชโชโชโชโช
1๏ธโฃ Definitions
What is a Native Image?
A Native Image is a standalone executable
that includes everything the application needs to run (classes, libraries, and the JVM itself) in one package. Unlike JVM-based JAR files, native images do not require a JVM at runtime, which means:
- Faster startup times (up to 50x faster)
- Lower memory usage (since thereโs no JVM)
- There is no need for JVM warm-up (perfect for short-lived apps like batch jobs)
๐ What is GraalVM?
GraalVM is a universal virtual machine
that can execute applications written in Java, Kotlin, Scala, and other languages. One of its most powerful features is the Native Image capability, which converts Java applications into ahead-of-time (AOT) compiled executables. This means the Java bytecode is turned into platform-specific machine code.
When you compile a Spring Boot application into a native image, you remove the need for the JVM, which leads to instant start times and minimal resource usage.
๐ต๐ตโชโชโชโชโชโช
2๏ธโฃ Requirement
Before starting, ensure you have the following tools installed on your machine:
GraalVM:
Download and install from GraalVM Downloads.
https://www.graalvm.org/downloads/
Visual Studio and Visual Studio Code:
Install Visual Studio (for C++ tools), as GraalVM uses native compilers.
https://www.graalvm.org/latest/getting-started/windows/
PostgreSQL:
3๏ธโฃ Create a Spring Batch Application
Spring Initializr:
Go to Spring Initializr: https://start.spring.io
Speed up your Spring Batch with Native Image and GraalVM
- PostgreSQL Driver
- Spring Data JPA
- Spring Cloud Native
Add the implementation
Here, I defined a batch for a billing service.
The business object
The batch job with its tasks: reading a CSV, logging it, and writing it to DB
Full complete code: https://github.com/vinny59200/spring-batch-native-image
๐ต๐ต๐ต๐ตโชโชโชโช
4๏ธโฃ Compile it into a Native Image
Commands to Compile
- Open Visual Code, open a terminal
- Clear previous builds: mvn clean package
- Compile to Native Image: ./mvnw -Pnative native:compile -DskipTests (It is a bit long --1 or 2min)
๐ต๐ต๐ต๐ต๐ตโชโชโช
5๏ธโฃ Benchmark
Without Native Image
Run it using the standard JVM-based JAR:
java -jar target/nativeBatch-0.0.1-SNAPSHOT.jar
โฑ๏ธ Execution time: ~4 seconds
With Native Image
Run it using the compiled native image:
./target/nativeBatch
โฑ๏ธ Execution time: ~0.2 seconds
Results Summary
๐ Why Native Image Wins
๐ต๐ต๐ต๐ต๐ต๐ตโชโช
๐ฃ Conclusion
For Spring Batch applications, Native Images are a game-changer.
- Instantaneous startup: From 4 seconds to 0.2 seconds.
- Perfect for batch jobs: Jobs that start, process, and exit benefit the most.
- Lower memory usage: No JVM, no warm-up, no extra overhead.
By using GraalVM Native Image, you can build batch jobs that are fast, efficient, and perfectly suited for cloud environments where "scale to zero" is essential.
If you're dealing with micro-batch jobs that run for seconds, Native Image is a must-have. If your batch jobs are long-lived (running for hours), the benefit is less significant, but for fast, one-shot batch jobs, Native Image is unbeatable.
๐ Switch to Native Image for Spring Batch. Your jobs will love it. ๐
๐ต๐ต๐ต๐ต๐ต๐ต๐ตโช
๐บ Video
๐ต๐ต๐ต๐ต๐ต๐ต๐ต๐ต
๐ More
- https://foojay.io/today/prevent-ldap-injection-in-java-with-springboot/
- https://foojay.io/today/a-simple-service-with-spring-boot/
- https://youtu.be/_oXnnQcD_wc (Legal JVM Dopes For Your Apps โ Dmitri Chuykot!)