aspose file tools*
The moose likes Servlets and the fly likes a call to a sendMail() method  delay the servlet's reponse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "a call to a sendMail() method  delay the servlet Watch "a call to a sendMail() method  delay the servlet New topic
Author

a call to a sendMail() method delay the servlet's reponse

alen agmon
Greenhorn

Joined: Sep 22, 2004
Posts: 11
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:

Object obj = .getObject();
persister.store(obj);

request.getRequestDispatcher("/msgSent.jsp").forward(....);

Mailer.sendMail(obj);

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
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30762
    
156

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
alen agmon
Greenhorn

Joined: Sep 22, 2004
Posts: 11
thanks for the reply,
as i see it i have 2 options:

i thought about using JMS , but i think thats overheaded -

the other option is to create an object in the application scope
to hold the queue . and put the mailer on timer on that queue.

how can i make it persisted ?
and not to be collected as garbage (in case of which the mail server can be down sometimes and i need the queue to stay alive)
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30762
    
156

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.)
alen agmon
Greenhorn

Joined: Sep 22, 2004
Posts: 11
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

Alen.
 
 
subject: a call to a sendMail() method delay the servlet's reponse