重新加入与 Java CDI Weld 的对话

Reattach to conversation with Java CDI Weld

我想加入现有的对话范围。

我开始对话:

conversation.begin(packageId);

我接近使用以下似乎有效的方法:

@Inject @Http CoversationContext context;

context.activate(packageId);

但是我在日志中看到一条警告,提示我没有正确处理。

WARN: WELD-000335: Conversation context is already active, most likely it was not cleaned up properly during previous request processing: HttpServletRequestImpl [ POST /path/to/url ]

如果有另一种方法可以删除对话并重新创建,我也很高兴(只要我可以继续使用相同的自定义对话 ID)我正在努力避免用户多次重新加载页面用相同包数据的副本填满内存。

我也考虑过使用@SessionScoped bean,但我想如果我可以将包 ID 设置为对话 ID,那么我就可以避免管理 @SessionScoped bean 的需要。

只要 cid 参数在请求中,并且对话是 long-running(因为你做了 conversation.begin(packageId))那么就不需要加入对话上下文,它在当前请求中已经处于活动状态。

然而,您需要做的是通过以下方式在每个请求表单或 url 参数中包含 cid

例如

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

<h:form> <f:param name="cid" value="#{javax.enterprise.context.conversation.id}"/> /h:form>

Note that the conversation must be long-running by explicitly starting it as conversation.begin(id)

还有:

At the end of your step processing, you need to explicitly call conversation.end() otherwise the conversation scoped beans will be destroyed only at the end of the session context

图书标记,则需要在路径中包含cid参数或任何逻辑映射,然后使用过滤器转发cid参数:

@WebFilter(urlPatterns = "/*")
public class CidFilter implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
        String cid = extractCidParameterIfAny(request);
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        if (cid != null) {
            String forwardUrl = buildForwardUrlWithCidParameter(cid);
            HttpServletRequest wrapper = new CidHttpServletRequest(httpRequest);
            httpRequest.getRequestDispatcher(forwardUrl).forward(wrapper, response);
        } else {
            chain.doFilter(request, response);
        }
    }

}