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,…
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 () {}
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();