Micronaut
Command line app
We like to refer to Micronaut as a foundation framework. You can build web applications but you can build other kind of apps. In my case, I wanted a command line app. I created a CLI app with micronaut create-cli-app
. To get exchange rates, I run:
$ ./gradlew build
$ java -jar subprojects/cli/build/libs/cli-0.2-all.jar
05:51:20.472 [main] INFO i.m.context.env.DefaultEnvironment - Established active environments: [cli]
2018-12-31,EUR,USD,0.8733624454
2019-01-02,EUR,USD,0.8774238835
2019-01-03,EUR,USD,0.8812125485
2019-01-04,EUR,USD,0.8769622029
2019-01-07,EUR,USD,0.873743993
2019-01-08,EUR,USD,0.8741258741
2019-01-09,EUR,USD,0.8729812309
2019-01-10,EUR,USD,0.8669267447
2019-01-11,EUR,USD,0.8670770832
2019-01-14,EUR,USD,0.8720676725
2019-01-15,EUR,USD,0.8753501401
...
Micronaut CLI apps use picocli and it has nice utilities such as completion candidates and converters which I use to get a nicer help output.
$ java -jar subprojects/cli/build/libs/cli-0.2-all.jar -help
05:54:50.855 [main] INFO i.m.context.env.DefaultEnvironment - Established active environments: [cli]
Usage: exchangeratesapi [-hV] [--base=<base>] [--end=<endAt>]
[--start=<startAt>] [--symbols=<symbols>]...
Obtains Foreign exchange rates from https://exchangeratesapi.io
--base=<base> Base currency. Default Value: USD. Possible values:
AED, AFN, ALL, AMD, ANG, AOA,...
Micronaut and Bean Validation 2.0
Since Micronaut 1.2, Micronaut has built-in support for validating beans that are annotated with javax.validation annotations.
Micronaut’s implementation includes the following benefits:
- Reflection and Runtime Proxy free validation resulting in reduced memory consumption.
- Smaller JAR size since Hibernate Validator adds another 1.4MB.
- Faster startup since Hibernate Validator adds 200ms+ startup overhead.
- Configurability via Annotation Metadata.
- Support for Reactive Bean Validation.
- Support for validating the source AST at compilation time.
- Automatic compatibility with GraalVM native without additional configuration.
I am using the @PastOrPresent Constraint to ensure I don't request rates for an invalid date.
With Spock, checking that the constraints are being validated is easy.
Client Retry
Apart from the CLI app, the project contains a java library that you can import in your Grails or Micronaut applications.
I like the pattern where I have a plain interface which is the type that you inject in your apps and another interface annotated with @Client which extends the previous one and overrides every method providing necessary @Get, @Post annotations.
The library's Declarative Micronaut HTTP client for https://exchangeratesapi.io is configured for you. You will not need to do anything to use it.
However, I took a page out of @mrhaki's book - Using Specific Configuration Properties For HTTP Client - to allow the configuration of the client to be changed via configuration including the retry configuration.
Being able to recover from failure is critical for HTTP clients, and that is where the integrated Retry Advice included as part of Micronaut comes in really handy.
Grails
Creating libraries which can be used in Grails 4 and Micronaut Apps
For Grails 4:
Micronaut is now the parent application context of Grails thus allowing using many Micronaut features including the Micronaut HTTP Client and Kafka Client.
If you don't need to use Grails specific features such as JSON Views or GSPs, we recommend you to create libraries such as this one instead of Grails Plugins.
Using this bean in a Grails 4 app is as simple as using @AutoWired
and the bean type.
Using the bean in a Micronaut app is as easy. You could could use contructor injection or Field injection with @Inject.
Using Micronaut HTTP Client in a Grails 4 Integration Test
Once you get used to use Micronaut HTTP Client in your Grails 4 integration test you will not want to go back.
Gradle
Building the project with Gradle and Kordamp
To build and publish the project I use Gradle and Kordmap Gradle Plugins by @aalmiray
This project provides a set of Gradle plugins that follow an opinionated way to build Java and Groovy projects
I have created two main structures:
samples - Examples of the library being used in Grails / Micronaut apps
subprojects - Java library and command line application.
Kordamp Gradle plugins provide a structure for the project and give me a lot of things out of the box, licensing, bintray publication, jacoco integration, test reporting and reports aggregation ....
To publish the project I just run ./gradlew -Prelease=true bintrayUpload
Comment
This is a different issue. I have just published a small project to get exchange rates and I would like to comment it with you.
If you hate/like the format, let me know by hitting the reply button or via twitter @sdelamo.
Sergio del AmoThe problem
Most of my income/expenses are in USD. However, I pay taxes and keep my books in EUR. Whenever I get payments/charges, I add them to my books in EUR.
As of today, I have a keep Excel file with two sheets. First sheet, Transferwise movements - more about my integration with Transferwise in a different email. Second sheet contains exchange rates. First sheet populates a column by using Excel Lookup with the exchange rate which applies to any payment/charge.
I wanted to automate obtaining the exchange rates.
Enter Foreign exchange rates API. Created by @madisvain, exchange rates API is a free service for current and historical foreign exchange rates published by the European Central Bank.
Sergio del Amo