Skip to main content

Spring on GraalVM Implementation

Topic: GraalVM                                                                                                  Level: Advanced

Spring on GraalVM Implementation

In this consecutive post (prior post), we shall exploit the theory of the previous post on GraalVM, Native Image, and Spring Native and set up a Spring Boot project running on GraalVM that leverages static analysis and linkage during the build time with ahead-of-time compilation delivering a windows native executable.
Spring Boot GraalVM Logo

Prerequisites

  1. GraalVM JDK 11 distribution (https://github.com/graalvm/graalvm-ce-builds/releases/tag/vm-22.3.0) - ensure you revise the JAVA_HOME and PATH appropriately
  2. Native Image executable (for windows, https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=16)
  3. Maven build tool
  4. IntelliJ IDE
  5. MySQL
  6. Windows 10 OS
We will commence by downloading a Spring template with Spring Boot >=2.7.5 version running on JDK 11 with the dependencies for GraalVM Native Support, Spring Web, Spring Data JPA, and MySQL Driver as shown below,
Spring Initializer Illustration
Click on the Generate button and then extract the downloaded project template and import it into the IntelliJ IDE.
Up next we will create the following classes with the objective of contacting an endpoint with firstname (ie., http://localhost:8080/v1/petclinic/{firstname}) provided to obtain the details correspondingly from the underlying database.
  1. Application properties for auto-configuring the Datasource bean on the context startup
  2. A RestController for exposing the GET endpoint to obtain the user data details
  3. A JPARepository implementation for performing database CRUD operations
  4. A domain model for mapping the extracted database resultset
Let's review the pom.xml for mentionable pointers, 
The spring-native dependency spawned via the GraalVM Native support is responsible for performing build-time static analysis linking and lazy loading (for more details refer the prior post)
Spring Native POM
For producing the native executable via the maven build we rely on spring-aot-maven-plugin and the build profile native with native-maven-plugin to be executed as a build goal during the maven package phase
Spring AOT Maven plugin
Spring Native Profile
Now we define a rest controller with endpoint v1/petclinic to perform a GET REST HTTP call on the firstname passed as a variable to the URL and return the Owner domain model in JSON by default
Spring Rest Controller
With the application code ready, let's compile and create an executable for the windows native platform (for which we would require Virtual Studio Build Tools Utility - C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build>vcvars64.bat).
Then we can run the maven build for the native profile to generate the executable as below,
mvn -f 'C:/Users/Vivek/Downloads/spring-native-demo' -Pnative -DskipTests clean install
Virtual Studio Build Tool
It might take a while to complete and assemble a native executable, however, it is worth the wait compensated by the minimal startup time on the executable
Native Executable
On more of how-to details on the native executable refer here
The final project structure with the generated executable artifacts residing in the /target directory as below,
Project Structure
With the prime focus on the *.exe file formats on the /target directory of the project let's run to validate the startup time of the precompiled application executable (in this case spring-native-demo.exe)
Running the native executable
This was a instantaneous startup with 0.571 seconds.
Time for cross validating the results, let's hit the exposed REST HTTP endpoint from a browser (http://localhost:8080/v1/petclinic/Jeff) and viola!
We get the JSON domain model response.
Querying endpoint
Now let's reconcile with the database for the provided firstname Jeff (here lookup for the firstname and it matches with the above JSON REST HTTP GET response)
Database Owners Details
With this shippable executable, we can deploy it directly onto the platform without requiring to have a JVM, also with the benefit of instantaneous startup, performance, scaling, and garbage collection.
The complete source code can be found on GitHub (https://github.com/raxb/spring-native-demo)

Spring on JDK 11 - Java HotSpot JVM

Let's evaluate the application startup from JDK11 with HotSpot JVM, (Traditional Approach)
HotSpot JVM 11
Now update the project module setting to use JDK 11,
Project Module Settings JVM 11
mvn -f 'C:/Users/Vivek/Downloads/spring-native-demo' spring-boot:run
On running the application with the above maven command let's focus on the startup time of the Spring Boot artifact (spring-native-demo-0.0.1-SNAPSHOT.jar) and the results are 4.463 seconds (compared to native startup 0.571 seconds) giving a boost of ~8X times.
Spring Boot Startup

Platform-specific native executables from Graal VMs Native Image with AOT compilation brings evidently a significant gain to application startup and performance, in turn opening up unexplored domains of prospects to be studied and experimented further.

References


Disclaimer: 
This is a personal blog. Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated. Any views or opinions are not intended to malign any religion, ethnic group, club, organization, company, or individual. All content provided on this blog is for informational purposes only. The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.
Downloadable Files and ImagesAny downloadable file, including but not limited to pdfs, docs, jpegs, pngs, is provided at the user’s own risk. The owner will not be liable for any losses, injuries, or damages resulting from a corrupted or damaged file.
  • Comments are welcome. However, the blog owner reserves the right to edit or delete any comments submitted to this blog without notice due to :
  • Comments deemed to be spam or questionable spam.
  • Comments including profanity.
  • Comments containing language or concepts that could be deemed offensive.
  • Comments containing hate speech, credible threats, or direct attacks on an individual or group.
The blog owner is not responsible for the content in the comments. This blog disclaimer is subject to change at any time.

Comments

Popular posts from this blog

Tech Conversant Weekly Jul 03 - Jul 15

Topic: General                                                                                                                                              Level: All Welcome to the world of cutting-edge technology! Every bi-week, we bring you the latest and most incredible advancements in the tech industry that are sure to leave you feeling inspired and empowered. Stay ahead of the game and be the first to know about the newest innovations shaping our world. Discover new ways to improve your daily life, become more efficient, and enjoy new experiences. This time, we've...

Tech Conversant Weekly Jun 19 - Jul 01

Topic: General                                                                                                                                              Level: All Welcome to the world of cutting-edge technology! Every bi-week, we bring you the latest and most incredible advancements in the tech industry that are sure to leave you feeling inspired and empowered. Stay ahead of the game and be the first to know about the newest innovations shaping our world. Discover new ways to improve your daily life, become more efficient, and enjoy new experiences. This time, we've...

Microservices - Design Patterns

Topic: Software Design                                                                                                        Level: Intermediate Microservices - What? Microservice is a software design methodology, delegated to perform an isolated decoupled single functionality (following the Single-Responsibility Principle from object-oriented SOLID design principles).  Moreover, microservices by design, are decoupled making it easy to develop, test, maintain, deploy, configure, monitor and scale modules independently. Microservices - Why? Having one microservice would not be helpful without it being able to interact with other microservices, to aid in bringing an end-to-end b...