Couldn't access FacesContext.getCurrentInstance() from Self Spawned Thread

孤者浪人 提交于 2019-12-21 06:04:47

问题


Couldn't get "FacesContext.getCurrentInstance()" in self spawned thread.

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.

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.

Is there any way to get the "FacesContext.getCurrentInstance()" available in self spawned thread?

Environment JSF2.0, Prettyfaces-jsf2-3.3.2, Primafaces 3.2, Tomcat 6.0.32, JDK 5.0

thank you,


回答1:


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.

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?



来源:https://stackoverflow.com/questions/11266427/couldnt-access-facescontext-getcurrentinstance-from-self-spawned-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!