Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to implement a "worker" bean? (continuous loop)

843829Oct 29 2003 — edited Nov 5 2003
I am new to J2EE development so please forgive my lack of knowledge. Yes, I have read the J2EE tutorial, the JMS tutorial and parts of the J2EE and EJB specifications. I am wanting to deploy on JBoss 3.2.2 (J2EE 1.3).

To my question: I want to implement a "worker" bean that just does some work in a continuous loop. The bean will need to respond to start and stop commands. When the bean has been started it should continually perform the work until it receives a stop command. It can then be started at a later date. I am wondering what the best way to implement this scenario within a J2EE application is?

My proposal is to create a message-driven bean that can be sent start and stop commands (messages). This will in turn call methods on a stateless session bean to actually perform the work. The start message will cause the message-driven bean to call a start method in the stateless session bean. This method will invoke a continous loop to perform the work. On each iteration it will monitor a static boolean to check whether it has been requested to stop. The stop message will cause the message-driven bean to call a stop method in the stateless session bean. This method will set the static boolean to false causing the start method to exit the continous loop and return (on the next iteration). Obviously the start call from the message-driven bean will only return once a stop method has been called.

I am not sure whether this is the best approach. Obviously there are threading implications. There could be multiple instances of the stateless session-bean if the start method is called more than once? Maybe if the boolean is already true a subsequent call to the start method should do nothing? Also, I'm not sure how the ejbRemove method would be called by the container if another method was in a continuous loop. I don't know if this scenario is recommended in J2EE.

I guess the alternative would be to implement a client which continuously calls the method in the stateless session bean to perform the work and then I would just start and stop the client to start and stop performing the work. This way the implementation is still in the container. I guess I wasn't wanting to have to implement a client like that but I'm still going to need to implement a client to send the start and stop commands to the message-driven bean anyway.

Any help would be much appreciated!

Thanks,

Ben
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 3 2003
Added on Oct 29 2003
2 comments
200 views