No-Tier Enterprise         Applications with CDILars Röwekamp | open knowledge GmbH                  @mobileLarson        ...
> Cool! JavaEE bietet IoC/DI
> Aber was ändert sich?
> SCHICHT 1TX Grenze            > SCHICHT 2            > SCHICHT 3
Einmal den Customer                 Controller, bitte ;-)                                         Und für mich den        ...
> Was wäre wenn ...? A K T U E L LA U S G E W Ä H L T E R K U N D E
> Oder wenn ...? L   I   S T E M E I N E R E I N K Ä U F E
TX Grenze            > „No-Tier“
J2EE 1.3 (JSR 58):                                           Java EE v5 (JSR 244):  September 2001                        ...
Enterprise JavaBeans                    Life Cycle Mmgt.                     Instance Pooling                       Concur...
CDI:Leichtegichtig       CDI Beans                 Life Cycle Mmgt.                      Scoping                      ... ...
CDI:Leichtegichtig        CDI Konzepte                      Typensicher DI                   (Stereotypes, Qulifier)      ...
> DI/IoC lite                  „Java EE without EJB“                > DI/IoC advanced                  LifeCyle Management...
Base Features
Base Features> @Inject> @Named> Sterotypes> Alternatives
Base Features  > Sope Singleton  > Cleanup  > Request  > Conversation  > Session  > Application  > Dependant*  > Singleton...
Base Features
Base Features
Base Features> Events> Observer> Interceptors
Base Features> Qualifier> Stereotypes> Alternatives
> Was haben wir gewonnen?
> SCHICHT 1> SCHICHT 2> SCHICHT 3
> SCHICHT 1> SCHICHT 2> SCHICHT 3
CDI Managed Beanimport javax.inject.Named;                „hello“ für ELimport javax.inject.Inject;import javax.enterprise...
CDI Managed Bean                                      Simple POJOpublic class Greeter {public String greet(Sting name) {  ...
CDI Managed Bean                                           Stateless EJB@Statelesspublic class GreeterEJB                 ...
CDI        Cool, aber             geht daMehrwert   nicht mehr?
FACHLICHE Injektion       statt „nur“Infrastruture Injection
eCustomer - Beispiel
CDI Refactoring> CDI Wiring> CDI Current User> CDI Beans only> CDI Style Views> CDI Events
CDI Refactoring> CDI Wiring> CDI Current User> CDI Beans only> CDI Style Views> CDI Events
CDI Refactoring> CDI Wiring> CDI Current User> CDI Beans only> CDI Style Views> CDI Events
eCustomer - CDI Current User> Producer Methods & Fields> „Factory Method“ Pattern für Objekte> @Produces als Mittel zum Zw...
eCustomer - CDI Current User                                  AuthenticationController@Named@SessionScopedpublic class Aut...
eCustomer - CDI Current User@Named@SessionScopedpublic class AuthenticationController                    implements Serial...
eCustomer - CDI Current User                                        Self-made Qualifierpackage de.openknowledge.qualifier;...
eCustomer - CDI Current User                                               JSF CustomerBean@javax.inject.Named@javax.conte...
Use Case „Kunde bearbeiten“                                     CustomerService EJB@Statelesspublic class CustomerServiceE...
CDI Refactoring> CDI Wiring> CDI Current User> CDI Beans only> CDI Style Views> CDI Events
eCustomer - CDI Beans only> Injizierbare CDI Ressourcen> „normale“ Java Klassen, optional mit @Named oder @Qualifier marki...
eCustomer - CDI Beans only                                        CustomerService CDI@Stateless// no annotation required!p...
eCustomer - CDI Beans only                                        CustomerService CDI@Stateless// no annotation required!p...
eCustomer - CDI Beans only                                        CustomerService CDI// simple pojo// no annotation requir...
CDIInterceptors
eCustomer - CDI Beans only?   @InterceptorBinding    @Retention(RetentionPolicy.RUNTIME)    @Target({ElementType.METHOD, E...
eCustomer - CDI Beans only                                            Transactional Interceptor*@Transactional@Interceptor...
eCustomer - CDI Beans only                                         Transactional Interceptor*@Transactional@Interceptorpub...
eCustomer - CDI Beans only                                          CustomerService CDI// no annotation required!public cl...
> SCHICHT 1TX Grenze            > SCHICHT 2            > SCHICHT 3
TX Grenze            > SCHICHT 1            > SCHICHT 2            > SCHICHT 3
eCustomer - CDI Beans only                                             CustomerBean CDI@Named@SessionScopedpublic class Cu...
CDI Refactoring> CDI Wiring> CDI Current User> CDI Beans only> CDI Style Views> CDI Events
eCustomer - CDI Style Views> CDI Conversations> @ConversationScoped> conversation.begin() / .end()> CDI „Backing Beans“> @...
eCustomer - CDI Style Views                                         CustomerBean (1/2)@javax.inject.Named@javax.context.en...
eCustomer - CDI Style Views                                          CustomerBean (2/2)@javax.inject.Named@javax.context.e...
eCustomer - CDI Style Views<html ...>                       Infrastructure in der View!  <h:body>    <h:form>    Vorname: ...
eCustomer - CDI Style Views<html ...>                       Fachlichkeit in der View!  <h:body>    <h:form>    Vorname: <h...
eCustomer - CDI Style Views                                          CustomerBean@javax.inject.Named@javax.context.enterpi...
CDI Refactoring> CDI Wiring> CDI Current User> CDI Beans only> CDI Style Views> CDI Events
eCustomer - CDI Events> Java EE Observer Pattern, inkl. ...> Event Object & Event Producer> Observer Method> „besondere“ E...
eCustomer - CDI Events                                        „User created!“„Ah, interessant.“                           ...
eCustomer - CDI Events> Ok, aber ...> Wie sieht ein solches Event aus?> Und wie fange ich es?> Und vor allem: wie löse ich...
eCustomer - CDI Events                            Customer Created Event (1/3) public class CustomerCreatedEvent {    priv...
eCustomer - CDI Events                              Customer Created Event (2/3)  public void sendWelcomeMail(            ...
eCustomer - CDI Events                             Customer Created Event (3/3) @Inject private Event<CustomerCreatedEvent...
eCustomer - CDI Events„Wird das Event Objecttatsächlich benötigt oder gibtes eventuell nochAlternativen?“        „Nicht un...
eCustomer - CDI Events                         Qualified CDI Event (1/3)@Qualifier@Target({TYPE, METHOD, PARAMETER, FIELD}...
eCustomer - CDI Events                           Qualified CDI Event - Alternative (2/3)  public void sendWelcomeMail(    ...
eCustomer - CDI Events                          Qualified CDI Event - Alternative (3/3)  @Inject @Created  private Event<C...
eCustomer - CDI Events„Wow! Observer mit Qulifiern -das muss ich erst einmalsacken lassen!“    „Und das ist erst die Spitz...
eCustomer - CDI Events                          Conditional Observer Methods// Conditional Observer Method that takes// - ...
TX Grenze            > „No-Tier“
Fazit:X
CDI FazitCDI ermöglicht ...“Java Enterprise Development without EJB -and without Spring“CDI bietet dafür ...typesafe und s...
@mobileLarson               @_openknowledgefacebook.com/openknowledgeGibt es nochFragen?Dann los ...
No Tier Enterprise Applications with CDI
Upcoming SlideShare
Loading in …5
×

No Tier Enterprise Applications with CDI

663 views
587 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
663
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Typensicheres DI: Types, Qualifier Loose Koppelung: Events, Interceptors, Decorators Sichtbarkeiten: Context, Scopes, Lifecycle Awareness
  • Typensicheres DI: Types, Qualifier Loose Koppelung: Events, Interceptors, Decorators Sichtbarkeiten: Context, Scopes, Lifecycle Awareness
  • WICHTIG: Use case erläutern!
  • Erläutern wofür der CurrentUser benötigt wird.
  • Aufruf via: @Inject @Current User user;
  • Achtung: Scope ist Default dependent!
  • Zwei Steps: 1. Audit Logik aus dem View Controller rausziehen 2. (next Slide) aktuellen SystemUser über Producer bekommen.
  • Injection in EJB! war vorher nicht möglich, da authenticationController = JSF ManagedBean
  • Audit Info
  • Audit Info
  • Audit Info
  • Audit Info
  • Audit Info
  • Audit Info
  • Audit Info
  • Audit Info
  • Audit Info
  • Wie sieht das Event aus?
  • Wie fange ich es?
  • Und vor allem: Wie erzeuge ich es
  • Audit Info
  • Audit Info
  • Audit Info
  • No Tier Enterprise Applications with CDI

    1. 1. No-Tier Enterprise Applications with CDILars Röwekamp | open knowledge GmbH @mobileLarson @_openknowledge
    2. 2. > Cool! JavaEE bietet IoC/DI
    3. 3. > Aber was ändert sich?
    4. 4. > SCHICHT 1TX Grenze > SCHICHT 2 > SCHICHT 3
    5. 5. Einmal den Customer Controller, bitte ;-) Und für mich den Customer Service, bitte! Ok, dann brauch ich nen Customer Entity mit der internene ID 147122, aber flott!> UseCase: ausgewählten Kunden bearbeiten
    6. 6. > Was wäre wenn ...? A K T U E L LA U S G E W Ä H L T E R K U N D E
    7. 7. > Oder wenn ...? L I S T E M E I N E R E I N K Ä U F E
    8. 8. TX Grenze > „No-Tier“
    9. 9. J2EE 1.3 (JSR 58): Java EE v5 (JSR 244): September 2001 Mai 2006  Servlet 2.3  Servlet 2.5  JSP 1.2 Spring V1 Spring V3  JSP 2.1 / JSF 1.2  EJB 2.0 Juni 2003 Dezember 2009  EJB 3.001 / 02 03 / 04 05 / 06 07 / 08 09 / 10„Spring“ J2EE 1.4 (JSR 151): Java EE v6 (JSR 316):Oktober 2002 November 2003 Dezember 2009  Servlet 2.4  Servlet 3.0  JSP 2.0  JSP 2.1 MR / JSF 2.0 Spring V2  EJB 2.1  EJB 3.1 Oktober 2006  JPA 2.0  CDI 1.0
    10. 10. Enterprise JavaBeans Life Cycle Mmgt. Instance Pooling Concurrency Transaction Security Scoping ...EJBs:Schwergewichtig
    11. 11. CDI:Leichtegichtig CDI Beans Life Cycle Mmgt. Scoping ... AND ... *** Extensibility ***
    12. 12. CDI:Leichtegichtig CDI Konzepte Typensicher DI (Stereotypes, Qulifier) Loose Koopelung (Events, Interceptors) Sichtbarkeiten (Sopes, LifeCycle ) „FACHLICHE INJECTION“
    13. 13. > DI/IoC lite „Java EE without EJB“ > DI/IoC advanced LifeCyle Management und Scoping > DI/IoC eXtreme Typensicherheit und lose Koppelung > DI/IoC open Extension-MechanismusCDI Kickstart
    14. 14. Base Features
    15. 15. Base Features> @Inject> @Named> Sterotypes> Alternatives
    16. 16. Base Features > Sope Singleton > Cleanup > Request > Conversation > Session > Application > Dependant* > Singleton**Pseudo-Scope
    17. 17. Base Features
    18. 18. Base Features
    19. 19. Base Features> Events> Observer> Interceptors
    20. 20. Base Features> Qualifier> Stereotypes> Alternatives
    21. 21. > Was haben wir gewonnen?
    22. 22. > SCHICHT 1> SCHICHT 2> SCHICHT 3
    23. 23. > SCHICHT 1> SCHICHT 2> SCHICHT 3
    24. 24. CDI Managed Beanimport javax.inject.Named; „hello“ für ELimport javax.inject.Inject;import javax.enterprise.context.RequestScoped;@Named(“hello“)@RequestScoped gültig für Requestpublic class HelloBean {@Inject Greeter greeter; Injection Pointprivate String name;public String getGreeting() { return greeter.greet(name);}// getter and setter for name attribute...}
    25. 25. CDI Managed Bean Simple POJOpublic class Greeter {public String greet(Sting name) { if (name != null) { return “Hello, “ + name; } else { return “Hello, stranger!“; }}}
    26. 26. CDI Managed Bean Stateless EJB@Statelesspublic class GreeterEJB implements Greeter {private static final Log logger = ...@Inject @Current CrmUser crmUser;public String greet(Sting name) { logger.debug(“Who: “ + crmUser.getName()); if (name != null) { ... // create & return greeting }}}
    27. 27. CDI Cool, aber geht daMehrwert nicht mehr?
    28. 28. FACHLICHE Injektion statt „nur“Infrastruture Injection
    29. 29. eCustomer - Beispiel
    30. 30. CDI Refactoring> CDI Wiring> CDI Current User> CDI Beans only> CDI Style Views> CDI Events
    31. 31. CDI Refactoring> CDI Wiring> CDI Current User> CDI Beans only> CDI Style Views> CDI Events
    32. 32. CDI Refactoring> CDI Wiring> CDI Current User> CDI Beans only> CDI Style Views> CDI Events
    33. 33. eCustomer - CDI Current User> Producer Methods & Fields> „Factory Method“ Pattern für Objekte> @Produces als Mittel zum Zweck> @Qualifier als Mittel zur Qualifizierung> Ermöglicht nicht nur Infrastruktur sondern vor allem auch fachliche Injection.
    34. 34. eCustomer - CDI Current User AuthenticationController@Named@SessionScopedpublic class AuthenticationController implements Serializable { private User authenticatedUser; public String authenticate() {...} @Produces @Current public User getAuthenticatedUser() {...} ...}
    35. 35. eCustomer - CDI Current User@Named@SessionScopedpublic class AuthenticationController implements Serializable { private User authenticatedUser; public String authenticate() {...} @Produces @RequestScoped @Current public User getAuthenticatedUser() {...} ...} @Inject @Current User user;
    36. 36. eCustomer - CDI Current User Self-made Qualifierpackage de.openknowledge.qualifier;import ...@Qualifier@Target({TYPE, METHOD, PARAMETER, FIELD})@Retention(RUNTIME)public @interface Current{}
    37. 37. eCustomer - CDI Current User JSF CustomerBean@javax.inject.Named@javax.context.enterpise.SessionScopedpublic class CustomerBean implements Serializable { // @javax.inject.Inject // private AuthenticationController authController; @javax.inject.Inject private CustomerService customerService; public String update() { // User currentUser =authController.getAuthenticatedUser(); // currentCustomer.updateAuditInformation(currentUser); customerService.updateCustomer(currentCustomer); return Outcome.SUCCESS; Audit „Bereinigung“ } ...}
    38. 38. Use Case „Kunde bearbeiten“ CustomerService EJB@Statelesspublic class CustomerServiceEJB implements CustomerService { @Inject @Current private User currentUser; @PersistenceContext private EntityManager em; // updates the customer - needs a transaction public Customer updateCustomer(Customer customer) { customer.updateAuditInformation(currentUser); return em.merge(customer); }}
    39. 39. CDI Refactoring> CDI Wiring> CDI Current User> CDI Beans only> CDI Style Views> CDI Events
    40. 40. eCustomer - CDI Beans only> Injizierbare CDI Ressourcen> „normale“ Java Klassen, optional mit @Named oder @Qualifier markiert> EJBs, wie Stateless, Stateful, Singleton> Sonstige Java EE Ressourcen, wie PersistenceContext, UserTransaction
    41. 41. eCustomer - CDI Beans only CustomerService CDI@Stateless// no annotation required!public class CustomerServiceBeanEJB implements CustomerService { @Inject @Current private User currentUser; @PersistenceContext private EntityManager em; // updates the customer - needs a transaction public Customer updateCustomer(Customer customer) { customer.updateAuditInformation(currentUser); return em.merge(customer); } ...}
    42. 42. eCustomer - CDI Beans only CustomerService CDI@Stateless// no annotation required!public class CustomerServiceBeanEJB implements CustomerService { @Inject @Current private User currentUser; @PersistenceContext Und Transaktionen?! private EntityManager em; // updates the customer - needs a transaction public Customer updateCustomer(Customer customer) { customer.updateAuditInformation(currentUser); return em.merge(customer); } Bauen wir uns selbst ... ...}
    43. 43. eCustomer - CDI Beans only CustomerService CDI// simple pojo// no annotation required!public class CustomerServicBean implements CustomerService { @Inject @Current private User currentUser; @PersistenceContext private EntityManager em; @Transactional public Customer updateCustomer(Customer customer) { customer.updateAuditInformation(currentUser); return em.merge(customer); } ...}
    44. 44. CDIInterceptors
    45. 45. eCustomer - CDI Beans only? @InterceptorBinding @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) public @interface Transactional { @Nonbinding public TransactionalType value() default TransactionalType.REQUIRED; } Transactional Annotation
    46. 46. eCustomer - CDI Beans only Transactional Interceptor*@Transactional@Interceptorpublic class TransactionAdvice { @Inject private UserTransaction utx; @AroundInvoke public Object applyTransaction( InvocationContext ic) throws Throwable { ... // 1. access @Transactional, // if needed ... // 2. implement utx.begin() ic.proceed(); // 3. call original method ... // 4. implement utx.commit() }} *XML registration omitted
    47. 47. eCustomer - CDI Beans only Transactional Interceptor*@Transactional@Interceptorpublic class TransactionAdvice { @Inject private UserTransaction utx; @AroundInvoke public Object applyTransaction( InvocationContext ic) throws Throwable { Transactional tx = ic.getMethod(). getAnnotation(Transactional.class); ... // 2. implement utx.begin() ic.proceed(); // 3. call original method ... // 4. implement utx.commit() }} *XML registration omitted
    48. 48. eCustomer - CDI Beans only CustomerService CDI// no annotation required!public class CustomerServiceBean implements CustomerService { @Inject @Current private User currentUser; @PersistenceContext private EntityManager em; @de.openknowledge.qualifier.Transactional public Customer updateCustomer(Customer customer) { customer.updateAuditInformation(currentUser); return em.merge(customer); inkl. Transaktion } ...}
    49. 49. > SCHICHT 1TX Grenze > SCHICHT 2 > SCHICHT 3
    50. 50. TX Grenze > SCHICHT 1 > SCHICHT 2 > SCHICHT 3
    51. 51. eCustomer - CDI Beans only CustomerBean CDI@Named@SessionScopedpublic class CustomerBean implements Serializable { @Inject private CustomerService customerService; @de.openknowledge.qualifier.Transactional public String update() { customerService.updateCustomer(currentCustomer); ... // some additional use case “tx“ related work return Outcome.SUCCESS; } ... inkl. „fachliche“ Transaktion}
    52. 52. CDI Refactoring> CDI Wiring> CDI Current User> CDI Beans only> CDI Style Views> CDI Events
    53. 53. eCustomer - CDI Style Views> CDI Conversations> @ConversationScoped> conversation.begin() / .end()> CDI „Backing Beans“> @Produces in Kombination mit @Named> „self-made“ Qualifier
    54. 54. eCustomer - CDI Style Views CustomerBean (1/2)@javax.inject.Named@javax.context.enterpise.ConversationScopedpublic class CustomerBean implements Serializable { private Customer customer; @javax.inject.Inject private Conversation conversation; public String selectCustomer(Customer customer) { conversation.begin(); this.customer = customer; return ... ; } ...}
    55. 55. eCustomer - CDI Style Views CustomerBean (2/2)@javax.inject.Named@javax.context.enterpise.ConversationScopedpublic class CustomerBean implements Serializable { private Customer customer; @javax.inject.Inject private Conversation conversation; public String updateCustomer() { ... conversation.end(); return ... ; } Conversation endet mit Request ...}
    56. 56. eCustomer - CDI Style Views<html ...> Infrastructure in der View! <h:body> <h:form> Vorname: <h:inputText value=“#{customerBean.customer.firstname}"/> Name: <h:inputText value=“#{customerBean.customer.lastname}"/> </h:form> </h:body></html>
    57. 57. eCustomer - CDI Style Views<html ...> Fachlichkeit in der View! <h:body> <h:form> Vorname: <h:inputText value=“#{selectedCustomer.firstname}"/> Name: <h:inputText value=“#{selectedCustomer.lastname}"/> </h:form> </h:body></html>
    58. 58. eCustomer - CDI Style Views CustomerBean@javax.inject.Named@javax.context.enterpise.ConversationScopedpublic class CustomerBean implements Serializable { private Customer customer; @javax.inject.Produces @javax.inject.Named(“selectedCustomer“) @de.openknowledge.qualifier.Selected public Customer getSelected { return customer; } ...}
    59. 59. CDI Refactoring> CDI Wiring> CDI Current User> CDI Beans only> CDI Style Views> CDI Events
    60. 60. eCustomer - CDI Events> Java EE Observer Pattern, inkl. ...> Event Object & Event Producer> Observer Method> „besondere“ Eigenschaften> schichtenneutral auch für POJOs> (ggf.) transaktionsgebunden> synchrone Interaktion
    61. 61. eCustomer - CDI Events „User created!“„Ah, interessant.“ „Und ich auch!“ „Finde ich!“
    62. 62. eCustomer - CDI Events> Ok, aber ...> Wie sieht ein solches Event aus?> Und wie fange ich es?> Und vor allem: wie löse ich es aus?
    63. 63. eCustomer - CDI Events Customer Created Event (1/3) public class CustomerCreatedEvent { private Customer customer; public CustomerCreatedEvent(Customer customer) { this.customer = customer; } public Customer getCustomer() { ... }}„Wie sieht ein solches Event aus?“
    64. 64. eCustomer - CDI Events Customer Created Event (2/3) public void sendWelcomeMail( @Observes CustomerCreatedEvent event) { Customer customer = event.getCustomer(); ... }„Und wie fange ich es?“
    65. 65. eCustomer - CDI Events Customer Created Event (3/3) @Inject private Event<CustomerCreatedEvent> eventSource; public String createCustomer() { Customer customer = ...; eventSource.fire( new CustomerCreatedEvent(customer) ); ... }„Und vor allem: Wie löse ich es aus?“
    66. 66. eCustomer - CDI Events„Wird das Event Objecttatsächlich benötigt oder gibtes eventuell nochAlternativen?“ „Nicht unbedingt, man kann auch sehr gut mit eigenen Qualifiern arbeiten!“
    67. 67. eCustomer - CDI Events Qualified CDI Event (1/3)@Qualifier@Target({TYPE, METHOD, PARAMETER, FIELD})@Retention(RUNTIME)public @interface Created{}
    68. 68. eCustomer - CDI Events Qualified CDI Event - Alternative (2/3) public void sendWelcomeMail( @Observes CustomerCreatedEvent event @Created Customer customer) { Customer customer = event.getCustomer(); ... }„Und wie fange ich es?“
    69. 69. eCustomer - CDI Events Qualified CDI Event - Alternative (3/3) @Inject @Created private Event<Customer> eventSource; public String createCustomer() { Customer customer = ...; eventSource.fire(customer); ... }„Und vor allem: Wie löse ich es aus?“
    70. 70. eCustomer - CDI Events„Wow! Observer mit Qulifiern -das muss ich erst einmalsacken lassen!“ „Und das ist erst die Spitze des Eisberges - schau dir mal die CONDITIONAL OBSERVER METHODS hier an!“
    71. 71. eCustomer - CDI Events Conditional Observer Methods// Conditional Observer Method that takes// - Transaction status, e.g. AFTER_SUCCESS, AFTER_FAILURE// - Bean instance status, e.g. ALLWAYS// into accountpublic void sendWelcomeMail( @Observes( receive=ALLWAYS, // bean during=AFTER_SUCCESS // tx ) @Created Customer customer) {...}
    72. 72. TX Grenze > „No-Tier“
    73. 73. Fazit:X
    74. 74. CDI FazitCDI ermöglicht ...“Java Enterprise Development without EJB -and without Spring“CDI bietet dafür ...typesafe und schichtenneutrales Injection„Framework für fachliche und technischeInjection sowie ein Rahmenwerk füreventgetriebene Entwicklung.“
    75. 75. @mobileLarson @_openknowledgefacebook.com/openknowledgeGibt es nochFragen?Dann los ...

    ×