hi all , i have a servlet that process an object store it to db and call to an ejb method to send a mail notification ,
because the sendMail() method take some time(waits for mail server response) i called to the requestDispatcher to forward to the msgSent screeen because i dont want the client to wait to that method to end. like:
the problem is although i called the dispatcher, it doesnt forward the resposne till the sendMail() is done - how can i make the response to be created immediatly , and that the sendMail method will run in the background - im using Jboss4.0 thanks
Alen, You need to run the Mailer asynchronously. Instead of calling the mailer directly, call a class that puts the request in a queue or launches a thread for it. Then the request to sendMail can be run without waiting for a response.
I would go with JMS because it already has the functionality you are looking for.
As for the other solution, do you want the queue to be persisted if the mail server is down or if the whole app server crashes. For the former, it will happen by default. The queue in application scope will have a reference until the mail is sent and it will not be garbage collected. For the later, you need to write out the message somewhere (like a file or database.)
Joined: Sep 22, 2004
thanks for the idea ,i think what im going to do is that the mailer will implements a timedobject and will have a collection of msgs in as a queue . the mailer's timer will be started at the contextInitialized(). and every written message will be added as mailer.getQueue().add(msg), the queue will live as long as the application will, in case of a mail server crash , nothing happend , dont forget that in my application the messages are stored anyway , and the mail notification is just a nice feature , i dont think it worth using jms ,and double storing the messages .. thank u very very much for your help
subject: a call to a sendMail() method delay the servlet's reponse