concept conversation in category ejb

appears as: conversation, conversation, A conversation, conversations
EJB 3 in Action, Second Edition

This is an excerpt from Manning's book EJB 3 in Action, Second Edition.

Not all sessions are equal. Sessions last for different durations with some being short and others long. Instead of the remote desktop example, let’s take a look at day-to-day conversations. A conversation can be thought of as a form of a session between two people. A short conversation would be a simple question and response to a stranger about the current time: “What time is it?” “It’s noon.” A longer conversation would be a discussion between two friends as they banter about a basketball game. In the short conversation, the conversation ends once the person responds with the time. You can categorize these two conversations as stateless and stateful sessions, respectively—conveniently corresponding to session bean types we’ll discuss soon.

The conversation scope deserves its own section. Unlike the other scopes, the conversation scope is a completely new beast. If you’ve used the forerunner to CDI, JBoss Seam (versions 1 or 2), you’re probably familiar with the concept of a conversation. A conversation is a scope that’s shorter than a session but longer than a request. This is shown in figure 12.2. A conversation is controlled programmatically; code determines when a conversation begins and ends.

Figure 12.2. Conversation scope versus session and application

The easiest way to think of a conversation is to look at how users interact with web applications using the current breed of web browsers. A bidder on ActionBazaar might open two tabs for viewing two items that they’re interested in bidding on. Although they’re viewing two separate items on two tabs, as far as ActionBazaar is concerned there’s one session for this user. As a result, the current item the user is viewing can’t be tracked using the user’s session because the session is shared between the browser tabs. Imagine what would happen if the user were viewing a kayak on one tab and then opened up another tab to view a five-carat diamond. When the user switched back to the kayak and clicked Place Bid, there would be panic if the confirmation page showed a bid for the five-carat diamond. The concept of a conversation addresses this problem.

A bean is marked as belonging to a conversation by adding the @ConversationScoped annotation to the bean definition. By default, a conversation is associated with the current request scope and is terminated when the current request is destroyed. To make a conversation survive beyond the current request, it must be promoted to a long-running conversation. The promotion to a long-running conversation is done programmatically. For this to be done, the current conversation must be acquired via injection and the begin method invoked. A conversation can then either be programmatically terminated or expire after a fixed time period. The timeout is thus less than that of a session.

We’ll revisit conversations later in this chapter once we’ve covered the basics of CDI. Conversations are one of the more advanced features. Conversations are an example of how the EE standard grows because of the experimentation of proprietary technologies. Conversations were first introduced by JBoss Seam and then later integrated into Java EE 6 and later.

12.7. Using conversations

Earlier in the chapter we touched briefly on conversations. CDI has the concept of scopes to which the lifecycle of a bean is tied. If a bean is tied to the request scope, it will exist only for the request—it won’t be available on a subsequent request. A conversation-scoped bean will exist for the duration of a conversation, which is less than that of a session but greater than that of a request. Thus, a conversation spans multiple page requests and represents a subunit of work.

Conversations are central to how most users interact with their web browsers. It’s now common for a user to have multiple tabs open in the web browser. The tabs may be pointed at the same web application. Consequently, the tabs will share the same session. For a travel site, the user might be planning a vacation in one tab and booking a business trip on another tab. It’s critical for the web application to isolate each tab so that when the user clicks Submit on the tab for booking the business trip, the vacation isn’t booked instead, or, worse yet, the business flights are booked but the hotel for the vacation is booked instead of the business accommodations. It’s either last page visit wins or a messy situation in which the state between the tabs is mixed. One way to avoid this problem is to use an abstraction on top of the session with its own unique ID tracked in each tab. This is essentially what CDI is providing, but it’s managing the lifecycle, thus enabling you to focus on the business logic.

To mark a bean as belonging to a conversation you annotate it with @ConversationScoped. But by default, a conversation will last for only one request unless you promote the current conversation. A conversation that hasn’t been promoted is termed a transient conversation. The explicit promotion is necessary because the container needs to know when a long-running conversation is starting versus a short chat. The container also needs to know when the conversation ends. Both the explicit start and stop are used to manage resources. To start and stop a conversation, you interact with a javax .enterprise.context.Conversation object. It’s acquired via injection. The methods on this interface are documented in table 12.4.

Table 12.4. Methods on conversation

Method

Description

void begin() Converts the current transient conversation into a long-running conversation
void begin(String id) Converts the current transient conversation into a long-running conversation with a custom ID
void end() Terminates the conversation and converts it to a transient conversation
String getId() Returns the ID of the current conversation
long getTimeout() Returns the timeout of the current conversation in milliseconds
boolean isTransient() Returns true if the current conversation is transient and will be lost at the end of the request
void setTimeout(long timeout) Sets a custom timeout for the current conversation; only applicable if the conversation isn’t transient

To better understand conversations, let’s look at a relatively simple use case from ActionBazaar. This use case will leverage most of the CDI skills you’ve acquired throughout the chapter, including producer methods and qualifiers. We’ll look at how a user goes from viewing a list of items to actually placing a bid, as shown in sequence in figure 12.5.

Listing 12.17. ItemController—starting a conversation