MICRONAUT: A Reflection-Free JVM Framework

Reading Time: 3 minutes

What is Micronaut

Micronaut is an Open Source Java-based application framework. . It is designed to develop and test low-memory Micro-services and Serverless applications with fast startup time and low memory consumption. Micronaut has supports for other platforms like Kotlin, Groovy and Build tools like Maven and Gradle. It was introduced in March-2018 by Object Computing Inc (Creators of the Grails framework).

Micronaut At Technology Radar

Micronaut graduated to Trail level in Thoughtworks Technology Radar Vol.21 in Nov.2019.International IT leader, ThoughtWorks, describes Micronaut as “A very promising entrant to the full-stack framework for the JVM space” in the most recent Languages and Frameworks section of its Technology Radar (TW Tech Radar) publication.

Who is using Micronaut

Following is the list of few top global organizations

https://micronaut.io/

Micronaut Key Features

Micronaut supports some very unique features such as

https://micronaut.io/

AOT Compilation

Unlike other frameworks like SPRING, QUARKAS , Micronaut uses reflection and proxies to perform dependency injection at runtime. Micronaut builds its dependency injection data at compile time. The application startup time and memory consumption aren’t bound to the size of the codebase which results in faster startup time with smaller memory consumption.The GraalVM support helps to further reduce startup times.

Reactive Streams

Micronaut supports reactive streams-based frameworks and also have integration capabilities with many different messaging systems, including Kafka, RabbitMQ , JMS .

Fast Data Access Configurations

Micronaut can be integrated easily with various reactive databases like Mongo DB, Redis, SQL, Hibernate, Neo4J, and Cassandra which results in faster data processing .

Cloud Native Support

Micronaut supports serverless functions for cloud providers like AWS Lambda, Oracle Functions, Google Cloud Functions, and Azure Functions. Have built-in cloud support including discovery services, distributed tracing, and cloud runtime.

https://micronaut.io/

Create a Sample MicroNaut Project





Maven Dependency

<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-core</artifactId>
<version>2.5.6</version>
</dependency>

Create Application class

src/main/java/helloworld/Application.java


package sample;
import io.micronaut.runtime.Micronaut;
public class Application {
  public static void main(String[] args) {
  Micronaut.run(Application.class);
 }
}

Update application.yml for PORT

src/main/resources/application.yml

micronaut:
application:
name: helloworld
server:
port: 8888

Ceate HTTP EndPoint (Controller)

src/test/java/helloworld/HelloController.java

package sample;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.reactivex.Single;

@Controller("/Hello")
public class HelloController {
 @Get("/{name}")
 public class HelloController {
    public Single<String> hello(String name) {
    return Single.just("Hello- World . Good Morning + name + "!");
 }
}
  

Create HTTP Client Test : Test the HelloWorld App.

src/test/java/helloworld/HelloControllerTest.java

package sample;
import io.micronaut.context.ApplicationContext;
import io.micronaut.runtime.server.EmbeddedServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class HelloControllerTest {
  private EmbeddedServer server;
  private HelloControllerClient client;

  @Before
  public void setup() {
  this.server = ApplicationContext.run(EmbeddedServer.class);
  this.client =   
  server.getApplicationContext().getBean(HelloControllerClient.class);
  }

  @Test
  public void testHello(World) {
  String response = client.hello("Knoldus").blockingGet();
  assertEquals(response, "Hello World. Good Morning Knoldus !");
  }

  @After
  public void cleanup() {
  this.server.stop();
  }
}

  

Conclusion

We have gone through a brief introduction of Micronaut and seen how it works using AOT compilation and has support for Cloud-native, Reactive Streams, Message-Driven Microservices, Serverless Functions, Service Discovery, Monitoring-Metrics, Distributed configuration, and much more. The framework already crossed the trial phase at Technology radar and has been successfully implemented by many global-level companies.

References

Written by 

Work as a Vice President, Engineering at Knoldus Inc. A Result-driven Techno-Functional Professional with over 20 years of extensive IT experience in Project Management, IT Delivery Operations, Team Management & Leadership. She is a Sun Certified Enterprise Architect (JAVA) professional with core expertise in Managing and Executing Real-time Trading /Foreign exchange / Capital Market/ Investment Banking projects using Java/J2EE/Cloud technologies.