Presentation by Micronaut creator of the Ahead of Time (AOT) compilation features in Micronaut given at Devnexus 2019 including a demo on how to use AOT to generate Avro schemas for Kafka.
Example code can be found at https://github.com/micronaut-projects/presentations/tree/master/demos/devnexus2019/aotdemos
2. About Me
• Graeme Rocher
• Creator of Grails and Micronaut
• Principal Engineer at Object Computing
• Oracle Groundbreaker Award Winner
http://github.com/graemerocher
https://twitter.com/graemerocher
3. Agenda
• Introduction to Micronaut and AOT
• Micronaut and GraalVM
• Micronaut AOT Features
• Demos!
4. Micronaut and AOT
• What separates Micronaut from
other frameworks
• Majority of framework logic
computed at compilation time
• Eliminates
• Reflection
• Runtime Proxies
• Dynamic Class Loading
• Type Erasure Problems
5. Why AOT
Compilation?
• The more you do at compilation
time, the less you have to do at
runtime
• The less you have to do at runtime
means:
• Faster startup
• Less memory consumption
• Better performance
6. Micronaut and
GraalVM and AOT
• A New Universal Virtual Machine
from Oracle
• Features a native-image Tool
• Converts Java -> native machine
code using AOT
• Works well with Micronaut
http://www.graalvm.org
7. Micronaut AOT API
• Micronaut AST (Abstract Syntax Tree)
• Package
io.micronaut.inject.ast
• TypeElementVisitor API
• Allows Visiting AST
• Works with all supported languages
(Java, Groovy and Kotlin)
9. TypeElementVisitor API
Visits AST Nodes
public class AvroVisitor
implements TypeElementVisitor<AvroEntity, Object> {
...
}
• The above only visits classes annotated with @AvroEntity
• Use visit* methods to dictate which parts of the class to
visit
10. ClassElement API
Represents a Class
void visitClass(ClassElement element,
VisitorContext context) {
// process type
}
• Provides API to inspect annotations,
resolve properties, handle generic types etc.
11. Element Types
• ClassElement - represents a class
• FieldElement - a field of a class
• PropertyElement - a JavaBean property
• MethodElement - a method
• ParameterElement - a parameter to a method
12. Generating Files
Use finish and VisitorContext
void finish(VisitorContext context) {
context.visitMetaInfFile("example.avro").ifPresent(file -> {
try (Writer w = file.openWriter()) {
// write file
}
});
}
• VisitorContext abstracts target directory etc.
13. Reporting Errors
Use fail and VisitorContext
void visitMethod(MethodElement element,
VisitorContext context) {
if (!element.getReturnType().isPrimitive()) {
context.fail("Only primitive return types supported", element);
}
}
• By passing in the element you generate a nice error
14. Micronaut AOT Use Cases
• Pre-computing serialization formats
• Avro, protobuf, JavaScript etc.
• Documentation generation
• Swagger, Custom docs etc.
• Advanced - Byte Code generation
• Example IntrospectedTypeElementVisitor
15. Summary
• Micronaut is optimized for AOT compilation
• Provides an API and toolkit for AOT
• So much can be done by the compiler
• We're just getting started!