i have a jsf application running on tomcat 6.0 and somewhere in the app i send e mails to some users.But sending mail slower than i thought, it causes lacks beetwen these related pages.
So my question is; is that a good(or doable) a way to give this proccess to another thread which i create, a thread that gets mail sending requests and put these in a queue and proccess these apart from main application.Hence the mail sending proccess would be out of the main flow and doesnt affect the app’s speed.
Yes, that’s definitely a good idea. You should only do it with an extreme care. Here’s some food for thought:
As you’re using Tomcat, which does not support EJB out the box (and thus
@Asynchronus@Singletonis out of question), I’d create an application scoped bean which holds anExecutorServiceto process the mail tasks. Here’s a kickoff example:This creates a single thread and puts the tasks in a queue. You can use it in normal beans as follows:
To learn more about
java.util.concurrentAPI, refer the official tutorial.