无法从自我产生的线程访问FacesContext.getCurrentInstance()

编程入门 行业动态 更新时间:2024-10-25 19:32:51
本文介绍了无法从自我产生的线程访问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 就像是 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()

本文发布于:2023-10-07 17:37:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1470053.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   自我   getCurrentInstance   FacesContext

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!