4 Replies Latest reply on Jun 1, 2012 12:52 PM by Dino Antonelli

    Unable to complete a task when running the task in a separate thread

    Connie Yang Newbie

      Hi,

       

      I have a WorkItemHandler that can run the service task either in the main thread or in a separate thread.  This is my way of handling for-each parallel (aka fanout).  When the service task is run in a separate thread, I have been experiencing a NullPointerException with the following stack trace upon marking the task completed (via NodeInstanceImpl.triggerCompleted).  What am I missing?

       

      1. java.lang.NullPointerException

            at org.jbpm.process.instance.impl.ProcessInstanceImpl.getProcess(ProcessInstanceImpl.java:67)

            at org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl.getWorkflowProcess(WorkflowProcessInstanceImpl.java:180)

            at org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl.getNodeContainer(WorkflowProcessInstanceImpl.java:68)

            at org.jbpm.workflow.instance.impl.NodeInstanceImpl.getNode(NodeInstanceImpl.java:100)

            at org.jbpm.workflow.instance.node.ActionNodeInstance.getActionNode(ActionNodeInstance.java:35)

            at org.jbpm.workflow.instance.node.ActionNodeInstance.internalTrigger(ActionNodeInstance.java:43)

            at org.jbpm.workflow.instance.impl.NodeInstanceImpl.trigger(NodeInstanceImpl.java:122)

            at org.jbpm.workflow.instance.impl.NodeInstanceImpl.triggerConnection(NodeInstanceImpl.java:185)

            at org.jbpm.workflow.instance.impl.NodeInstanceImpl.triggerCompleted(NodeInstanceImpl.java:150)

            at org.jbpm.workflow.instance.impl.ExtendedNodeInstanceImpl.triggerCompleted(ExtendedNodeInstanceImpl.java:47)

            at org.jbpm.workflow.instance.node.StateBasedNodeInstance.triggerCompleted(StateBasedNodeInstance.java:162)

            at org.jbpm.workflow.instance.node.StateBasedNodeInstance.triggerCompleted(StateBasedNodeInstance.java:143)

            at org.jbpm.workflow.instance.node.WorkItemNodeInstance.triggerCompleted(WorkItemNodeInstance.java:239)

            at org.jbpm.workflow.instance.node.WorkItemNodeInstance.workItemCompleted(WorkItemNodeInstance.java:301)

            at org.jbpm.workflow.instance.node.WorkItemNodeInstance.signalEvent(WorkItemNodeInstance.java:277)

            at org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl.signalEvent(WorkflowProcessInstanceImpl.java:333)

            at org.drools.persistence.jpa.processinstance.JPAWorkItemManager.completeWorkItem(JPAWorkItemManager.java:117)

            at com.ebay.cloud.orchest.jbpm.LcmPojoWorkItemHandler$1.run(LcmPojoWorkItemHandler.java:95)

            at com.ebay.kernel.executor.TaskExecutor$Adapter.call(TaskExecutor.java:587)

       

      Thanks in advance!

      Connie

        • 1. Re: Unable to complete a task when running the task in a separate thread
          Kris Verlaenen Master

          You can get this exception when you are running some code inside the engine on a thread but outside the control of the engine, so without any transaction etc. 

           

          Note that, when you execute your work asynchronously in your work item handler, you shouldn't just call manager.completeWorkItemHandler (with manager being the WorkItemManager object that is passed in the executeWorkItem method) when the asynchronous result comes back, as that would result in you accessing the internal engine directly without wrapping it with a transaction.  In this case, you should use ksession.getWorkItemManager().completeWorkitem(..) instead, where you can pass a reference to your ksession in the constructor of your work item handler for example.

           

          Kris

          • 2. Re: Unable to complete a task when running the task in a separate thread
            Calvin Ling Newbie

            Thanks Kris! Since we had the ksession set in the work item handler already, the exception did go away after I changed to ksession.getWorkItemManager().completeWorkitem(..).  However, this completion event seems to have never taken effect? As we have .waitForCompletion(true) set for this work item node, but the flow was stuck in this node forever even though completeWorkitem was called. I checked that was the case via processinstancelog and nodeinstancelog.

            • 3. Re: Unable to complete a task when running the task in a separate thread
              Kris Verlaenen Master

              Do you have a small code sample that I could run that would show this behavior?
              As it's difficult to guess why a process would be "stuck" without looking at the actual code?

              If so, please open a JIRA and attach the sample project there, and I'll take a look.

               

              Kris

              • 4. Re: Unable to complete a task when running the task in a separate thread
                Dino Antonelli Newbie

                Hi Kris,

                I've the same problem as Connie. I've a different thread that is running the handler and I'm also experiencing the above NullPointerException during the its execution. I've passed, as you suggested, the ksession to the handler so I call "ksession.getWorkItemManager().completeWorkItem" to complete the execution. Anyway I'm getting the exception very frequently. I've read that there must be a transaction to have a successful complete but it seems that the thread that makes the call always run inside a transaction; below my snippet of code:

                 

                TransactionManager tm = TransactionManagerLocator.getInstance().locate();

                boolean transactionStarted = false;

                if(tm.getStatus() == Status.STATUS_NO_TRANSACTION) {

                          transactionStarted = true;

                          tm.begin();

                }

                this.ksession.getWorkItemManager().completeWorkItem(wi.getId(), inputParameters);

                if(transactionStarted) {

                          tm.commit();

                }

                 

                It seems that I've never execute the begin because thre's always an active transaction.

                Have you any idea?

                thnks in advance

                Dino