This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Servlets and the fly likes Servlet - best practice question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet - best practice question" Watch "Servlet - best practice question" New topic
Author

Servlet - best practice question

James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
Once my servlet has sent its response to the client, it may do some other stuff like modifying a database and/or sending an email.

Q: Is it best practice to do that extra stuff in a new Thread (so my processRequest() method returns asap) or is that not necessary/adviseable?

Thanks,
James
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41629
    
  55
Wouldn't it be better not to send the response until the other stuff is done, so that you can return an error message in case something goes wrong?


Ping & DNS - my free Android networking tools app
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 686

once your request go to the servlet your servlet makes new thread to process your request. At that time you have no holdover your code. So I think it is not necessary and adviseable ...


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
Thanks for the replies.

In this case, whether or not the other stuff successfully completes wouldn't affect the content sent to the client, so that isn't really the issue.

What I'm really getting at is that once I've called...

...could it cause a slower response to the the client if I don't do my other stuff in a new Thread??
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If it's been flushed and closed I don't really see how.

Seems like it'd be *really* easy to test by putting in a sleep.
James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
Yes, I put in a sleep and it was fine, but I have heard of some people experiencing problems with this sort of thing... So I was just wondering if there was a particular best practice to adopt... I'll keep it in the same Thread anyway. Thanks all.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Well, in theory, you're not supposed to do your own thread management in EE containers.
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
James Hodgkiss wrote:Once my servlet has sent its response to the client, it may do some other stuff like modifying a database and/or sending an email. Q: Is it best practice to do that extra stuff in a new Thread (so my processRequest() method returns asap) or is that not necessary/adviseable?
Thanks,
James

I don't know about the Best Practice part, but I see nothing wrong with using a thread to avoid blocking the user's response. If the failure of the db call, or whatever, will affect the response, then obviously you would *want* to wait until that completes or fails before sending the response. But if the success or failure of that action is transparent to the user, then using a spawned thread seems fine to me.
M. Justin
Greenhorn

Joined: Sep 03, 2009
Posts: 16
James Hodgkiss wrote:Once my servlet has sent its response to the client, it may do some other stuff like modifying a database and/or sending an email.

Q: Is it best practice to do that extra stuff in a new Thread (so my processRequest() method returns asap) or is that not necessary/adviseable?

Thanks,
James


A very common solution for asynchronous processing in an EE container would be a JMS message queue. The servlet just passes the necessary information as a JMS message, and sends it to a queue. A message driven bean then would pick up the message and process the database / email stuff.


Michael Justin - http://www.habarisoft.com/
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
M. Justin wrote:A very common solution for asynchronous processing in an EE container would be a JMS message queue. The servlet just passes the necessary information as a JMS message, and sends it to a queue. A message driven bean then would pick up the message and process the database / email stuff.

I agree.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet - best practice question