1 2 Previous Next 19 Replies Latest reply on Nov 25, 2011 1:22 AM by ashpcs

    A question about rule task in jBPM5

    zhangjing2000

      I noticed that when process instance reachs rule task node, application needs to call ksession.fireAllRules to move process instanct forward.

      I wonder why need to issue such a call. I think it would be better that when process instance arrived, all rules will be fired automatically, just like when process instance reached a script task, the script will be run automatically.

        • 1. A question about rule task in jBPM5
          salaboy21

          Hi Jing,

          That's right.. jbpm5 is flexible enough to provide you that behavior too. Take a look at this tests:

          https://github.com/Salaboy/Drools_jBPM5-Training-Examples/blob/master/jbpm5/01-jBPM5-SimpleEmergencyServiceProcess/src/test/java/com/wordpress/salaboy/examples/SimpleEmergencyProcessTest.java

           

          More specifically the test called: reactiveProcessAndRulesTest ()

          It uses the engine in reactive mode calling the fireUntilHalt() method.

           

          Hope it helps!

          Greetings

          • 2. A question about rule task in jBPM5
            zhangjing2000

            Thanks Mauricio,

            I found in your example, you used a new thread to call fireUntilHalt, but it caused a NPE in my code during call to internalExecuteWorkItem method in JPAWorkItemManager. I wonder if the new thread won't able to leverage the JPA context from main thread.

             

             

             

            Exception in thread "Thread-12" org.drools.RuntimeDroolsException: Unexpected exception executing action org.jbpm.process.instance.event.DefaultSignalManager$SignalAction@1b9da92

                      at org.drools.common.AbstractWorkingMemory.executeQueuedActions(AbstractWorkingMemory.java:1000)

                      at org.drools.common.DefaultAgenda.fireUntilHalt(DefaultAgenda.java:1044)

                      at org.drools.common.AbstractWorkingMemory.fireUntilHalt(AbstractWorkingMemory.java:780)

                      at org.drools.common.AbstractWorkingMemory.fireUntilHalt(AbstractWorkingMemory.java:756)

                      at org.drools.command.runtime.rule.FireUntilHaltCommand$1.run(FireUntilHaltCommand.java:50)

                      at java.lang.Thread.run(Unknown Source)

            Caused by: java.lang.NullPointerException

            at org.drools.persistence.jpa.processinstance.JPAWorkItemManager.internalExecuteWorkItem(JPAWorkItemManager.java:43)

                      at org.jbpm.workflow.instance.node.WorkItemNodeInstance.internalTrigger(WorkItemNodeInstance.java:101)

                      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.RuleSetNodeInstance.signalEvent(RuleSetNodeInstance.java:73)

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

                      at org.jbpm.process.instance.event.DefaultSignalManager.internalSignalEvent(DefaultSignalManager.java:80)

                      at org.jbpm.process.instance.event.DefaultSignalManager$SignalAction.execute(DefaultSignalManager.java:175)

                      at org.drools.common.AbstractWorkingMemory.executeQueuedActions(AbstractWorkingMemory.java:998)

            • 3. A question about rule task in jBPM5
              salaboy21

              Can you share the knowledge logger output with us?

              I wanna understand what is happening there.

              Greetings.

              • 4. A question about rule task in jBPM5
                zhangjing2000

                Here is the log I got from Eclipse printout.

                 

                ACTIVATION CREATED rule:MyRule activationId:MyRule [0] declarations:  ruleflow-group: myRules

                BEFORE RULEFLOW STARTED process:Simple Process Test[id=SimpleProcessTest]

                BEFORE RULEFLOW NODE TRIGGERED node:StartProcess[id=1] process:Simple Process Test[id=SimpleProcessTest]

                null process:Simple Process Test[id=SimpleProcessTest]

                BEFORE RULEFLOW NODE TRIGGERED node:Task 1[id=2] process:Simple Process Test[id=SimpleProcessTest]

                execute work item org.drools.persistence.jpa.processinstance.JPAWorkItemManager@1b9ef36

                AFTER RULEFLOW NODE TRIGGERED node:Task 1[id=2] process:Simple Process Test[id=SimpleProcessTest]

                null process:Simple Process Test[id=SimpleProcessTest]

                AFTER RULEFLOW NODE TRIGGERED node:StartProcess[id=1] process:Simple Process Test[id=SimpleProcessTest]

                AFTER RULEFLOW STARTED process:Simple Process Test[id=SimpleProcessTest]

                ACTIVATION CREATED rule:MyRule activationId:MyRule [0] declarations:  ruleflow-group: myRules

                Completing the first Activity !

                complete work item 1

                null process:Simple Process Test[id=SimpleProcessTest]

                BEFORE RULEFLOW NODE TRIGGERED node:rule node[id=3] process:Simple Process Test[id=SimpleProcessTest]

                BEFORE RULEFLOW GROUP ACTIVATED group:myRules[size=1]

                AFTER RULEFLOW GROUP ACTIVATED group:myRules[size=1]

                AFTER RULEFLOW NODE TRIGGERED node:rule node[id=3] process:Simple Process Test[id=SimpleProcessTest]

                null process:Simple Process Test[id=SimpleProcessTest]

                BEFORE ACTIVATION FIRED rule:MyRule activationId:MyRule [0] declarations:  ruleflow-group: myRules

                my rules executed!

                AFTER ACTIVATION FIRED rule:MyRule activationId:MyRule [0] declarations:  ruleflow-group: myRules

                BEFORE RULEFLOW GROUP DEACTIVATED group:myRules[size=0]

                AFTER RULEFLOW GROUP DEACTIVATED group:myRules[size=0]

                null process:Simple Process Test[id=SimpleProcessTest]

                BEFORE RULEFLOW NODE TRIGGERED node:Task 2[id=4] process:Simple Process Test[id=SimpleProcessTest]

                Exception in thread "Thread-12" org.drools.RuntimeDroolsException: Unexpected exception executing action org.jbpm.process.instance.event.DefaultSignalManager$SignalAction@1b9da92

                          at org.drools.common.AbstractWorkingMemory.executeQueuedActions(AbstractWorkingMemory.java:1000)

                          at org.drools.common.DefaultAgenda.fireUntilHalt(DefaultAgenda.java:1044)

                          at org.drools.common.AbstractWorkingMemory.fireUntilHalt(AbstractWorkingMemory.java:780)

                          at org.drools.common.AbstractWorkingMemory.fireUntilHalt(AbstractWorkingMemory.java:756)

                          at org.drools.command.runtime.rule.FireUntilHaltCommand$1.run(FireUntilHaltCommand.java:50)

                          at java.lang.Thread.run(Unknown Source)

                Caused by: java.lang.NullPointerException

                          at org.drools.persistence.jpa.processinstance.JPAWorkItemManager.internalExecuteWorkItem(JPAWorkItemManager.java:43)

                          at org.jbpm.workflow.instance.node.WorkItemNodeInstance.internalTrigger(WorkItemNodeInstance.java:101)

                          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.RuleSetNodeInstance.signalEvent(RuleSetNodeInstance.java:73)

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

                          at org.jbpm.process.instance.event.DefaultSignalManager.internalSignalEvent(DefaultSignalManager.java:80)

                          at org.jbpm.process.instance.event.DefaultSignalManager$SignalAction.execute(DefaultSignalManager.java:175)

                          at org.drools.common.AbstractWorkingMemory.executeQueuedActions(AbstractWorkingMemory.java:998)

                          ... 5 more

                Completing the second Activity

                • 5. A question about rule task in jBPM5
                  zhangjing2000

                  Mauricio,

                  have you had chance to look at my issue again? I can send my source code if you need

                  • 6. A question about rule task in jBPM5
                    salaboy21

                    if you can create a maven project with a test that show that behavior I will download it and debug it here in my machine. But I'm very busy these days. If you can create that I will try to help asap.

                    Greetings.

                    • 7. Re: A question about rule task in jBPM5
                      zhangjing2000

                      Mauricio,

                      I have attached my source code with POM inside. Really appreciate your time and energy spent on this

                       

                      Have a good weekend!

                      • 8. A question about rule task in jBPM5
                        afisboy

                        Has there been any progress with this? I'm getting a similar stack trace when attempting a call to completeWorkItem() from a different thread using JPA sessions.

                        • 9. A question about rule task in jBPM5
                          salaboy21
                          • 10. Re: A question about rule task in jBPM5
                            dalgwen

                            Hello,

                             

                            Did you get to manage your issue?

                            I got exactly the same stack trace, with the same situation (call to completeWorkItem() from a different thread, and with JPA persistence). Don't know if is of some value, but the code of my WorkItemHandler looks like this :

                             

                             

                            @Override
                            public void executeWorkItem(final WorkItem workItem, final WorkItemManager manager) {
                            
                            
                            new Thread(new Runnable() {
                            public void run() {
                            
                            try {
                            //some code (...)
                            manager.completeWorkItem(workItem.getId(), null);
                            }
                            catch (Exception ex) {
                            // some code (...)
                            }
                            }
                            }).start();
                            
                            }
                            

                             

                            Curious fact : this error is triggerered in the second WorkItemHandler of my workflow, despite the fact that the first one use the same kind of code.

                            When I debug, i see that :

                             

                            PersistenceContext context = ((PersistenceContextManager) env.get( EnvironmentName.PERSISTENCE_CONTEXT_MANAGER )).getCommandScopedPersistenceContext();
                            context.persist( workItemInfo );
                            
                            ((WorkItemImpl) workItem).setId(workItemInfo.getId());
                            

                             

                            "workitemInfo" is ok, but its id is null, so the nullPointerException.

                             

                            I also debugged with the first WorkItem of my workflow, and then the id is ok after the context.persist(...) call.

                            Don't know why it works the first time and not after.

                             

                            Thanks for reading me.

                             

                            (Sorry for my english, it's not my mother langage ;-) !)

                            • 11. Re: A question about rule task in jBPM5
                              dalgwen

                              Hello again,

                               

                              Mauricio, I see that you requested a simple maven project to test the issue by yourself.

                              I did one simple test (I took most of the code from your "emergency" exemple).

                              With one JUnit execution you should be able to see the error. (maybe two or three executions because sometimes it crashes before, probably because of me messing things up)

                              Here it is : http://dl.free.fr/getfile.pl?file=/r6iubu9C

                              (the link is available one month, you have to click on "Telechargez ce fichier", sorry it's a french provider ;-) )

                               

                              For instance, here is the output of my Junit test:

                               

                              -----------------

                              Hibernate: insert into SessionInfo (id, lastModificationDate, rulesByteArray, startDate, OPTLOCK) values (null, ?, ?, ?, ?)

                              Hibernate: insert into ProcessInstanceInfo (InstanceId, lastModificationDate, lastReadDate, processId, processInstanceByteArray, startDate, state, OPTLOCK) values (null, ?, ?, ?, ?, ?, ?, ?)

                              BEFORE RULEFLOW STARTED process:Emergency Service Main Process[id=myworkflow]

                              BEFORE PROCESS NODE TRIGGERED node:StartProcess[id=1] process:Emergency Service Main Process[id=myworkflow]

                              BEFORE PROCESS NODE EXITED node:StartProcess[id=1] process:Emergency Service Main Process[id=myworkflow]

                              BEFORE PROCESS NODE TRIGGERED node:MyFirstWorkItem[id=8] process:Emergency Service Main Process[id=myworkflow]

                              Hibernate: insert into WorkItemInfo (workItemId, creationDate, name, processInstanceId, state, OPTLOCK, workItemByteArray) values (null, ?, ?, ?, ?, ?, ?)

                              AFTER PROCESS NODE TRIGGERED node:MyFirstWorkItem[id=8] process:Emergency Service Main Process[id=myworkflow]

                              AFTER PROCESS NODE TRIGGERED node:StartProcess[id=1] process:Emergency Service Main Process[id=myworkflow]

                              AFTER PROCESS NODE TRIGGERED node:StartProcess[id=1] process:Emergency Service Main Process[id=myworkflow]

                              AFTER RULEFLOW STARTED process:Emergency Service Main Process[id=myworkflow]

                              Work1

                              BEFORE PROCESS NODE EXITED node:MyFirstWorkItem[id=8] process:Emergency Service Main Process[id=myworkflow]

                              BEFORE PROCESS NODE TRIGGERED node:MyFirstWorkItem[id=9] process:Emergency Service Main Process[id=myworkflow]

                              java.lang.NullPointerException

                                  at org.drools.persistence.jpa.processinstance.JPAWorkItemManager.internalExecuteWorkItem(JPAWorkItemManager.java:43)

                                  at org.jbpm.workflow.instance.node.WorkItemNodeInstance.internalTrigger(WorkItemNodeInstance.java:105)

                                  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:119)

                                  at test.MyFirstWorkItemHandler$1.run(MyFirstWorkItemHandler.java:16)

                                  at java.lang.Thread.run(Thread.java:662)

                              Hibernate: insert into WorkItemInfo (workItemId, creationDate, name, processInstanceId, state, OPTLOCK, workItemByteArray) values (null, ?, ?, ?, ?, ?, ?)

                              Hibernate: update ProcessInstanceInfo set lastModificationDate=?, lastReadDate=?, processId=?, processInstanceByteArray=?, startDate=?, state=?, OPTLOCK=? where InstanceId=? and OPTLOCK=?

                              Hibernate: update WorkItemInfo set creationDate=?, name=?, processInstanceId=?, state=?, OPTLOCK=?, workItemByteArray=? where workItemId=? and OPTLOCK=?

                              Hibernate: update SessionInfo set lastModificationDate=?, rulesByteArray=?, startDate=?, OPTLOCK=? where id=? and OPTLOCK=?

                              Hibernate: update SessionInfo set lastModificationDate=?, rulesByteArray=?, startDate=?, OPTLOCK=? where id=? and OPTLOCK=?

                              -------------------------

                               

                              If you could get a look at it, I would be very pleased.

                              Greetings

                              • 12. Re: A question about rule task in jBPM5
                                mbraud

                                I've got exactly the same problem, did you find a solution?

                                 

                                Thanks

                                Maïté

                                • 13. Re: A question about rule task in jBPM5
                                  eaa

                                  Using fireUntilHalt() is not recomended nowadays becasue it causes a bunch of problems like race conditions, rollbacks in TX and even loose of controll of your execution (you don't know in which thread is your process instance running!).

                                  A better approach is to use an invocation pattern as the following one:

                                   

                                  Each time you want to interact with a process, you need to execute these steps:

                                  • get the session using JPAKnowledgeService (if you have different threads accessing the same session, it is recomended to queue all the access)
                                  • invoque the desidered action: startProcess(), signalEvent(), completeWorkItem(), etc.
                                  • invoque fireAllRules() this invocation is only useful if your process instances reached a Rule Node. If that is not the case, this invocation does nothing, so it is safe to execute it.
                                  • dispose your session.

                                   

                                  As far as I know, jBPM5 team and community are working in a more elegant solution that will encapsulate the call of fireAllRules() and make it transparent for the API user.

                                   

                                  Best Regards

                                  • 14. Re: A question about rule task in jBPM5
                                    mbraud

                                    Hi Esteban,

                                     

                                    Thanks for the quick feedback, I'm not sure how much my problem has to do with fireUntilHalt().

                                    I was replying to the post of dalgwen and Tom G regarding the org.drools.persistence.info.WorkItemInfo not being properly persisted.

                                     

                                    Curious fact : this error is triggerered in the second WorkItemHandler of my workflow, despite the fact that the first one use the same kind of code.

                                    When I debug, i see that :

                                     

                                    PersistenceContext context = ((PersistenceContextManager) env.get( EnvironmentName.PERSISTENCE_CONTEXT_MANAGER )).getCommandScopedPersistenceContext(); context.persist( workItemInfo );   ((WorkItemImpl) workItem).setId(workItemInfo.getId()); 

                                     

                                    "workitemInfo" is ok, but its id is null, so the nullPointerException.

                                     

                                    I also debugged with the first WorkItem of my workflow, and then the id is ok after the context.persist(...) call.

                                    Don't know why it works the first time and not after.

                                     

                                    Basically I've got a WorkItemHandler which wraps a call to an external webservice. Once finished, this webservice asynchronously sends me a webservice callback and I then call the WorkItemManager.completeWorkItem in order to queue the next step in the workflow.

                                     

                                    I've persisted the WorkItemManager in a static variable in order to be able to access it from the client and the server but maybe that's the problem.

                                     

                                    Thanks for the help.

                                    Maïté

                                    1 2 Previous Next