Despite my Micronaut Java service having a fast startup time, I wanted to see if I could get it starting even fast for production. The short answer was YES.
In order to do this, I used GraalVM - an advanced optimizing compiler with the ability to generate executables - even for a Java application.
First I used https://sdkman.io to install any GraalVM.
sdk use java 20.0.0.r11-grl
Then I started my micronaut service:
CFE-NB-1023:vin-check bporter$ ./gradlew run
> Task :run
OpenJDK 64-Bit Server VM warning: forcing TieredStopAtLevel to full optimization because JVMCI is enabled
13:26:30.738 [main] INFO io.micronaut.runtime.Micronaut -
Startup completed in 760ms. Server Running: http://localhost:5000
It started in 760ms - which still is not bad for a Java service starting using a JIT.
Then I compiled the executable with GraalVM and got it down to 32ms!!
CFE-NB-1023:vin-check bporter$ ./micronaut-graal-app
13:26:40.739 [main] INFO io.micronaut.runtime.Micronaut -
Startup completed in 32ms. Server Running: http://localhost:5000
In order to achieve that, I followed the instructions on this page https://guides.micronaut.io/micronaut-creating-first-graal-app/guide/index.html which basically involved:
gu install native-image
compileOnly "org.graalvm.nativeimage:svm"
annotationProcessor "io.micronaut:micronaut-graal"
native-image.properties
to my resources/META-INF/native-image/example.micronaut/complete-application
directory ./gradlew assemble
native-image --verbose --no-server -cp build/libs/vin-check-0.1-all.jar
./micronaut-graal-app
If you made it this far, you may as well follow me on LinkedIn: Follow Brian Porter