I could not find a definitive answer to whether it is safe to spawn threads within session-scoped JSF managed beans. The thread needs to call methods on the stateless EJB instance (that was dependency-injected to the managed bean).
The background is that we have a report that takes a long time to generate. This caused the HTTP request to time-out due to server settings we can’t change. So the idea is to start a new thread and let it generate the report and to temporarily store it. In the meantime the JSF page shows a progress bar, polls the managed bean till the generation is complete and then makes a second request to download the stored report. This seems to work, but I would like to be sure what I’m doing is not a hack.
Introduction
Spawning threads from within a session scoped managed bean is not necessarily a hack as long as it does the job you want. But spawning threads at its own needs to be done with extreme care. The code should not be written that way that a single user can for example spawn an unlimited amount of threads per session and/or that the threads continue running even after the session get destroyed. It would blow up your application sooner or later.
The code needs to be written that way that you can ensure that an user can for example never spawn more than one background thread per session and that the thread is guaranteed to get interrupted whenever the session get destroyed. For multiple tasks within a session you need to queue the tasks.
Also, all those threads should preferably be served by a common thread pool so that you can put a limit on the total amount of spawned threads at application level.
Managing threads is thus a very delicate task. That’s why you’d better use the built-in facilities rather than homegrowing your own with
new Thread()and friends. The average Java EE application server offers a container managed thread pool which you can utilize via among others EJB’s@Asynchronousand@Schedule. To be container independent (read: Tomcat-friendly), you can also use the Java 1.5’s Util ConcurrentExecutorServiceandScheduledExecutorServicefor this.Below examples assume Java EE 6+ with EJB.
Fire and forget a task on form submit
Asynchronously fetch the model on page load
In case you’re using JSF utility library OmniFaces, this could be done even faster if you annotate the managed bean with
@Eager.Schedule background jobs on application start
Continuously update application wide model in background
See also: