Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, that's definitely a good idea. You should only do it with an extreme care. Here's some food for thought:</p> <ul> <li><a href="https://stackoverflow.com/questions/6149919/is-it-safe-to-start-a-new-thread-in-a-jsf-managed-bean">Is it safe to start a new thread in a JSF managed bean?</a></li> <li><a href="https://stackoverflow.com/questions/7499534/applicationscope-bean-that-uses-a-timertask-sheduler-good-or-bad">Spawning threads in a JSF managed bean for scheduled tasks using a timer</a></li> </ul> <p>As you're using Tomcat, which does not support EJB out the box (and thus <code>@Asynchronus</code> <code>@Singleton</code> is out of question), I'd create an application scoped bean which holds an <a href="http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html" rel="nofollow noreferrer"><code>ExecutorService</code></a> to process the mail tasks. Here's a kickoff example:</p> <pre><code>@ManagedBean(eager=true) @ApplicationScoped public class TaskManager { private ExecutorService executor; @PostConstruct public void init() { executor = Executors.newSingleThreadExecutor(); } public &lt;T&gt; Future&lt;T&gt; submit(Callable&lt;T&gt; task) { return executor.submit(task); } // Or just void submit(Runnable task) if you want fire-and-forget. @PreDestroy public void destroy() { executor.shutdown(); } } </code></pre> <p>This creates a single thread and puts the tasks in a queue. You can use it in normal beans as follows:</p> <pre><code>@ManagedBean @RequestScoped public class Register { @ManagedProperty("#{taskManager}") private TaskManager taskManager; public void submit() { // ... taskManager.submit(new MailTask(mail)); // You might want to hold the return value in some Future&lt;Result&gt;, but // you should store it in view or session scope in order to get result // later. Note that the thread will block whenever you call get() on it. // You can just ignore it altogether (as the current example is doing). } } </code></pre> <p>To learn more about <code>java.util.concurrent</code> API, refer the <a href="http://download.oracle.com/javase/tutorial/essential/concurrency/index.html" rel="nofollow noreferrer">official tutorial</a>.</p>
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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