File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Thread.sleep() in stateless session beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Thread.sleep() in stateless session beans" Watch "Thread.sleep() in stateless session beans" New topic
Author

Thread.sleep() in stateless session beans

ritesh hk
Greenhorn

Joined: Mar 15, 2005
Posts: 6
Hi all,

I have been reading up that calling Thread.sleep() is not a good desgin in EJB.

But I was wondering why thread.sleep() is bad design when its used in a stateless bean or a MessageDrivenBean.

lemme know what u think.

Ritesh


-------------<br />SCJP 1.4 <br />SCWCD (just started)
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
Hi Ritesh,

I'm sure you know by now how difficult is to implement a good and optimal thread management in a multithreaded, eventualy distributed system (supposing you're not using a container and you'd have to do this work yourself). If in top of that you'd consider implementing a transactional system as well, where multiple local or global (distributed) transactions and concurrent threads are associated you can imagine the complexity of such system. If your EJB container will allow programmers to interfere with the thread management then an entire Pandora's box of malefic possibilities will be wilde openned. The result will mostly be catastrophic and will break the container's transaction management. As for the "innocent" thread.sleep() inside of an SLSB or MDB one might guess what could happen: the current thread that owns the bean instance will sleep. The transaction associated with that thread will block as well. Now you know that one of the secrets of designing optimal transactions is to make them as short as possible. Even worse, if your transaction uses a pesimistic locking approach than database locks will be held resulting in database deadlocks as well. On the other hand, if you'd consider global transactions and the 2PC protocol, you'd remark how complex the problem will become.
Regards.


I think, therefore I exist -- Rene Descartes
ritesh hk
Greenhorn

Joined: Mar 15, 2005
Posts: 6
I was thinking of the doing the Thread.sleep() to send only certain # of Messages (to the Jboss Msging service) at a time. ( I looked up a DB counter that was updated by each of the Child MDBs before they finished execution. That way I could enusre that X # of msgs were always processing)

But now I am sending all possible messages to the messaging service(jboss). The # of MDB instances in the pool has to be very low though. Hopefully the queue will hold a few thou msgs.

Anyways, I think there's gotta be a better solution than that too.

Thanks for the detailed reponse.
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
You're very welcome Ritesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread.sleep() in stateless session beans
 
Similar Threads
EJB3 DEployment descriptor
EJB question
JNDI naming and DI in EJB 3
WA #1.....word association
how to use System.currentTimeMillis() to check time for every 10 milli seconds?