8 Replies Latest reply on Aug 28, 2012 9:31 AM by rahulamt

    Asynchronous service tasks and transactions

    rahulamt

      jBPM5.2 documentation states the following.

       

      5.8.7. Multi-threading

       

      "When a service task is reached in the process, the engine will also invoke the handler of this service synchronously. This means that the engine will wait for the completeWorkItem(..) method to return before continuing execution. Therefore, it is important that your service handler executes your service asynchronously if its execution is not instantaneous. Image for example you want to invoke an external service. Since the delay of invoking this service remotely and waiting for the results might be too high, it might be a good idea to invoke this service asynchronously. This means that the handler will only invoke the service asynchronously and will notify the engine if the results are available...."

       

       

      But how to notify the engine that the execution is complete and the it can continue the execution. The suggested way is to use completeWorkItem(..). But If i try to invoke this in the thread that I have started, the entity manager is flushing the persistence context.

       

      If I explicitely flush the entity manager then for back-to-back service tasks the entity manager is not flushed even if I explicitely say EntityManager.flush().

       

      Please help!!

        • 1. Re: Asynchronous service tasks and transactions
          swiderski.maciej

          when instantiating your handler ensure that it will get access to knowledge session:

           

          new YouHandlerImpl(ksession)

           

          and then inside you handler, when it's done with processing use session to complete work item:

           

          session.getWorkItemManager().completeWorkItem(workItemId, result);

           

          HTH

          • 2. Re: Asynchronous service tasks and transactions
            rahulamt

            Hi,

             

            I doing it the same way but not directly. I am not registering my handler with kession. But the completeWorkItem(workItemId, result) i am calling on the same session. The call to this method is not inside the handler but inside an ejb that is called from the new thread that i am calling.

             

            My workItemHandler is as follows:

             

            public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {

             

             

                                final String operation = (String) workItem.getParameter("Operation");

             

             

                                final WorkItem internalWorkItem = workItem;

                                final WorkItemManager internalWorkItemManager = manager;

             

             

                                new Thread(new Runnable()

                                {

                                               public void run()

                                               {

                                                   

                                                    Map<String, Object> inParams = internalWorkItem.getParameters();

                                                    inParams.put("workItemId",Long.valueOf(internalWorkItem.getId()));

                                                   

                                                    inputParams.add((Serializable)inParams);

                                                    try

                                                    {

                                                         results = EJBCallLookUP.call(operation, inputParams);

                                                    }

                                                    catch(Exception e)

                                                    {

                                                          e.printStackTrace();

                                                    }

             

                                             }

                             }).start();

                 }

             

            in the EJBCallLookUP.call I am looking up the ejb for the operation and calling the method with operation name. Inside that operation i am calling the completeWorkItem(...) with the session.

             

             


            • 3. Re: Asynchronous service tasks and transactions
              swiderski.maciej

              and how do you pass session to the ejb? Do you get any errors or what is the problem?

              • 4. Re: Asynchronous service tasks and transactions
                rahulamt

                I am saving sessions in a static variables and retrieving sessions from those static variables inside ebj. The error is even if I say the completeWorkItem(..) inside ejb or outside it, but always in a separate thread, the persistence context is not flushed. Essentially the workiteminfo entry is not removed for that workitem and  nodeinstancelog  and processinstancelog tables are also not updated. So there is inconsistency in the process state. But if i perform a new operation( any operation that will do some database transaction on jbpm database) like starting new process instance the persistence context is flushed.

                • 5. Re: Asynchronous service tasks and transactions
                  swiderski.maciej

                  then it looks like transaction issue, transaction is not committed properly so entity manager is not flushed as I believe flush type is set to commit. Is you ejb CMT or BMT?

                  1 of 1 people found this helpful
                  • 6. Re: Asynchronous service tasks and transactions
                    rahulamt

                    ebj uses CMT. Sometimes transactions are not commited properly, but most of the times those are commited properly.

                    • 7. Re: Asynchronous service tasks and transactions
                      rahulamt

                      Hi Maciej,

                      the transactions are getting commited as far as application database transactions are concerned. I tried updating application database (this is different from the jBPM database) inside the EJB being called in a new thread. The updates to the that application database are getting commited properly. But the jBPM database is not updated. Is it the issue with hibernate or jBPM?

                       

                      Please help!!

                      • 8. Re: Asynchronous service tasks and transactions
                        rahulamt

                        One more update I would like to inform. If I have deployed my application, and then I am debugging it in eclipse using remote debugging then everything works fine but once i turn off eclipse remote debugging and run it normally then I am stuck at the same point. What could be the reason for this strange behaviour?