It allows to maintain the same conversation (state) between distinct page requests although it has a smaller scope than session context. That happens because conversation scoped beans have a specific number of requests while session scoped beans have unlimited requests making it much heavier. The bean will stay active until it times out.
What's NOT so great about conversation beans?
BusyConversationException: when two requests arrive at the server simultaneously or in short succession. Conversation scoped beans along with AJAX are a very dangerous solution! However, this is a possible solution.
How to implement it?
You need to add a beans.xml file and place it in /WEB-INF directory.
Sample of beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>
Sample of a Conversation scoped bean:
@Named
@ConversationScoped
public class MyConversationBean implements Serializable {
@Inject
Conversation conversation;
}
Why does the conversation scoped bean has to be serializable?
Session and conversation scoped beans live for longer periods of time and the container while managing its resources might need to free some of it. Then, the container persists the bean into some sort of persistent media type or physical storage. Whenever the bean is needed agan, the container fetchs the bean and restores its state.
Why does the conversation scoped bean has the @ConversationScoped annotation?
This annotation tells the container that this bean will maintained while conversation is running.
Why do you have to inject the Conversation object reference?
The Conversation object manages the conversation context by using begin() and end() methods.
Why does the conversation scoped bean has the @Named annotation?
This annotation allows you to access the bean using Expression Language in JSF pages.
What are conversation scoped bean states?
There are two possible states: transient or long-running. When injected, the conversation is transient. Mark the conversation as long-running (when an actual conversation starts) by calling Conversation.begin() and back to transient by Conversation.end(). However, when trying to begin() a long-running conversation or when trying to end() a transient conversation an IllegalStateException is thrown.
Do conversation scoped beans propagate?
According to here, conversation automatically propagates in redirections and JSF faces requests, for instance, form submission. It does not propagate with non-faces requests like navigation via link.
The conversation can be forced to propagate in a non-faces request by including cid as a request parameter (therefore cid is a reserved parameter name). The cid lets the container know which conversation to load.
If the conversation is automatically propagated, cid is added to the URL by the container.
If you want to avoid conversation propagation, use the conversationPropagation request parameter (introduced in CDI 1.1).
Do conversation scoped beans timeout?
When no conversation is propagated to a JSF request, the request is associated with a new transient conversation. All long-running conversations are scoped to a particular HTTP servlet session and may not cross session boundaries. In the following cases, a propagated long-running conversation cannot be restored and reassociated with the request:
When the HTTP servlet session is invalidated, all long-running conversation contexts created during the current session are destroyed, after the servlet service() method completes.
The container is permitted to arbitrarily destroy any long-running conversation that is associated with no current JSF request, in order to conserve resources.
The timeout is the period of inactivity before the conversation is destroyed (as opposed to the amount of time the conversation is active).
The Conversation object provides a method to set the timeout. This is a hint to the container, which is free to ignore the setting.
conversation.setTimeout(timeoutInMillis);
Useful resources:
JBoss ConversationScoped
JBoss Conversation Propagation
CDI bean scopes
CDI and conversation scope