Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet - best practice question

 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, in theory, you're not supposed to do your own thread management in EE containers.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic