Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Introduction</h2> <p>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.</p> <p>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. </p> <p>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. The average Java EE application server offers a container managed thread pool which you can utilize via among others EJB's <a href="http://docs.oracle.com/javaee/6/api/javax/ejb/Asynchronous.html" rel="noreferrer"><code>@Asynchronous</code></a> and <a href="http://docs.oracle.com/javaee/6/api/javax/ejb/Schedule.html" rel="noreferrer"><code>@Schedule</code></a>. To be container independent, you can also use the Java 1.5's Util Concurrent <a href="http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html" rel="noreferrer"><code>ExecutorService</code></a> and <a href="http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html" rel="noreferrer"><code>ScheduledExecutorService</code></a> for this.</p> <p>Below examples assume Java EE 6+ with EJB.</p> <h2>Fire and forget a task on form submit</h2> <pre><code>@Named @RequestScoped // Or @ViewScoped public class Bean { @EJB private SomeService someService; public void submit() { someService.asyncTask(); // ... (this code will immediately continue without waiting) } } </code></pre> <pre><code>@Stateless public class SomeService { @Asynchronous public void asyncTask() { // ... } } </code></pre> <h2>Asynchronously fetch the model on page load</h2> <pre><code>@Named @RequestScoped // Or @ViewScoped public class Bean { private Future&lt;List&lt;Entity&gt;&gt; asyncEntities; @EJB private EntityService entityService; @PostConstruct public void init() { asyncEntities = entityService.asyncList(); // ... (this code will immediately continue without waiting) } public List&lt;Entity&gt; getEntities() { try { return asyncEntities.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new FacesException(e); } catch (ExecutionException e) { throw new FacesException(e); } } } </code></pre> <pre><code>@Stateless public class EntityService { @PersistenceContext private EntityManager entityManager; @Asynchronous public Future&lt;List&lt;Entity&gt;&gt; asyncList() { List&lt;Entity&gt; entities = entityManager .createQuery("SELECT e FROM Entity e", Entity.class) .getResultList(); return new AsyncResult&lt;&gt;(entities); } } </code></pre> <p>In case you're using JSF utility library <a href="http://omnifaces.org" rel="noreferrer">OmniFaces</a>, this could be done even faster if you annotate the managed bean with <a href="http://showcase.omnifaces.org/cdi/Eager" rel="noreferrer"><code>@Eager</code></a>.</p> <h2>Schedule background jobs on application start</h2> <pre><code>@Singleton public class BackgroundJobManager { @Schedule(hour="0", minute="0", second="0", persistent=false) public void someDailyJob() { // ... (runs every start of day) } @Schedule(hour="*/1", minute="0", second="0", persistent=false) public void someHourlyJob() { // ... (runs every hour of day) } @Schedule(hour="*", minute="*/15", second="0", persistent=false) public void someQuarterlyJob() { // ... (runs every 15th minute of hour) } @Schedule(hour="*", minute="*", second="*/30", persistent=false) public void someHalfminutelyJob() { // ... (runs every 30th second of minute) } } </code></pre> <h2>Continuously update application wide model in background</h2> <pre><code>@Named @RequestScoped // Or @ViewScoped public class Bean { @EJB private SomeTop100Manager someTop100Manager; public List&lt;Some&gt; getSomeTop100() { return someTop100Manager.list(); } } </code></pre> <pre><code>@Singleton @ConcurrencyManagement(BEAN) public class SomeTop100Manager { @PersistenceContext private EntityManager entityManager; private List&lt;Some&gt; top100; @PostConstruct @Schedule(hour="*", minute="*/1", second="0", persistent=false) public void load() { top100 = entityManager .createNamedQuery("Some.top100", Some.class) .getResultList(); } public List&lt;Some&gt; list() { return top100; } } </code></pre> <h3>See also:</h3> <ul> <li><a href="https://stackoverflow.com/questions/7499534/spawning-threads-in-a-jsf-managed-bean-for-scheduled-tasks-using-a-timer">Spawning threads in a JSF managed bean for scheduled tasks using a timer</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload