4 Replies Latest reply on Jun 22, 2010 5:34 AM by johannesk

    How to recognize when async-node is finished?

    johannesk

      Hi,

       

      I'm trying to run a test with a simple process definition that contains the start state, an async node, a wait state and the end state. An example is attached.

       

      I create a process instance, remember the ID, obtain the root token and send a signal. The token then moves to the first node, which has set async to true. Then I save the processInstance and close the JbpmContext so the job message is sent to the job executor. The async node simply waits some time before it calls ExecutionContext.leaveNode() to send the token into a wait state.

       

      What I want to do now is: retrieve the process instance by the previously saved ID (with a newly created JbpmContext), get the root token and send a signal to make it take the transition to the end state.

       

      My problem is: How long do I have to wait after the token was sent to the async node if I want to be sure that the token has entered the next wait state and is no longer locked by the job? Is there any possibility to be notified?

       

      I tried to use a simple monitor where the main thread calls wait() after sending the first signal and the node implementation calls notifyAll() after leaveNode(), but the token is still locked by "Job(1)" when I send the second signal. It works if I wait some extra time (e. g. 250 ms) in the main thread before sending the second signal, but that is obviously not a good solution.

       

      Kind regards

       

      Johannes Krämer

        • 1. Re: How to recognize when async-node is finished?
          sebastian.s

          Maybe you want to use an EventListener on the transition?

          1 of 1 people found this helpful
          • 2. Re: How to recognize when async-node is finished?
            johannesk

            Sorry, I forgot to add that I'm using Jbpm 3.2.6 SP1. I can't see any EventListener in the API, is it maybe new to Jbpm4? Is there any possibility to use something like that with Jbpm3.

             

            Another thing I've tried is adding the monitor.notifyAll() as a separate Event to the outgoing transition. It still only works, if I wait some time after the notification to send the second signal.

             

            Has nobody ever tried to continue a process immediately after an async node has finished its execution? How should it work?

            • 3. Re: How to recognize when async-node is finished?
              mohreece

              Johannes,

               

              This is a problem we've been facing as well, e.g. in testing: how long do you wait for an async execution to end? There's no mechanism in jBPM 3 (as far as I know) to be notified that such an execution has ended, and determining the end of the execution is somewhat non-deterministic - it would take into account the time used for scheduling the async command(s) and executing the corresponding code.

               

              One mechanism available in Hibernate is to register a hook (an implementation of Synchronization) to the async transaction (something I tried to explain here: http://community.jboss.org/message/399790#399790), but that only works if you are able to set up that hook.

               

              An approach we've been using as well is to simply poll for the async execution to be ended (using relative short cycles with sleeps in between). I would agree if you'd say that's just butt-ugly, but we've found it works for us - lacking a better solution.

               

              Regards,

              Maurice

              • 4. Re: How to recognize when async-node is finished?
                johannesk

                The solution with a Synchronization object works like a charme. The async node now takes the JbpmContext from the ExecutionContext, then the Session and finally the hibernate Transaction to register a new Synchronization which performs a monitor.notifyAll() in afterCompletion(int).

                I've attached the modified version of the original test project in case someone wants to have a look at it.

                 

                Thanks a lot for your answer!