CDI ConversationScoped长期运行的Bean无法正常工作 [英] CDI ConversationScoped long-running Bean not working

查看:136
本文介绍了CDI ConversationScoped长期运行的Bean无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在了解Weld或CDI的对话范围时,我遇到了一些问题.

I got some problems understanding the Conversation scope of Weld or CDI.

在我的JSF Faclets页面中,我呼叫:

In my JSF Faclets page i call:

        <f:metadata>
            <f:event type="preRenderView" listener="#{viewBean.start}" />
        </f:metadata>

豆:

import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
@Named
@ConversationScoped
public class ViewBean implements Serializable {

@Inject
    private Conversation conversation;

public void start() {
    if (conversation.isTransient()) {
        System.out.println("START CONVERSATION");
        conversation.begin();

    }
}

现在,每次刷新浏览器时,都会启动一个新的对话.那是正确的行为吗?那么,为什么对话总是短暂的?没有异常被抛出. beans.xml已创建且为空:

Now every time I refresh my browser, a new Conversation is started. Is that the correct behaviour? So why is the conversation always transient? No exception is thrown. The beans.xml is created and empty:

<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>

推荐答案

简短答案:是的,这是正确的行为.

Short answer: Yes, this is the correct behavior.

好答案:对话代表一个工作单元",必须明确标出.就像您已经在做的那样,通过对session.begin()的显式调用来完成此操作.如果您想在一个以上的请求中使用同一会话,则必须传播-这是您正在做的事情:-)

Long answer: A conversation represents a "unit of work", which a such has to be demarcated explicitly. This is done with the explicit call of conversation.begin() - as you are doing already. Should you want to use the same conversation over more than one request, you have to propagate it - this is what you are not doing :-)

传播对话时,对话ID会附加到请求中.这告诉容器想要进行哪个对话.当您仅在请求中没有会话ID的情况下按下刷新按钮时,就会为每个请求生成一个新的会话.

When you propagate a conversation, a conversation-id is appended to the request. This tells the container which conversation is wanted. When you just hit the refresh button without a conversation-id in your request a new conversation is generated for each request.

从文档中:

对话上下文自动 与任何JSF Faces请求一起传播 (提交JSF表单)或重定向.它 不会自动传播 非面孔请求,例如, 通过链接导航.

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.

如果您需要手动传播它,只需将对话ID添加到请求中即可:

If you need to propagate it manually, just add the conversation-id to the request:

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

所有这些以及更多内容都得到了解释

All that and much more is explained here.

这篇关于CDI ConversationScoped长期运行的Bean无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆