Java Reference
In-Depth Information
As you can see from this listener, CDI handled event registration for you. You didn't need
to implement an interface or acquire a reference to the ItemController to register this
class as a listener. The onNewItem method is a listener because the item parameter is an-
notated with @Observes . The instances of item that you'll receive are restricted to
only new items with the @NewItem qualifier
.
We've covered all the basic features of CDI. With your full understanding of CDI, it's time
to revisit conversations to better understand how they work and are used.
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 rep-
resents 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 anoth-
er 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 lo-
gic.
To mark a bean as belonging to a conversation you annotate it with @ConversationS-
coped . 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
Search WWH ::




Custom Search