无法在自生线程中获取"FacesContext.getCurrentInstance()".
Couldn't get "FacesContext.getCurrentInstance()" in self spawned thread.
需要根据后端过程动态更新组件.通过创建一个用于监视进程的线程并与p:poll一起调用组件更新来实现相同的目的.
Required to update the components dynamically based on a back-end process. Achieved the same by creating a thread for monitor the process and call back the component updation along with p:poll.
但是,"FacesContext.getCurrentInstance()"似乎不适用于新线程.从新生成的线程中调用任何依赖于FacesContext.getCurrentInstance()的实用程序方法时,只需获取NPE.
However, seems "FacesContext.getCurrentInstance()" is not available for the new thread. Just getting NPE while calling any utility methods that relies with FacesContext.getCurrentInstance() from the newly spawned thread.
有什么方法可以在自生线程中获取"FacesContext.getCurrentInstance()"吗?
Is there any way to get the "FacesContext.getCurrentInstance()" available in self spawned thread?
环境JSF2.0,Prettyfaces-jsf2-3.3.2,Primafaces 3.2,Tomcat 6.0.32,JDK 5.0
Environment JSF2.0, Prettyfaces-jsf2-3.3.2, Primafaces 3.2, Tomcat 6.0.32, JDK 5.0
谢谢
推荐答案FacesContext 就像是 ThreadLocal<T> 存储在执行当前HTTP Servlet请求的线程中.您肯定无法在自己产生的其他线程中访问它,这会中断JSF/Servlet的整个工作.相反,您应该将需要的 信息从可运行对象中的FacesContext传递到其构造函数,也许只是具体的bean实例本身,或其他也称为的对象会话属性.
The FacesContext is as being a ThreadLocal<T> stored in the thread which executes the current HTTP servlet request. You definitely can't access it in other threads which you spawned yourself, that would break the whole working of JSF/Servlet. You should instead be passing exactly that information which you need from the FacesContext in the runnable object to its constructor, perhaps just the concrete bean instance itself, or some other object which is in turn also referenced as a session attribute.
顺便说一句,在Java EE Web应用程序中产生非托管线程是一种气味.确保您真的了解自己在做什么.另请参见在JSF托管Bean中启动新线程是否安全?
By the way, spawning unmanaged threads in a Java EE web application is a smell. Make sure that you really understand what you're doing. See also Is it safe to start a new thread in a JSF managed bean?
更多推荐
无法从自我产生的线程访问FacesContext.getCurrentInstance()
发布评论