...
For users who aren't familiar with the conversation concepts provided by MyFaces Orchestra:
Orchestra allows to group beans within a (named) conversation. Furthermore, it's possible to use beans with different (conversation-) lifecycles in the same page. (Usually there isn't one big conversation - like the std. CDI conversation which is more like a bit smarter session for a window/tab.) Conversations are bound to a window/tab of a browser. That means you automatically get new conversations, if you open the application in a new tab/window. Conversations are started automatically as soon terminates as a conversation scoped bean is used. A conversation ends terminates based on a timeout (in case of the access scope - a conversation ends terminates as soon as it wasn't used by a page). Furthermore, it's possible to end close (or restart) conversations manually. That means via an API call it's possible to end terminate a conversation immediately. The next access leads to a new conversation. If the whole session gets destroyed, also all conversations will be destroyed automatically.
...
Code Block |
---|
title | Usage of @ConversationScoped |
---|
borderStyle | solid |
---|
|
@ConversationScoped
public class DemoBean1 implements Serializable
{
public String getWelcomeText()
{
return "Hello MyFaces CODI!";
}
}
|
Notetip |
---|
|
As soon as you access the bean, the conversation gets started! |
Tipnote |
---|
|
If you would like to use the bean within your JSF pages, you have to add @Named (javax.inject.Named ). |
Code Block |
---|
title | Injecting conversation scoped beans |
---|
borderStyle | solid |
---|
|
@Named
@ConversationScoped
public class DemoBean2 implements Serializable
{
@Inject
private DemoBean1 bean1;
private String text;
@PostConstruct
protected void init()
{
text = this.bean1.getWelcomeText();
}
/*generated*/
public String getText()
{
return text;
}
}
|
...
Code Block |
---|
title | Injecting the current Conversation |
---|
borderStyle | solid |
---|
|
@ConversationScoped
public class DemoBean3 implements Serializable
{
@Inject
private Conversation conversation;
public void save()
{
//your custom logic
//...
this.conversation.endclose();
//or use:
//conversation.restart();
//... if you know that you will need new instances of the same beans e.g. in the next view
//#restart it's just a possible performance improvement
}
}
|
If you don't like to close a conversation immediately, you will be able to terminate a conversation after the rendering process of the next view. This feature will be implemented soon. Currently you can use phase-observer-methods in combination with @View to do it manually.
Window Scope
The usage is exactly the same like @ConversationScoped
. The only difference is that there is no timeout.
You can compare it to a session per window. However, as we will see later on it's possible to reset the window-context manually.
Code Block |
---|
title | Usage of @WindowScoped |
---|
borderStyle | solid |
---|
|
@WindowScoped
public class DemoBean4 implements Serializable
{
}
|
View-Access Scope
The usage is exactly the same like @ConversationScoped
. The only difference is that there is no timeout. If you don't use @ViewAccessScoped
beans within a view, they won't be available any longer. That means: They get cleaned up automatically, as soon as you don't use them.
Code Block |
---|
title | Usage of @ViewAccessScoped |
---|
borderStyle | solid |
---|
|
@ViewAccessScoped
public class DemoBean5 implements Serializable
{
}
|
Conversation Groups
MyFaces Orchestra introduced conversation names to group conversation scoped beans. Instead of names MyFaces CODI provides a typesafe alternative: conversation groups. A conversation group is a marker class or interface. Behind the scenes @ConversationGroup
is a CDI qualifier which is treated in a special way. If you don't define a special group the class of the bean is used as conversation group. That means: every conversation scoped bean belongs to an explicit or implicit conversation group.
Code Block |
---|
title | Usage of @ConversationGroup |
---|
borderStyle | solid |
---|
|
@ConversationScoped
@ConversationGroup(ConversationGroup1.class)
public class DemoBean6 implements Serializable
{
@Inject
private Conversation conversation;
public void save()
{
//...
this.conversation.close();
}
}
|
Code Block |
---|
title | Implementation of ConversationGroup1 |
---|
borderStyle | solid |
---|
|
public interface ConversationGroup1
{
}
|
Code Block |
---|
title | A bean for different conversation groups |
---|
borderStyle | solid |
---|
|
public class DemoBean7 implements Serializable
{
@Produces
@ConversationScoped
@ConversationGroup(ConversationGroup1.class)
public DemoBean7 createBeanForGroup1()
{
return new DemoBean7();
}
@Produces
@ConversationScoped
@ConversationGroup(ConversationGroup2.class)
public DemoBean7 createBeanForGroup2()
{
return new DemoBean7();
}
}
|
Note |
---|
|
In case of this.conversation.close(); the whole conversation of ConversationGroup1 will be destroyed (in this case 2 beans). |
If you would like to close all conversations of an other conversation-group, you can use the WindowContext
.
Code Block |
---|
title | Close a whole conversation group |
---|
borderStyle | solid |
---|
|
public class DemoBean8
{
@Inject
private WindowContext windowContext;
public void save()
{
//...
this.windowContext.closeConversationGroup(ConversationGroup2.class);
}
}
|