为什么使用线程时上下文为空? [英] Why context null when using thread?

查看:112
本文介绍了为什么使用线程时上下文为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用primefaces 3.2,在JSF中使用线程时遇到了问题.我无法从该线程获得对当前FacesContext的引用.

如何在该线程内获取对该上下文的引用.

解决方案

当前FacesContext存储在静态线程局部变量中. Servlet容器使用特定的线程来处理到达容器的每个请求/响应,因此,将FacesContext存储在线程局部变量中可确保每个请求处理生命周期只有一个实例,实际情况应该如此. /p>

当您尝试从另一个线程获取对FacesContext的引用时,会发生什么事情,因为它被存储为线程局部变量,并且由于您的新线程尚未初始化它:您获得了null参考.

如果您希望/需要在另一个新线程中创建一个faces上下文,则唯一可能的方法是通过FacesContextFactory创建一个新的上下文实例.但是,您将需要引用HttpServletRequestHttpServletResponseServletContext来实例化它(而且我想您将无法从单独的线程中获得对这些对象的有效引用).

I am using primefaces 3.2, i have a problem while using a thread in JSF. I couldn't get a reference to the current FacesContext from that thread.

How to get a reference to that context inside that thread.

解决方案

The current FacesContext is stored in a static thread-local variable. Servlet containers use a specific thread to process each request/response that arrives to the container so, storing the FacesContext in a thread-local variable ensures that there is just one instance per request processing lifecycle, which is how it should be.

When you try to obtain a reference to a FacesContext from another thread what happens is, since the it's stored as a thread-local variable and since your new thread didn't initialized it yet: you obtain a null reference.

If you want/need a faces context in another new thread the only possible way is to create a new context instance by means of the FacesContextFactory. However you will need a reference to a HttpServletRequest, a HttpServletResponse and a ServletContext to instantiate it (and I guess you won't be able to get valid references to those objects from a separate thread).

这篇关于为什么使用线程时上下文为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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