Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Micronaut Deep Dive - Devnexus 2019

Presentation given at Devnexus 2019 on Micronaut Internals by Micronaut creator Graeme Rocher including information how how Dependency Injection and Aspect Oriented Programming work in Micronaut.

  • Login to see the comments

Micronaut Deep Dive - Devnexus 2019

  1. 1. MICRONAUT DEEP DIVE Building a Framework for the Future
  2. 2. About Me • Graeme Rocher • Creator of Grails and Micronaut • Principal Engineer at Object Computing • Oracle Groundbreaker Award Winner
  3. 3. Agenda • Introduction to Micronaut • Challenges Facing Java • Micronaut's Approach to Solving The Problems • Demos!
  4. 4. Micronaut is... • A Microservices and Serverless Focused framework (hence the branding) • Also a Complete Application Framework for any type of Application • Dependency Injection, Aspect Oriented Programming (AOP), Configuration Management, Bean Introspection and more..
  5. 5. With Micronaut You Can Build • Microservices • Serverless Applications • Message-Driven Applications with Kafka/Rabbit • CLI Applications • Even Android Applications • Anything with static void main(String..args)
  6. 6. So What is Micronaut? Really? • An Application Framework for the Future • Reflection Free, Runtime Proxy Free, No Dynamic Classloading (in 1.1) • Ahead of Time (AOT) Compilation AOT APIs • ... oh and let's you build Microservices
  7. 7. Why Micronaut? Why Now? • Challenges to using Java in Serverless / Microservices scenarios • Existing Tools and Frameworks Not Optimized for Cold Starts / Low Memory • Go, Node etc. better in this regards • Tim Bray (Amazon/AWS) and others not recommending Java https://youtu.be/IPOvrK3S3gQ?t=1109
  8. 8. Java's Problems • Greatly Exaggerated (Java has been dead forever) • Java can be Fast! (see Android and Micronaut) • However Most Existing Tools are based around • Reflection • Runtime Proxies • Runtime Byte Code Generation (bytebuddy/cglib)
  9. 9. Micronaut and GraalVM • A New Universal Virtual Machine from Oracle • Features a native-image Tool • Converts Java -> native machine code using AOT • Works well with Micronaut • Startup time 20ms and Memory Consumption 18MB! http://www.graalvm.org
  10. 10. Micronaut and GraalVM • GraalVM is cool and a project to keep an eye on • Still in beta and experimental • Micronaut optimizes for GraalVM, but also optimizes for regular Java (what most people use today) http://www.graalvm.org
  11. 11. Java's Problems for Frameworks • Limited Annotation API • Type Erasure • Slow Reflection • Reflective Data Caches • Classpath Scanning • Slow Dynamic Class Loading
  12. 12. DEMOMicronaut Bean Introspection
  13. 13. @Introspected @Introspected class MyBean { private List<String> names; //getter/setter omitted } • AOT Reflection-free replacement for java.beans.Introspector • Set/get bean properties, create instances • Includes AnnotationMetadata
  14. 14. AnnotationMetadata AnnotationMetadata metadata = BeanIntrospection.getIntrospection(MyBean.class) .getAnnotationMetadata(); if (metadata.hasStereotype(SomeAnnotation.class)) { // do stuff } • AOT Computed / Merged Annotation data from class/ interface hierarchy • Includes knowledge of meta-annotations
  15. 15. Argument BeanProperty<MyBean, List> property = introspection.getRequireProperty("names", List.class); // returns an Argument with an underlying type of String Optional<Argument> typeVariable = property.asArgument() .getFirstTypeVariable() • AOT Computed Generic Type information • No type erasure / crazly reflection logic to get the type argument
  16. 16. DEMOMicronaut Dependency Injection
  17. 17. Micronaut Dependency Injection • Precomputes Everything • Annotation Metadata (see AnnotationMetadata interface) • Generic Types (No Type Erasure!) • Class Loading Requirements (No Dynamic Class Loading!)
  18. 18. BeanDefinition ApplicationContext ctx = ApplicationContext.run(); BeanDefinition<MyBean> definition = ctx.getBeanDefinition(MyBean.class); • Contains precompuated AnnotationMetadata and generic type info • Used by ApplicationContext to instantiate beans
  19. 19. BeanDefinition • Bean definitions produced for any @Singleton • Constructor injection used by default • Use @Factory for beans you cannot annotate • Compliant with javax.inject spec and TCK
  20. 20. @ConfigurationProperties Type Safe Configuration @ConfigurationProperties("example") class ExampleConfiguration { // getters/setters omitted private String name; } ApplicationContext context = ApplicationContext.run("example.name":"Demo"); FooConfiguration config = context.getBean(FooConfiguration); assert config.name == 'Demo'
  21. 21. @Requires Conditional Beans Made Easy @Requires(property="example.enabled") @Requires(beans=DataSource.class) @Requires(missingBeans=Example.class) @Singleton class DefaultExampleBean implements Example { ... } ApplicationContext context = ApplicationContext.run("example.enabled":"true") Example example = context.getBean(Example)
  22. 22. @Executable @Scheduled(fixedRate = "5m") // @Scheduled annotated with @Executable void everyFiveMinutes() { messageService.sendMessage("Hello World"); } • Common Stereotype annotation • Identifies where framework invokes your code • Produces reflection-free ExectubleMethod instance
  23. 23. @ExecutableMethodProcessor public class ScheduledMethodProcessor implements ExecutableMethodProcessor<Scheduled> { public void process(BeanDefinition<?> beanDefinition, ExecutableMethod<?, ?> method) { // do stuff with the bean } } • Processes only methods annotated by type argument • In the above case setting up a scheduled job
  24. 24. DEMOMicronaut AOP
  25. 25. @Around @Retryable // @Retryable is annotated with @Around void invokeMe() { // do stuff } • Intercepts a method with a MethodInterceptor • No runtime proxies (compile time proxies) or reflection needed
  26. 26. @Around @Around @Type(DefaultRetryInterceptor.class) public @interface Retryable { // annotation attributes } • Use @Type to specify the implementation • At compilation Micronaut applies the AOP advise
  27. 27. @Introduction @Client("/hello") // @Client is annotated with @Introduction public interface HelloClient { @Get("/") Single hello(); } • Introduction AOP advise implements abstract behaviour • Interfaces or abstract classes
  28. 28. @Introduction @Introduction @Type(HttpClientIntroductionAdvice.class) @Recoverable @Singleton public @interface Client { } • @Type used again to specify implementation • Can apply other annotations as meta annotations
  29. 29. Summary • Micronaut Provides an Awesome Foundation • Sacrifices Compilation Speed to Gain so Much More • Solves Problems with Spring, Jakarta EE and Java in General • Opens the Door to GraalVM Native
  30. 30. Q & A

×