`

Java EE CDI ConversationScoped

    博客分类:
  • JSF
阅读更多

Conversation scope

The conversation scope holds state associated with a user of the system, and spans multiple requests to the server.A conversation represents a task—a unit of work from the point of view of the user.

 

The conversation context is active during any JSF request. Most conversations are destroyed at the end of the request. If a conversation should hold state across multiple requests, it must be explicitly promoted to a long-running conversation.

 

Conversation demarcation

 

CDI provides a built-in bean for controlling the lifecycle of conversations in a JSF application. This bean may be obtained by injection:

 

@Inject
Conversation conversation;

 

To promote the conversation associated with the current request to a long-running conversation, call the begin() method from application code. To schedule the current long-running conversation context for destruction at the end of the current request, call end().

 

public void beginConversation() {
    if (conversation.isTransient()) {
        conversation.begin();
        conversation.setTimeout(1000 * 60 * 30);
    }
}

public void endConversation() {
    conversation.end();
}

 

The container is permitted to destroy a conversation and all state held in its context at any time in order to conserve resources. The Conversation object provides a method to set the timeout. The timeout is the period of inactivity before the conversation is destroyed (as opposed to the amount of time the conversation is active).

 

The method beginConversation() may be called when the page is generated by the means of the JSF preRenderView event.

 

...
<f:event type="preRenderView" listener="#{action.beginConversation}"/>
<h:head>
  ...
</h:head>
...

 

 

Conversation propagation

 

The conversation context automatically propagates with any JSF faces request (JSF form submission) or redirect. It does not automatically propagate with non-faces requests, for example, navigation via a link.

 

We can force the conversation to propagate with a non-faces request by including the unique identifier of the conversation as a request parameter. The CDI specification reserves the request parameter named cid for this use. The unique identifier of the conversation may be obtained from the Conversation object, which has the EL bean name javax.enterprise.context.conversation.

 

Therefore, the following link propagates the conversation:

 

<a href="/addProduct.jsp?cid=#{javax.enterprise.context.conversation.id}">Add Product</a>

 

It's probably better to use one of the link components in JSF 2:

 

<h:link outcome="/addProduct.xhtml" value="Add Product">
   <f:param name="cid" value="#{javax.enterprise.context.conversation.id}"/>
</h:link>

 

In order to keep the code simple, you can define custom tags beginConversation and  conversationId:

 

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:f="http://java.sun.com/jsf/core">
    <f:event type="preRenderView" listener="#{action.beginConversation}"/>
</ui:composition>

 

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:f="http://java.sun.com/jsf/core">
    <f:param name="cid" value="#{javax.enterprise.context.conversation.id}"/>
</ui:composition>

 

Difference between JSF FlowScoped and CDI ConversationScoped

 

A flow is a group of views related by navigation rules. Flow-scoped beans stay alive as long as you navigate within this flow, and they die when you leave the flow.

Conversation-scoped beans can also survive navigation, but their lifetime is delimited programmatically by calling Conversation.begin() and Conversation.end(). They are not directly related to a specific group of views.

You can use conversation scoped beans without using JSF.

 

Reference

Java EE CDI ConversationScoped example

Java EE CDI bean scopes

Java EE 7 CDI bean scopes

Java EE CDI ViewScoped example

Java EE CDI TransactionScoped example

JSF Flow example

Weld reference documentation

分享到:
评论

相关推荐

    Pro CDI 2 in Java EE 8.pdf

    In Pro CDI 2 in Java EE 8, use CDI and the CDI 2.0 to automatically manage the life cycle of your enterprise Java, Java EE, or Jakarta EE application’s beans using predefined scopes and define custom...

    Pro CDI 2 in Java EE 8

    Pro CDI 2 in Java EE 8

    Java EE 7权威指南卷1_1

    包括资源创建、资源注入和打包,还涵盖了多项相关技术,包括JavaServer Faces(JSF)、Java Servlet、WebSocket Java API、JSON处理Java API(JSON—P)、国际化和本地化、bean验证、Java EE上下文和依赖注入(CDI)以及...

    Practical JSF in Java EE 8 pdf

    By developing these JSF web applications, you'll take a tour through the other Java EE technologies such as JPA, CDI, Security, WebSockets, and more. In Practical JSF in Java EE 8, you will learn to...

    Apress.Pro.CDI.2.in.Java.EE.8.pdf

    In Pro CDI 2 in Java EE 8, use CDI and the CDI 2.0 to automatically manage the lifecycle of your enterprise Java, Java EE or Jakarta EE application’s beans using predefined scopes and define custom ...

    Java EE 7权威指南卷1 part2

    包括资源创建、资源注入和打包,还涵盖了多项相关技术,包括JavaServer Faces(JSF)、Java Servlet、WebSocket Java API、JSON处理Java API(JSON—P)、国际化和本地化、bean验证、Java EE上下文和依赖注入(CDI)以及...

    Java+EE+8+Application+Development-Packt+Publishing(2017).epub

    This book covers the latest versions of the most popular Java EE specifications, including JavaServer Faces (JSF), the Java Persistence API (JPA), Enterprise JavaBeans (EJB), Contexts and Dependency ...

    The Java EE 6 Tutorial Basic Concepts 4th Edition

    Java EE 6 APIs in the Java Platform, Standard Edition 6.0 31 GlassFish Server Tools 34 Chapter 2: Using the Tutorial Examples 37 Required Software 37 Starting and Stopping the GlassFish Server ...

    Java EE 7 with GlassFish 4 Application Server 2014年英文原版

    本书涵盖了所有主要的Java EE 7的API,包括JSF 2.2,EJB 3.2,1.1 CDI,对于WebSocket的,JAX-WS,JAX-RS多的Java API。 书中还介绍了JSON-P,为JSON(JavaScript对象符号)处理的Java API。这种先进的主题涉及如何...

    JAVA EE 8 RECIPES

    是一本关于JAVA EE经典开发的书,分为:JPA; JSF; FACELET;EJB;CDI;JPA;JTA;互操作:jax-rs;jax-ws

    Java.EE.7.Development.with.NetBeans.8

    Covers the latest versions of the major Java EE APIs such as JSF 2.2, EJB 3.2, JPA 2.1, CDI 1.1, and JAX-RS 2.0 Walks you through the development of applications utilizing popular JSF component ...

    java_ee_sdk-8u1.zip

    Java EE 8 continues to improve API and programming models needed for today's applications and adds features requested by our world-wide community. This release modernizes support for many industry ...

    Beginning JAVA EE 7经典

    本书为java ee 规范的讲解,JSF,CDI,EJB,JPA,JTA,互操作包括message queue;restful service; soapservice

    Java EE 8 Application Development-Packt Publishing(2017).pdf

    Dependency Injection (CDI), the Java API for JSON Processing (JSON-P), the new Java API for JSON Binding (JSON-B), the Java API for WebSocket, the Java Messaging Service (JMS) API 2.0, the Java API ...

    Introducing Jakarta EE CDI.pdf

    The ultimate goal is to help you, the everyday Java developer, write better code. Throughout the book, you will be working on a simple restaurant application, seeing when and how the various CDI ...

    java-ee-7-com-jsf-primefaces-e-cdi

    java-ee-7-com-jsf-primefaces-e-cdi

    cormo:基于Spring和Java EE CDI的.NET应用程序框架

    [事件和观察者](../../wiki/事件和观察者) [拦截器](../../wiki/interceptors)科莫Cormo是一个.NET应用程序开发框架,将和Java EE的CDI(上下文和依赖项注入)规范(特别是[JBoss Weld]( )的实现)引入了。...

    cdi jsf java ee 6 weld

    cdi jsf 可以运行在tomcat6.0.20上

    The Definitive Guide to JSF in Java EE 8

    turbulent announcement by Oracle in 2017 that Java EE, thus including JSF, would be transferred to the Eclipse foundation. Oracle would stop leading the specs it owned before, which again includes ...

Global site tag (gtag.js) - Google Analytics