Fundamentals of Micronaut and its features

Young Asian software developer working over new program
Reading Time: 5 minutes
Micronaut Logo

In this blog, we will discuss Micronaut in context of what and why, and we will also see how to create an application from Micronaut launch.

Before taking a deep dive into Micronaut, let us get familiar with some of the associated terminologies. Having this information will help us better understand it.

Ahead of time compilation(AOT) – It is the pre-computation of application code using closed-world static analysis. A fancy way of saying, do more at compile time and less at run time.

Reflection – Reflection in Java is an API (Application Programming Interface) that analyzes or changes classes, methods, and interfaces during runtime. It involves examining or making changes to the run-time behavior of a class.

What is Micronaut?

The Micronaut framework is designed for building lightweight, modular applications, and microservices in the cloud. It’s an open-source JVM-based framework tailored for building and testing low-memory microservices, serverless applications, and message-driven microservices based on cloud architecture.

Micronaut uses ahead-of-time (AOT) compilation to pre-compute the application requirements at compile time. As a result, significantly less memory is consumed, startup times are shortened, and reflection is eliminated from the framework infrastructure.

In contrast to other frameworks, Micronaut does not depend on the size of the application’s codebase for startup time and memory consumption. This is because it analyzes metadata and builds its dependency injection at compilation time instead of runtime, which makes integration testing faster and easier.

A unique characteristic of Micronaut is that it does not modify byte code during compilation time, and it removes all reflection levels. Thus, maximum speed can be achieved at runtime.

Why Micronaut?

Micronaut incorporates existing microservices patterns that developers rely on, helping to eliminate the need to piece together separate tools and frameworks.

Features that make it so well suited for microservices.

  • Support for frameworks that implement the Reactive Streams standard.
  • Support for messaging systems like Kafka, RabbitMQ, MQTT, JMS, and Nats.io.
  • Supports serverless functions for cloud providers like AWS Lambda, Oracle Functions, Google Cloud Functions, and Azure Functions.
  • Supports OpenAPI documentation by creating a YAML file at compilation time.
  • GraalVM readiness to further reduce startup time. 

Pros and Cons of Micronaut

Pros

  • Cloud Native framework for web applications and microservices
  • Compile time reflection
  • Minimal cold startup time and memory use
  • Reactive stack
  • Polyglot framework (Supports Java, Kotlin, Groovy)
  • Minimal use of proxies
  • No runtime bytecode generation
  • Easy unit testing

Cons

  • Smaller community

How to create a Micronaut application?

There are multiple ways to create an application and the fastest way without installing anything is to go to the micronaut.io homepage.

On the top right corner, there is a button with the text LAUNCH. 

micronaut.io homepage

Upon clicking this button, a new page opens where the application can be configured.

Micronaut Launch page

This page is called Micronaut Launch. It provides a lot of settings for creating an application.

It is possible to define multiple application types like a command line, serverless, gRPC, and messaging-driven application. Select the standard Micronaut application type.

Micronaut Launch page

Then select Java version 17.

Micronaut Launch page

Next specify name and base package. According to Java package guidelines, the base package name should be the domain name in reverse order.

Micronaut Launch page
Choose Micronaut version 3.7.2. Then select Java as the programming language. As build tools, Gradle and Maven can be used. Ensure that Maven is selected. Lastly, select JUnit as the test framework.
Micronaut Launch page

There is a line of buttons at the bottom.

Micronaut Launch page

The leftmost button is called “FEATURES”. On clicking it, a pop-up opens that lists all the modules Micronaut can support.

Micronaut Launch page

Now search and select netty-server as the runtime.

Micronaut Launch page
Micronaut Launch page

Before we generate the project, we can inspect the diff, which is quite handy when adding new features. For now, click “PREVIEW” as we are creating a new project.

Micronaut Launch page

Clicking the “PREVIEW” button allows viewing the source structure. The application has a standard Java structure that includes a pom.xml, a maven wrapper, and a source folder.

To generate the project, close this preview and press the “GENERATE PROJECT” button. It is possible to push a project directly to GitHub, download as a zip and use some commands to generate a project. Click on Download Zip to download the project as a zip file. Now unzip the zip file and open it in IntelliJ.

Micronaut Launch page

There is a Application class containing the Java main method. This class contains the static Micronaut.run method, which is the starting point for the application.

Application Image

For configuring the application, there is an application.yml file.

Application Image

By default, Micronaut includes the logback logging implementation in the logback.xml file.

Application Image

There is also DemoAppTest class which contains one test method asserting that the application can start up.

Application Image

Run the test case to see if that works.

Application Image

The log output indicates the use of a test environment. By the way, Micronaut has built in support for different environments, which is quite nice.

There is also the micronaut-cli.yml file, which uses Micronaut CLI along with it. It’s meta information when using CLI commands.

Application Image

And as its a Maven project, so there is a pom.xml file. This file contains all the dependencies.

Application Image
Now head back to the Application class and run the application. The console output shows the Micronaut banner, the version, and the log that startup took 3473 milliseconds.
Application Image

We can also create an application through Micronaut CLI. For more information on creating an application through the CLI, click here.

References

To know more about Micronaut, you can visit this link.

Click here to learn more about reflection in Java.

To read more tech blogs, feel free to visit Knoldus Blogs.

Written by 

Akshat Mathur is a Software Consultant at Knoldus Inc. He has worked on Java, Angular 11, and Spring Boot for more than three years. Learning is his passion and solving problems is his forte.