5 Replies Latest reply on Nov 11, 2003 5:45 PM by benstarr

    Calling Thread.sleep() in an EJB

    benstarr

      Is it acceptible to call Thread.sleep() in an EJB? The EJB spec (2.0) states that "The enterprise bean must not attempt to manage threads. The enterprise bean must not attempt to start, stop, suspend, or resume a thread; or to change a thread's priority or name. The enterprise bean must not attempt to manage thread groups." but it does not say anything specifically about calling the sleep method. Is there any good reason not to do this?

      I want to wait for a job to complete but a while loop would be a bit inefficient. My proposal is to sleep for a little while, check if the job is done and if not sleep some more up to a given maximum amount of time. Obivously an ideal solution would be to notify the EJB via JMS but that is not possible in this case, I have to poll the job to see if it is completed.

      Thanks.

        • 1. Re: Calling Thread.sleep() in an EJB
          frito

          Well,
          spec says you must not. Well, at least it is possible, but you must even know what you are doing (be familiar with the container behaviour of your server implementation!).
          I would prefer a clean solution of this problem. Waiting for an asynchron task to finish can be realized with Message Driven Beans or try to use the scheduler service to start an ejb which can check from time to time if the job is done.

          Greetings,
          Frito

          • 2. Re: Calling Thread.sleep() in an EJB
            benstarr

            Thanks for your reply. I guess my point was that although the spec says there are other things you mustn't do with a thread it doesn't explicitly say that you can't make it sleep for a little while. I don't see how it is much different from a method that just takes a while to complete since the threading model will cause it to suspend to give time to another thread anyway. However, I am wanting to use the scheduler to send a message to a queue in another part of my application but I haven't been able to find any examples of how to do it. I believe I have to use an MBean but I'm not sure how to get a handle on the Queue. Can I just use the default initial context? Obviously this code will execute outside of the container. I know how to create an MBean invoked by a scheduler, I'm just not sure how to do the rest. Any help would be appreciated. Thanks.

            • 3. Re: Calling Thread.sleep() in an EJB
              benstarr

              I have figured out that I can just create a scheduler that calls an MBean and then just access the Queue through JNDI as I would through a normal client (except that the JNDI namespace would not be mapped onto an ENC).

              • 4. Re: Calling Thread.sleep() in an EJB
                frito

                Well, I don't think you have to use both, MBean and JMS.
                My proposal: just use the scheduler or your own MBean. There you can do what you did in your EBJ before (thread sleep for a while and then polling for the asynchronous job to see if finished).

                Greetings,
                Frito

                • 5. Re: Calling Thread.sleep() in an EJB
                  benstarr

                  Thanks for the suggestion. Perhaps I could poll the job in the MBean and then notify a message-driven bean when it has completed or failed.