0 Replies Latest reply on May 7, 2011 5:15 AM by piotrekde

    Two timeouts for one bean (order and design)

    piotrekde Newbie

      Hi,

       

      My @Service bean must perform some action every 30 seconds and every 2 minutes. To achieve it, I'd like to use standard EJB Timer Service API.

       

      The first thing is, that as far as I know, one bean cannot register itself for receiving more than 2 timeouts? I mean, that it can be only one ejbTimeout(Timer) method in case of implementing TimedObject interface, or only one method annotated with @Timeout, right?

      So I've decided to design it using observer-like pattern:

       

      Timeout A - every 30 seconds, Timeout B - every 2 minutes

      // observer - like
      @Service
      public class MyServiceBean {
        public void onTimeoutA(Timer t) {
          // do some stuff
        }
        public void onTimeoutB(Timer t) {
          // do some stuff.
        }
      }
      
      @Stateless
      public class TriggerA {
        @EJB
        MyService service;
      
        //(...)
      
        @Timeout // registered for timeout A
        public void timeoutA(Timer timer) {
          service.onTimeoutA(timer)
        }  
      }
      
      @Stateless
      public class TriggerB {
        @EJB
        MyService service;
      
        //(...)
      
        @Timeout // registered for timeout B
        public void timeoutB(Timer timer) {
          service.onTimeoutB(timer)
        }  
      }
      
      

       

      Is it any more elegant way to do so?

       

      The second question is about order of timeouts. It is easy to notice that every 2 minutes both timeout A and timeout B will be triggered. I'd like to make sure that in this case, timeout A will be fired _before_ timeout B:

      (30sec): timeoutA, (60sec): timeoutA, (90sec): timeoutA, (120sec): timeoutA, timeoutB

       

      How can I do it? (I use EJB 3.0 and JBoss)

       

      Thanks in advance, any help appreciated

      Piotr