Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

CDI 2.0 is coming

1.453 visualizaciones

Publicado el

Slides of the talk gave to Devoxx with José Paumard about new features coming in CDI 2. Java SE support, asynchronous event, Interceptor on producers...

Publicado en: Software

CDI 2.0 is coming

  1. 1. Is coming CDI 2.0 @antoine_sd @JosePaumard
  2. 2. @antoine_sd @JosePaumard#Devoxx #CDI2 Agenda  Flashback on CDI 1.0, 1.1 and 1.2  CDI 2.0 status  Gathering feedback for CDI 2.0  CDI 2.0 new features  Questions and Feedback
  3. 3. Previously on CDI
  4. 4. CDI Timeline Dec 2009 June 2013 Apr 2014 Sep 2014 2016
  5. 5. @antoine_sd @JosePaumard#Devoxx #CDI2 CDI 1.0 – December 2009  A typesafe dependency injection mechanism  A well-defined lifecycle for stateful objects  The ability to decorate or to associate interceptors to objects with a typesafe approach  An event notification model  An SPI allowing portable extensions
  6. 6. @antoine_sd @JosePaumard#Devoxx #CDI2 CDI 1.1 – June 2013  CDI is automatically enabled in Java EE  Introspection with bean, events, decorator and interceptor metadata  Ease access to CDI from non CDI code  Work on interceptor for reuse by other Java EE specs  SPI enhancement for portable extensions
  7. 7. @antoine_sd @JosePaumard#Devoxx #CDI2 CDI 1.2 – April 2014  Clarifications in the spec • Lifecycles • Events • Conversation scope  Fix conflict with other JSR 330 frameworks  OSGi support in the API
  8. 8. @antoine_sd @JosePaumard#Devoxx #CDI2 CDI 2.0 started 12 months ago  JSR 365! • First Java EE 8 JSR proposed and voted  Weekly IRC meeting  Regular release of Weld 3.0 Alpha (CDI 2.0 RI)  We have a lot of community momentum  Early Draft was released this summer  Release expected in 2016 (Q2?)
  9. 9. @antoine_sd @JosePaumard#Devoxx #CDI2 EG members  Pete Muir (Red Hat)  Antoine Sabot-Durand (Red Hat)  José Paumard  John Ament  David Currie (IBM)  Anatole Tresch (Credit Suisse)  Antonio Goncalves  Thorben Janssen  Raj. Hegde (JUG Chennai)  Werner Keil  Joseph Snyder (Oracle)  Mark Paluch  Florent Benoit (SERLI)  Mark Struberg  David Blevins (Tomitribe)  George Gastaldi (Red Hat)  Otavio Santana
  10. 10. @antoine_sd @JosePaumard#Devoxx #CDI2 We are open to the community!
  11. 11. @antoine_sd @JosePaumard#Devoxx #CDI2 Gathering feedback for CDI 2.0
  12. 12. @antoine_sd @JosePaumard#Devoxx #CDI2 CDI 2.0 survey 260 participants 20 features to rate
  13. 13. @antoine_sd @JosePaumard#Devoxx #CDI2 1st feature  Asynchronous support for events and method invocation
  14. 14. @antoine_sd @JosePaumard#Devoxx #CDI2 Other top requested features  @Startup for CDI  Bootstraping outside of Java EE  AOP for custom beans  Security support  Observers ordering, better event control  Access to metadata through SPI
  15. 15. CDI 2.0 new features
  16. 16. Java SE support Using CDI outside of the Java EE Container
  17. 17. @antoine_sd @JosePaumard#Devoxx #CDI2 Why that?  To ease the testing of CDI applications  To provide a mean of building new stacks out of Java EE  To boost CDI adoption for Spec working already on Java SE  First step before working on a CDI light
  18. 18. @antoine_sd @JosePaumard#Devoxx #CDI2 Java SE support will start in EDR1  We specified API to boot CDI in Java SE:  Desktop and non Java EE application can now use a standard way to boot CDI public static void main(String... args) { CDIProvider provider = CDI.getCDIProvider(); CDI<Object> cdi = provider.initialize(); // retrieve a bean and do work with it MyBean myBean = cdi.select(MyBean.class).get(); myBean.doWork(); // when done cdi.shutdown(); }
  19. 19. @antoine_sd @JosePaumard#Devoxx #CDI2 What did we do? CDI Specification CDI Core CDI for Java EE CDI for Java SE
  20. 20. @antoine_sd @JosePaumard#Devoxx #CDI2 There’s still work to do  What about built-in contexts activation in Java SE? • RequestScoped, SessionScoped, ConversationScoped…  Answer: we may introduce a new scope • MethodScoped • Living only during method invocation • Activated by an Interceptor • Discussion is in early stage on that
  21. 21. @antoine_sd @JosePaumard#Devoxx #CDI2 There’s still work to do  What about bean discovery in Java SE? • Annotated mode can be very costly • Implicit bean archive is disable now  Answer: we are working on SE enhancement • Take more SE features from weld • Choose bean discovery mode at build time • Adding classes / packages explicitly at build time
  22. 22. Modularity Provide sub specs in CDI (called parts) that can be used independently Each part should have an implementation
  23. 23. @antoine_sd @JosePaumard#Devoxx #CDI2 Why that?  To avoid the “bloated spec” syndrom  Having parts will help CDI adoption  Third party won’t have to implement the whole spec if they don’t want to
  24. 24. @antoine_sd @JosePaumard#Devoxx #CDI2 Full CDI - Events - Normal scopes - Interceptor & Decorator - Advanced SPI Modularity – 2 core parts CDI Light - Basic DI - Producers - Programmatic lookup - Singleton and dependent scopes - Basic SPI for integration
  25. 25. @antoine_sd @JosePaumard#Devoxx #CDI2 Modularity – challenges  Will bring 4 subspec: • CDI light for Java SE • CDI full for Java SE • CDI light for Java EE • CDI full for Java EE  Having an RI and TCK for each part can be an important work
  26. 26. Enhancing events Making a popular feature even more popular!
  27. 27. @antoine_sd @JosePaumard#Devoxx #CDI2 Enhancing Events  CDI events are a very loved feature! For CDI 2.0, we plan to introduce :  Asynchronous events  Events ordering
  28. 28. @antoine_sd @JosePaumard#Devoxx #CDI2 Events in CDI 1.x: patterns  Firing pattern: @Inject Event<Payload> event; public void someBSCriticalBusinessMethod() throws WTFException { event.fire(new Payload()); }
  29. 29. @antoine_sd @JosePaumard#Devoxx #CDI2 Events in CDI 1.x: patterns  Observing pattern:  Supports qualifiers and many other things public void callMe(@Observes Payload payload) { // Do something with the event }
  30. 30. @antoine_sd @JosePaumard#Devoxx #CDI2 CDI 1.x: Sync / Async  Sync / Async is not specified  The immutable status of the payload is not specified  Implementations use a Sync model  The payload is mutated in some implementations / framework  Going async “blindly” might raise problems…
  31. 31. @antoine_sd @JosePaumard#Devoxx #CDI2 Events are sync in CDI 1 Right now:  All the observers are called in the firing thread  In no particular order (at least not specified)  The payload may be mutated
  32. 32. @antoine_sd @JosePaumard#Devoxx #CDI2 Events and contexts Contexts  Two contexts are critical: transactions and HTTP requests / sessions  Events are aware of those contexts  In an all-sync world, everything is fine  But in an async world, we will be in trouble
  33. 33. @antoine_sd @JosePaumard#Devoxx #CDI2 Asynchronous Events  So designing backward compatible async events is more tricky than it looks: 1) A currently sync event should remain sync 2) Going sync / async should be a decision taken from the firing side 3) Being sync should be possible from the observing side
  34. 34. @antoine_sd @JosePaumard#Devoxx #CDI2 Asynchronous Events  Pattern for the firing side: @Inject Event<Payload> event; public void someEvenMoreCriticalBusinessMethod() { event.fireAsync(new Payload()); }
  35. 35. @antoine_sd @JosePaumard#Devoxx #CDI2 Asynchronous Events  Pattern for the observing side: public void callMe(@Observes Payload payload) { // I am called in the firing thread // Whether is was async fired or not } public void callMe(@ObservesAsync Payload payload) { // I am called in another thread }
  36. 36. @antoine_sd @JosePaumard#Devoxx #CDI2 Asynchronous Events  So, in a nutshell callMe( @Observes payload) callMe( @ObservesAsync payload) event .fire(payload) Sync call Not notified event .fireAsync(payload) Not notified Async call
  37. 37. @antoine_sd @JosePaumard#Devoxx #CDI2 What about mutable payloads?  One short answer:  Don’t do it!  Or suffer the full penalty of race conditions!
  38. 38. @antoine_sd @JosePaumard#Devoxx #CDI2 We have some more  Let us come back to this pattern:  1st question: in what thread are the observers going to be called? @Inject Event<Payload> event; public void someOtherCriticalBusinessMethod() { event.fireAsync(new Payload()); }
  39. 39. @antoine_sd @JosePaumard#Devoxx #CDI2 We have some more  Let us come back to this pattern:  2nd question: what if exceptions are thrown by the observers? @Inject Event<Payload> event; public void someOtherCriticalBusinessMethod() { event.fireAsync(new Payload()); }
  40. 40. @antoine_sd @JosePaumard#Devoxx #CDI2 Adding an Executor to fireAsync  What if the observer needs to be called in the GUI thread? @Inject Event<PanelUpdater> event; public void someOtherCriticalBusinessMethod() { event.fireAsync(new PanelUpdater(green), executor); // Of type Executor }
  41. 41. @antoine_sd @JosePaumard#Devoxx #CDI2 Adding an Executor to fireAsync  What if the observer needs to be called in the GUI thread? @Inject Event<PanelUpdater> event; public void someOtherCriticalBusinessMethod() { event.fireAsync(new PanelUpdater(green), SwingUtilities::invokeLater); }
  42. 42. @antoine_sd @JosePaumard#Devoxx #CDI2 Handling exceptions  The firing async is built on the Java 8 async model: CompletionStage @Inject Event<PanelUpdater> event; public void someOtherCriticalBusinessMethod() { CompletionStage<PanelUpdater> stage = event.fireAsync(new PanelUpdater(green), SwingUtilities::invokeLater); }
  43. 43. @antoine_sd @JosePaumard#Devoxx #CDI2 Handling exceptions  Three ways of handling exceptions: Returns a new CompletionStage  That completes when the CS completes  Either with the same result (normal completion)  Or with the transformed exception stage.exceptionaly( // Function exception -> doSomethingNotTooStupidWith(exception));
  44. 44. @antoine_sd @JosePaumard#Devoxx #CDI2 Handling exceptions  Three ways of handling exceptions: Returns a new CompletionStage  That completes when the CS completes  Calls the BiFunction with a null as result or exception  As a bonus: observers can return objects! stage.handle( // BiFunction (result, exception) -> doSomethingWith(result, exception));
  45. 45. @antoine_sd @JosePaumard#Devoxx #CDI2 Handling exceptions  Three ways of handling exceptions:  The returned exception is a FireAsyncException  It holds all the exceptions in the suppressed exception set stage.handle( // BiFunction (result, exception) -> doSomethingWith(result, exception));
  46. 46. @antoine_sd @JosePaumard#Devoxx #CDI2 Handling exceptions  Three ways of handling exceptions: stage.whenComplete( // BiConsumer, also async version (result, exception) -> doSomethingWith(result, exception));
  47. 47. @antoine_sd @JosePaumard#Devoxx #CDI2 Events ordering  Pattern:  Ordering in async… possible but complex public void firstObserver(@Observes @Priority(1) Payload p) {} public void secondObserver(@Observes @Priority(2) Payload p) {}
  48. 48. AOP Enhancement
  49. 49. @antoine_sd @JosePaumard#Devoxx #CDI2 Support AOP on producer  In CDI 1.x you cannot bind an interceptor to a produced bean  When you write:  @Transactional is applied to producer method @Produces @Transactional public MyService produceService() { ... }
  50. 50. @antoine_sd @JosePaumard#Devoxx #CDI2 Support AOP on producer  We are working on an instance builder.  Inspired by the UnManaged class  This builder could take an AnnotatedType to know where adding interceptor bindings
  51. 51. @antoine_sd @JosePaumard#Devoxx #CDI2 BeanInstanceBuilder example @Produces @RequestScoped public MyClass produceTransactionalMyClass() { BeanInstanceBuilder<MyClass> bib = new BeanInstanceBuilder<>(); AnnotatedTypeBuilder<MyClass> atb = new AnnotatedTypeBuilder<>() .readFrom(MyClass.class) .addToMethod(MyClass.class.getMethod("performInTransaction"), new TransactionalLiteral()); return bib.readFromType(atb.build()).build(); }
  52. 52. CDI 2.0 DEMO
  53. 53. Which JSR you’ll use 365 days a year? JSR 365!!
  54. 54. @antoine_sd @JosePaumard#Devoxx #CDI2 CDI 2.0 needs you!! CDI 2.0 specification is open to everyone Mailing list, IRC channel http://cdi-spec.org @cdispec

×