4

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,

Jeyan
  • 729
  • 1
  • 14
  • 27

1 Answers1

5

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?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555