Discussions

EJB programming & troubleshooting: How to cancel a time consuming operation

  1. How to cancel a time consuming operation (5 messages)

    I have a Session Bean that creates objects containing statistic data from a large database through complex SQL statements. These statements can take quite som processing time. Therefore I need to have a "Cancel" button in the client (Swing), that interupts the operation if the users gets fed up waiting for the response.
    Is there any way of interrupting the the Session Bean and the database and ending their work when the user "Cancels"?
  2. first, let me say that i've never done this, so your mileage may vary...

    but i'd think you could create a stateful session bean, and then have a thread that calls your stateful session bean's performTimeConsumingOperation() method. if another thread calls your ejb's cancel() method, you can set a flag in your ejb that will tell performTimeConsumingOperation() to stop processing and return (or throw an exception, or whatever your desired behavior is).
  3. Can you really do that? I thought that concurrent access to a stateful session bean was not allowed and any attempt would get a RemoteException or EJBException or something along those lnes.
  4. Tim's suggestion should work. He is suggesting something like this:

    Client -> Stateful Session EJB -> Thread

    The stateful session EJB launches the long running process in a seperate thread, but keeps a reference to that thread. Since the process is running in the thread rather than the Session EJB, the Session EJB is done, so that the client can invoke a cancel method on the Session EJB.

    The code might look like this:

    public class LongProcess extends Thread {
      public void run() {
        while (!isInterrupted()) {
          // Do stuff ...
        }
      }
    }

    public class StatefulEJB implements SessionBean {
      private LongProcess process;

      public void go() {
        process = new LongProcess();
        process.start();
        // Method completes, returning control to client
      }

      public void cancel() {
        process.interrupt();
      }
    }
  5. I was under the impression that spawning threads in EJBs was strongly discouraged in app servers since app servers do thread management. I'm not sure if this is a suggestion for best practices or if it holds potential for problems in app servers, but I'm sure it something vendors don't recommend. Has anyone implemented a solution that uses an EJB with a thread spawned? If so, did you experience any anomolies?

    To address this problem, I am thinking in the lines of having the cancel button post an interruption message to a JMS topic for a message driven bean. The Session bean performing the long operation can regularly poll for a unique topic (based on client ID, for example) and immediately terminate further processing if it receives an interruption request through the message bean. Any thoughts on this?

    Thanks,
    Ruben
  6. Yes, threads are not allowed, according to the EJB spec. This is because it interferes with container operations, such as passivation and EJB destruction.

    There *may* be a way to do it with message driven beans... not sure I can picture how, though...


    -Tim.