无法从自我产生的线程访问FacesContext.getCurrentInstance() [英] Couldn't access FacesContext.getCurrentInstance() from Self Spawned Thread

查看:131
本文介绍了无法从自我产生的线程访问FacesContext.getCurrentInstance()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法在自生线程中获取"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 就像是

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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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