Timers in EJBs

Discussions

EJB design: Timers in EJBs

  1. Timers in EJBs (4 messages)


    The scenario is simple: a system, as part of its business logic, makes a request to something, and expects a response within 2 seconds (or minutes or days or whatever). If the response is not received, the system must alert an operator.

    How do I implement this timer within the J2EE framework?

    Threaded Messages (4)

  2. Timers in EJBs[ Go to top ]

    Have you considered using a JMS service call? I think it is possible to specify a timeout for synchronous calls, which may be exactly what you are looking for.
  3. Timers in EJBs[ Go to top ]

    I do not this it has anything to do with J2EE. In your monitoring application you just implement timeout mechanism and use it to see if the application responds in a timely manner, for example:

      TimedOp op = new CheckSomeFunction(...);
      if(!op.do(2000) {
        ... alert operator here ...
      }

    abstract class TimedOp implements Runnable {
      private boolean done = false;
      ...
      public boolean do(timeout) throws InterruptedException {
        boolean ok = false;
        (new Thread(this)).start();
        synchronized(this) {
          if(done) {
            ok = true;
          } else {
            wait(timeout);
            ok = done;
          }
        }
        return ok;
      }

      public void run() {
        doStuff();
        synchronized(this) {
          done = true;
          notify();
        }
      }

      // to be implemented by the user
      public abstract void doStuff();
    }
  4. Timers in EJBs[ Go to top ]

    Note, use of thread synchornization is strictly forbidden in EJB. So you cannot use the above approach in a portable way. I reccomend a synchornous JMS call with a timeout.

    Dave Wolf
    Internet Applications Division
    Sybase
  5. Timers in EJBs[ Go to top ]

    When you say "a system, as part of its business logic, makes a request to something, and expects a response within 2 seconds", who is making the asynchronous request? The client or an EJBean?

    In either case, to make asynchronous requests and callbacks involving EJBs, you CANNOT use a timer! Plain EJBs are simply synchronous; to make EJBs asynchronous you either have to spice it up with JMS or use EJB 2.0 MessageBeans.

    Gene Chuang
    Teach the world... Join Kiko!