Hello Micronaut

Rik Scarborough Java, Microservices, Testing Leave a Comment

Attention: The following article was published over 4 years ago, and the information provided may be aged or outdated. Please keep that in mind as you read the post.

I have a book on my shelf titled Service-Oriented Architecture by Thomas Erl. This is the fifth printing of the book; printed in June 2006. I’ve probably had it close to that long.

I purchased the book because I have been interested in the Web Service architecture for a very long time. At the time, and ever since, it reminds me of the early architecture applied to Unix applications. Small units of code that solved a single problem and could be chained together to address more significant problems.

Except, in the case of Web Services, it wasn’t that simple.

We entered a world of WSDLs and massive XMLs files. In my opinion, what we had was not easily maintainable nor quick to write. In recent years, development has moved on to pass data around using JSON instead of XML. Services have become discoverable, instead of forcing a contract in a WSDL. With those changes and other excellent decisions, suddenly it became much easier to write Web Services that are maintainable and quick to write.

From some of my previous posts, you can get the idea that I promote the idea of developing maintainable code rapidly. So I was pretty excited when I learned that the same group that was responsible for Grails was working on a similar project for Web Services. Hello, Micronaut.

In this post, I provide an introduction to the Micronaut framework and its features to provide a foundation for you to try it out yourself.

Introducing Micronaut

Micronaut is a microservices framework that does so much more than just Web Services (be still my geeky little heart).

With the rise of Web Services and Containerized applications, a useful framework can be just what you need in your toolbox. Micronaut promises fast startup times, a reduced memory footprint, minimal use of reflection, minimal use of proxies, and easy unit testing (taken from the user guide on the Micronaut website).

The fast startup partially comes from the fact that, whenever it is able, it processes the annotations at compile time as opposed to run-time. I am hoping this also gives us errors caused by how we’ve configured annotations faster, at compile time instead of run-time.

This article is not intended to be a tutorial, but an introduction to Micronaut. One of the nice things about this framework is it’s documented here, where there are also excellent guides.

To give you an idea, here is a quick introduction to working with the Micronaut command line interface. After installing the framework, you can create a Java Web Service with the command:

 mn create-app <project-name> 

This will create a Web Service application in Java. Using the -l flag, you can change the language to Groovy or Kotlin. I’m fond of the Groovy language but have never used Kotlin.

 mn create-app <project-name> -l=groovy 

Setting the language to Groovy also changes the unit test that is created from JUnit in the Java version to Spock. I am hoping in the future there will be an option to choose the test framework separately. Although I would likely always use Spock in Groovy applications, I would like to be able to switch for Java applications.

For me, one of the exciting parts of the Groovy selection is the ability to use GORM. GORM is an Object-Relational Mapper used by Grails. It has always been one of the parts of Grails I’ve enjoyed. I was able to get the GORM objects I created for a Grails projects copied over to a Micronaut application with minimal changes. This allowed me to get a service set up in front of an existing database in a matter of minutes.

Once an application is created, the mn command line application is useful for creating objects for the application. Use the following commands in the project directory:

 mn create-bean <object-name> 

This will create a class that is a Singleton that can be injected into other classes.

 mn create-job <object-name> 

This will create a class that is annotated with Scheduled, and allow it to be scheduled for repeatable tasks.

 mn create-client <object-name> 

This command generates a simple client interface to a Micronaut service.

 mn create-controller <object-name> 

Use this command to create a Controller class.

This creates the executable portion of the code, for instance, the endpoint. It also creates a test class for the Controller. As a proponent of doing unit testing early, I find this to be extremely helpful.

Final Thoughts

Since the makers of Micronaut have provided excellent documentation and tutorials, I have tried to introduce you to the framework in hopes to get you interested enough to try it out. Keyhole Software would be happy to help you implement Web Services of many types, including Micronaut.

This post scratches the surface of what Micronaut can do. I’ll be spending the next few weeks and months digging into the framework and hopefully bring you more insights into what you can accomplish with it. Thanks to OCI for developing a great tool.

Believe in Good Code.

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments