MyFaces CODI Conversations

O
MyFaces CODI Scopes
Use the right scopes for your CDI beans
Agenda


•   The History
•   CODI in a Nutshell
•   CODI Setup
•   CODI Scopes in Action
•   Coding
•   CODI Conversation - Insights
•   Your Use-Cases
THE HISTORY
Scopes of the Servlet Spec

                         Application



                        Session




                  Custom Scopes




              Request
CODI Scope Overview

                           Application



                        Session



                      Window



               Conversation



             View-Access



            Request
Why custom Scopes?


• Request scope too short for most UI-Use-Cases
• Session scope
   – Too long
   – No support for multi-window applications
• Different (conversation) concepts available
   – MyFaces Orchestra
   – CDI Conversation Scope
   – MyFaces CODI Scopes
       • Conversation Scope
       • View-Access Scope
       • Window Scope
MyFaces Orchestra


•   CODI is NOT Orchestra.NEXT
•   Orchestra is for Spring (only)
•   CODI is for CDI (only)
•   Orchestra introduced great concepts!
     CODI was planned as "type-safe Orchestra" for CDI
    Now it’s way more (in most areas) but
    without Persistence-Management!
What‘s about @ConversationScoped of CDI?


• Completely broken for several use-cases!
• More like inflexible sessions per window
• Some of the disadvantages
   –   Overhead e.g. with failed conversion/validation
   –   Lazy termination (after the rendering process)
   –   Restarting conversations during a request isn’t possible
   –   End the whole conversation or nothing
   –   An explicit start is required
   –   Manual check if conversation has been started
• Test them with your use-cases!
   use them or forget them 
MyFaces CODI - Overview


• MyFaces Extensions CDI aka MyFaces CODI is a
  portable CDI extension which can be used with
  Apache OpenWebBeans, JBoss Weld,… and
  in combination with other portable CDI extensions
• CODI-Core is required in any case
• Modules
   –   JSF Module (for 1.2 and 2.0 and 2.1)
   –   JPA Module
   –   BV Module
   –   I18n-Message Module
   –   Scripting Module
   –   Test Support Modules
MyFaces CODI in a Nutshell



•   JSF 1.2 and 2.x          • Type-safe View-Configs
•   Type-safety              • Type-safe Navigation
•   Extensibility            • JPA Integration
•   Advanced Scopes          • Dependency Injection
•   Various Events             Support for BV
•   View-Controller          • Advanced I18n
•   JSF 2 Scope Mappings     • Scripting Integration
                             • And more!
CODI - SETUP
Getting CODI up and running


• Add CODI to the project
    – With Maven
        • Add the modules (or the all-in-one package) to the POM
    – Without Maven
        • Download the current dist package
        • Add the modules (or the all-in-one package) to the Classpath of
          the project
•   Start using it!



                      Hint:
                      mvn archetype:generate -DarchetypeCatalog=http://myfaces.apache.org
Getting CODI up and running - Hints


• With JEE 5 and Mojarra use the controlled bootstrapping
  add-on
• Attention (hint for all bean archives):
  Ensure that the libs aren’t duplicated – otherwise the CDI
  implementation will blame ambiguous interceptors, beans,…
CODI SCOPES IN ACTION
CODI Scopes (with OWB) - Performance
Request Scope (as a Comparison) - Details - 2
Request Scope (as a Comparison) - Details - 1
Window-Scope


•   Like an advanced session per Browser-Window/Tab
•   Use it e.g. instead of @SessionScoped
•   Starts automatically with the first access
•   No support for (CODI conversation-)groups
•   Example:

    @WindowScoped
    public class PreferencesBean
      implements Serializable {
      //...
    }
View-Access Scope


• The next page does not use a view-scoped bean
   the bean get un-scoped
• Simple alternative to @ConversationScoped (of CODI)
• Starts automatically with the first access
• No support for (CODI conversation-) groups
• Example

  @ViewAccessScoped
  public class RegistrationWizard
   implements Serializable {
    //...
  }
(Grouped) Conversations


• Every bean is isolated in a separated conversation
• The bean class is the implicit group
• Terminating a conversation un-scopes all beans connected
  with the conversation (per default 1) immediately
• Termination manually via API or automatically via timeout
• Starts automatically with the first access
• Example
  @ConversationScoped
  public class OrderWizard
   implements Serializable {
    //...
  }
Grouped Conversations - 1


• Grouping beans which belong to a logical conversation
  (e.g. a Use-Case) can be grouped together
• One bean can be used for multiple (parallel) use-cases
• @ConversationGroup is a "special" CDI qualifier
• Termination of the group  un-scoping of all bean(s)
• Example 1
  @ConversationScoped
  @ConversationGroup(OrderUseCase.class)
  public class Customer implements Serializable {
    //...
  }
Grouped Conversations - 2


@Produces
@ConversationScoped
@ConversationGroup(UseCase1.class)
public DemoBean createDemoBeanForUseCase1() {
  return new DemoBean("createDemoBeanForUseCase1");
}

@Produces
@ConversationScoped
@ConversationGroup(UseCase2.class)
public DemoBean createDemoBeanForUseCase2() {
  return new DemoBean("createDemoBeanForUseCase2");
}
Closing vs. Restarting Conversations - 1


• Conversation#close
  Closes a conversation immediately
• Conversation#restart
  Un-scopes all beans of the conversation but internal
  data-structures are intact for using it again
  (better performance)
• Example

   @Inject
   private Conversation conversation;
   //...
   this.conversation.close();
@CloseConversationGroup


• @CloseConversationGroup
  Interceptor for closing a conversation group e.g.
  after a method-invocation or based on a given exception.
• Examples:
   @CloseConversationGroup
   public void registerUser () {}

   @CloseConversationGroup(group = UseCase1.class)
   public void registerUser () {}

   @CloseConversationGroup(
     group = UseCase1.class, on = MyException.class)
   public void registerUser () {}
CODI CONVERSATION - INSIGHTS
Optional Configuration


• CodiConfig
   – WindowContextConfig
      •   getWindowContextTimeoutInMinutes
      •   isUnknownWindowIdsAllowed
      •   getMaxWindowContextCount
      •   isCloseEmptyWindowContextsEnabled
      •   is*EventEnabled
      •   …
   – ConversationConfig
      • getConversationTimeoutInMinutes
      • is*EventEnabled
      • …
   – …
WindowScoped vs WindowContext


• The Window-Context represents the whole window including
  all scopes which are bound to it
• Window-scoped beans are just a part of the current window
• WindowContext API allows to manage the current window
• Example

  @Inject
  private WindowContext windowContext;
  //...
  this.windowContext.getId();
  this.windowContext.closeConversations();
Window-Context

                                   Window(-Context)
                                       API and SPI


    [Scope]            [Scope]             [Scope]          [Scope]    [Scope]

   Window            Conversation       Conversation        View-      View-
                        Group:            Group:            Access     Access
                        Bean1            UseCase1




                                                              Bean 3




                                                                         Bean 4
   Bean 1

            Bean 2




                          Bean 1




                                          Bean 1

                                                   Bean 2

                                       Attributes
Powerful SPI


•   WindowContextManagerFactory
•   WindowContextFactory
•   WindowContextQuotaHandler
•   WindowHandler
•   ConversationFactory
•   WindowContextManager
•   BeanEntryFactory
•   …
Optional Events


• WindowContextEvent
   – CreateWindowContextEvent
   – CloseWindowContextEvent
• ConversationEvent
   – StartConversationEvent
   – RestartConversationEvent
   – CloseConversationEvent
• BeanEvent
   – ScopeBeanEvent
   – AccessBeanEvent
   – UnscopeBeanEvent
TELL US YOUR USE-CASES!
Links


•   http://myfaces.apache.org/extensions/cdi/
•   https://cwiki.apache.org/confluence/display/EXTCDI/Index
•   https://svn.apache.org/repos/asf/myfaces/extensions/cdi/
•   http://twitter.com/MyFacesTeam
•   http://myfaces.apache.org/extensions/cdi/mail-lists.html
•   Recommended
    – http://openwebbeans.apache.org
    – http://code.google.com/a/apache-extras.org/p/
      myfaces-codi-addons/
    – http://os890.spaaze.com/myfaces-codi
1 von 32

Más contenido relacionado

Was ist angesagt?

Liferay maven sdkLiferay maven sdk
Liferay maven sdkMika Koivisto
3.2K views20 Folien
Maven for DummiesMaven for Dummies
Maven for DummiesTomer Gabel
6.3K views9 Folien
Note - Apache Maven IntroNote - Apache Maven Intro
Note - Apache Maven Introboyw165
2.6K views15 Folien

Was ist angesagt?(20)

Liferay maven sdkLiferay maven sdk
Liferay maven sdk
Mika Koivisto3.2K views
Maven for DummiesMaven for Dummies
Maven for Dummies
Tomer Gabel6.3K views
Polygot Java EE on the GraalVMPolygot Java EE on the GraalVM
Polygot Java EE on the GraalVM
Ryan Cuprak1K views
Introduction tomavenIntroduction tomaven
Introduction tomaven
Manav Prasad236 views
Note - Apache Maven IntroNote - Apache Maven Intro
Note - Apache Maven Intro
boyw1652.6K views
Java build tool_comparisonJava build tool_comparison
Java build tool_comparison
Manav Prasad338 views
Java EE 8Java EE 8
Java EE 8
Ryan Cuprak4.4K views
OSGi in 5 minutesOSGi in 5 minutes
OSGi in 5 minutes
Serge Huber3K views
Maven basic conceptMaven basic concept
Maven basic concept
Ming-Sian Lin843 views
Spring - CDI InteropSpring - CDI Interop
Spring - CDI Interop
Ray Ploski6.5K views
Maven pptMaven ppt
Maven ppt
natashasweety74.6K views
Intro To OSGiIntro To OSGi
Intro To OSGi
Stephan Janssen4.9K views
Spring 4 on Java 8 by Juergen HoellerSpring 4 on Java 8 by Juergen Hoeller
Spring 4 on Java 8 by Juergen Hoeller
ZeroTurnaround13.3K views
OSGi PresentationOSGi Presentation
OSGi Presentation
Michal Malohlava5.3K views
Learning Maven by ExampleLearning Maven by Example
Learning Maven by Example
Hsi-Kai Wang1.6K views
Maven tutorialMaven tutorial
Maven tutorial
Dragos Balan1.8K views
From JavaEE to AngularJSFrom JavaEE to AngularJS
From JavaEE to AngularJS
Nebrass Lamouchi1.6K views

MyFaces CODI Conversations

  • 1. MyFaces CODI Scopes Use the right scopes for your CDI beans
  • 2. Agenda • The History • CODI in a Nutshell • CODI Setup • CODI Scopes in Action • Coding • CODI Conversation - Insights • Your Use-Cases
  • 4. Scopes of the Servlet Spec Application Session Custom Scopes Request
  • 5. CODI Scope Overview Application Session Window Conversation View-Access Request
  • 6. Why custom Scopes? • Request scope too short for most UI-Use-Cases • Session scope – Too long – No support for multi-window applications • Different (conversation) concepts available – MyFaces Orchestra – CDI Conversation Scope – MyFaces CODI Scopes • Conversation Scope • View-Access Scope • Window Scope
  • 7. MyFaces Orchestra • CODI is NOT Orchestra.NEXT • Orchestra is for Spring (only) • CODI is for CDI (only) • Orchestra introduced great concepts!  CODI was planned as "type-safe Orchestra" for CDI Now it’s way more (in most areas) but without Persistence-Management!
  • 8. What‘s about @ConversationScoped of CDI? • Completely broken for several use-cases! • More like inflexible sessions per window • Some of the disadvantages – Overhead e.g. with failed conversion/validation – Lazy termination (after the rendering process) – Restarting conversations during a request isn’t possible – End the whole conversation or nothing – An explicit start is required – Manual check if conversation has been started • Test them with your use-cases!  use them or forget them 
  • 9. MyFaces CODI - Overview • MyFaces Extensions CDI aka MyFaces CODI is a portable CDI extension which can be used with Apache OpenWebBeans, JBoss Weld,… and in combination with other portable CDI extensions • CODI-Core is required in any case • Modules – JSF Module (for 1.2 and 2.0 and 2.1) – JPA Module – BV Module – I18n-Message Module – Scripting Module – Test Support Modules
  • 10. MyFaces CODI in a Nutshell • JSF 1.2 and 2.x • Type-safe View-Configs • Type-safety • Type-safe Navigation • Extensibility • JPA Integration • Advanced Scopes • Dependency Injection • Various Events Support for BV • View-Controller • Advanced I18n • JSF 2 Scope Mappings • Scripting Integration • And more!
  • 12. Getting CODI up and running • Add CODI to the project – With Maven • Add the modules (or the all-in-one package) to the POM – Without Maven • Download the current dist package • Add the modules (or the all-in-one package) to the Classpath of the project • Start using it! Hint: mvn archetype:generate -DarchetypeCatalog=http://myfaces.apache.org
  • 13. Getting CODI up and running - Hints • With JEE 5 and Mojarra use the controlled bootstrapping add-on • Attention (hint for all bean archives): Ensure that the libs aren’t duplicated – otherwise the CDI implementation will blame ambiguous interceptors, beans,…
  • 14. CODI SCOPES IN ACTION
  • 15. CODI Scopes (with OWB) - Performance
  • 16. Request Scope (as a Comparison) - Details - 2
  • 17. Request Scope (as a Comparison) - Details - 1
  • 18. Window-Scope • Like an advanced session per Browser-Window/Tab • Use it e.g. instead of @SessionScoped • Starts automatically with the first access • No support for (CODI conversation-)groups • Example: @WindowScoped public class PreferencesBean implements Serializable { //... }
  • 19. View-Access Scope • The next page does not use a view-scoped bean  the bean get un-scoped • Simple alternative to @ConversationScoped (of CODI) • Starts automatically with the first access • No support for (CODI conversation-) groups • Example @ViewAccessScoped public class RegistrationWizard implements Serializable { //... }
  • 20. (Grouped) Conversations • Every bean is isolated in a separated conversation • The bean class is the implicit group • Terminating a conversation un-scopes all beans connected with the conversation (per default 1) immediately • Termination manually via API or automatically via timeout • Starts automatically with the first access • Example @ConversationScoped public class OrderWizard implements Serializable { //... }
  • 21. Grouped Conversations - 1 • Grouping beans which belong to a logical conversation (e.g. a Use-Case) can be grouped together • One bean can be used for multiple (parallel) use-cases • @ConversationGroup is a "special" CDI qualifier • Termination of the group  un-scoping of all bean(s) • Example 1 @ConversationScoped @ConversationGroup(OrderUseCase.class) public class Customer implements Serializable { //... }
  • 22. Grouped Conversations - 2 @Produces @ConversationScoped @ConversationGroup(UseCase1.class) public DemoBean createDemoBeanForUseCase1() { return new DemoBean("createDemoBeanForUseCase1"); } @Produces @ConversationScoped @ConversationGroup(UseCase2.class) public DemoBean createDemoBeanForUseCase2() { return new DemoBean("createDemoBeanForUseCase2"); }
  • 23. Closing vs. Restarting Conversations - 1 • Conversation#close Closes a conversation immediately • Conversation#restart Un-scopes all beans of the conversation but internal data-structures are intact for using it again (better performance) • Example @Inject private Conversation conversation; //... this.conversation.close();
  • 24. @CloseConversationGroup • @CloseConversationGroup Interceptor for closing a conversation group e.g. after a method-invocation or based on a given exception. • Examples: @CloseConversationGroup public void registerUser () {} @CloseConversationGroup(group = UseCase1.class) public void registerUser () {} @CloseConversationGroup( group = UseCase1.class, on = MyException.class) public void registerUser () {}
  • 26. Optional Configuration • CodiConfig – WindowContextConfig • getWindowContextTimeoutInMinutes • isUnknownWindowIdsAllowed • getMaxWindowContextCount • isCloseEmptyWindowContextsEnabled • is*EventEnabled • … – ConversationConfig • getConversationTimeoutInMinutes • is*EventEnabled • … – …
  • 27. WindowScoped vs WindowContext • The Window-Context represents the whole window including all scopes which are bound to it • Window-scoped beans are just a part of the current window • WindowContext API allows to manage the current window • Example @Inject private WindowContext windowContext; //... this.windowContext.getId(); this.windowContext.closeConversations();
  • 28. Window-Context Window(-Context) API and SPI [Scope] [Scope] [Scope] [Scope] [Scope] Window Conversation Conversation View- View- Group: Group: Access Access Bean1 UseCase1 Bean 3 Bean 4 Bean 1 Bean 2 Bean 1 Bean 1 Bean 2 Attributes
  • 29. Powerful SPI • WindowContextManagerFactory • WindowContextFactory • WindowContextQuotaHandler • WindowHandler • ConversationFactory • WindowContextManager • BeanEntryFactory • …
  • 30. Optional Events • WindowContextEvent – CreateWindowContextEvent – CloseWindowContextEvent • ConversationEvent – StartConversationEvent – RestartConversationEvent – CloseConversationEvent • BeanEvent – ScopeBeanEvent – AccessBeanEvent – UnscopeBeanEvent
  • 31. TELL US YOUR USE-CASES!
  • 32. Links • http://myfaces.apache.org/extensions/cdi/ • https://cwiki.apache.org/confluence/display/EXTCDI/Index • https://svn.apache.org/repos/asf/myfaces/extensions/cdi/ • http://twitter.com/MyFacesTeam • http://myfaces.apache.org/extensions/cdi/mail-lists.html • Recommended – http://openwebbeans.apache.org – http://code.google.com/a/apache-extras.org/p/ myfaces-codi-addons/ – http://os890.spaaze.com/myfaces-codi