9 Replies Latest reply on Jul 19, 2006 4:17 AM by ProcessJunky Abs

    JBPM TaskCreation at RunTime

    ProcessJunky Abs Newbie

      Hi, I am having problem while getting taskInstance Id while creating Task at run time.
      This is what i have

      TASKNODE A which has task 1a and 2a , when a1 is completed process is transited(leaving task 2a dangling) to Node TASKNODE B , which at rumtime creats task 1b.

      I have action ActionHandler on " event type='task-create' " which is set to task a1, a2 and b1 and prints tasksInstance id using

      executionContext.getTaskInstance().getId()

      on a1 and a2 event relavent taskInstanceId are printed fine but on
      b1 i get taskinstanceId = 0 ( all this activity i am testing in a junit test and everything happing within same transaction and in the end i rollback)

      Why b1 taskinstance id is 0 t?????????? Any clues and help would be great...!!!

      follwing is my process defination
      < process-definition xmlns='' name='TestCallIssue'>
      < start-state name='start'>
      < transition name='' to='setup'>< /transition>
      < /start-state>
      < node name='setup'>
      < action class='com.sample.SetupProcessActionHandler'/>
      < transition name='' to='Test_Node'>< /transition>
      < /node>
      < task-node name='Test_Node' signal='never' create-tasks='false'>
      < event type='node-enter'>
      < action class='com.sample.testTask.OnNodeEnter'/>
      < /event>
      < task name='MG_CALL_AGREED'>
      < event type='task-create'>
      < action class='com.sample.testTask.OnTaskCreation'/>
      < /event>
      < event type='task-end'>
      < action class='com.sample.testTask.OnComplete'/>
      < /event>
      < /task>
      < task name='MG_CALL_SYSTASK_DATA_UPDATE'>
      < event type='task-create'>
      < action class='com.sample.testTask.OnTaskCreation'/>
      < /event>
      < event type='task-end'>
      < action class='com.sample.testTask.OnComplete'/>
      < /event>
      < /task>
      < transition name='on_update_msg' to='Test_Node2'>< /transition>
      < /task-node>

      < task-node name='Test_Node2' signal='never' create-tasks='false'>
      < event type='node-enter'>
      < action class='com.sample.testTask.OnNodeEnter2'/>
      < /event>

      < task name='MG_CALL_AGREED2'>
      < event type='task-create'>
      < action class='com.sample.testTask.OnTaskCreation'/>
      < /event>
      < event type='task-end'>
      < action class='com.sample.testTask.OnComplete'/>
      < /event>
      < /task>
      < transition name='' to='end'>< /transition>
      < /task-node>

      < node name='loopback'>
      < transition name='' to='Test_Node'>< /transition>
      < /node>

      < end-state name='end'>< /end-state>

      < /process-definition>
      < code>

        • 1. Re: JBPM TaskCreation at RunTime
          Chris OBrien Novice

          You say you are programmatically creating task 1b.

          Show us what code you are creating it with, you are probably missing setting something.

          • 2. Re: JBPM TaskCreation at RunTime
            ProcessJunky Abs Newbie

            This the code of ActionHandler which creats Tasks

            public class OnNodeEnter implements ActionHandler {

            private final static Log log = LogFactory.getLog( OnNodeEnter.class );

            public void execute(ExecutionContext executionContext) throws Exception {

            System.out.println("OnCreation-->- Start");

            Token token = executionContext.getToken();
            TaskMgmtInstance tmi = executionContext.getTaskMgmtInstance();
            TaskNode taskNode = (TaskNode) executionContext.getNode();


            Task doRecalc = taskNode.getTask("MG_CALL_AGREED");
            tmi.createTaskInstance(doRecalc, token);

            Task doRecalc3 = taskNode.getTask("MG_CALL_SYSTASK_DATA_UPDATE");
            tmi.createTaskInstance(doRecalc3, token);

            System.out.println("OnCreation-->- END");

            }

            }



            • 3. Re: JBPM TaskCreation at RunTime
              ProcessJunky Abs Newbie

              Here is the code in detial

              < process-definition xmlns='' name='TestProcess'>
              < start-state name='start'>
              < transition name='' to='setup'>< /transition>
              < /start-state>
              < node name='setup'>
              < action class='com.sample.SetupProcessActionHandler'/>
              < transition name='' to='Test_Node'>< /transition>
              < /node>
              < task-node name='A' signal='never' create-tasks='false'>
              < event type='node-enter'>
              < action class='com.sample.testTask.OnNodeEnter'/>
              < /event>
              < task name='TASK_A1'>
              < event type='task-create'>
              < action class='com.sample.testTask.OnTaskCreation'/>
              < /event>
              < event type='task-end'>
              < action class='com.sample.testTask.OnComplete'/>
              < /event>
              < /task>
              < task name='TASK_A2'>
              < event type='task-create'>
              < action class='com.sample.testTask.OnTaskCreation'/>
              < /event>
              < event type='task-end'>
              < action class='com.sample.testTask.OnComplete'/>
              < /event>
              < /task>
              < transition name='on_update_msg' to='Test_Node2'>< /transition>
              < /task-node>

              < task-node name='B' signal='never' create-tasks='false'>
              < event type='node-enter'>
              < action class='com.sample.testTask.OnNodeEnter2'/>
              < /event>

              < task name='TASK_B1'>
              < event type='task-create'>
              < action class='com.sample.testTask.OnTaskCreation'/>
              < /event>
              < event type='task-end'>
              < action class='com.sample.testTask.OnComplete'/>
              < /event>
              < /task>
              < transition name='' to='end'>< /transition>
              < /task-node>

              < node name='loopback'>
              < transition name='' to='Test_Node'>< /transition>
              < /node>

              < end-state name='end'>< /end-state>

              < /process-definition>
              < code>



              TASKNODE A Task creation handler
              public class OnNodeEnter implements ActionHandler {

              private final static Log log = LogFactory.getLog( OnNodeEnter.class );

              public void execute(ExecutionContext executionContext) throws Exception {

              System.out.println("OnCreation-->- Start");

              Token token = executionContext.getToken();
              TaskMgmtInstance tmi = executionContext.getTaskMgmtInstance();
              TaskNode taskNode = (TaskNode) executionContext.getNode();


              Task doRecalc = taskNode.getTask("TASK_A1");
              tmi.createTaskInstance(doRecalc, token);

              Task doRecalc3 = taskNode.getTask("TASK_A2");
              tmi.createTaskInstance(doRecalc3, token);

              System.out.println("OnCreation-->- END");

              }

              }



              TASKNODE B Task creation handler

              public class OnNodeEnter2 implements ActionHandler {

              private final static Log log = LogFactory.getLog( OnNodeEnter2.class );

              public void execute(ExecutionContext executionContext) throws Exception {

              System.out.println("OnCreation-->- Start");

              Token token = executionContext.getToken();
              TaskMgmtInstance tmi = executionContext.getTaskMgmtInstance();
              TaskNode taskNode = (TaskNode) executionContext.getNode();


              Task doRecalc = taskNode.getTask("TASK_B1");
              tmi.createTaskInstance(doRecalc, token);

              System.out.println("OnCreation-->- END");

              }

              }



              ActionHandler on task-create event on all tasks
              public class OnTaskCreation implements ActionHandler {


              public void execute(ExecutionContext executionContext) throws Exception {
              System.out.println("--> TaskInstacne Id ="+executionContext.getTaskInstance().getName());
              System.out.println("--> TaskInstacne Id ="+executionContext.getTaskInstance().getId());
              }
              }


              In case of TASK_A1 and TASK_A, OnTaskCreation prints proper Ids

              But In case of TASK_B1, OnTaskCreation prints 0






              • 4. Re: JBPM TaskCreation at RunTime
                Ronald van Kuijk Master

                you mix up names and code and exampleprocesses.... Please post a full new set where all code is related.

                e.g.

                Task doRecalc = taskNode.getTask("TASK_B1");
                refers to a non-existent task

                And also post the code where you execute the pd

                • 5. Re: JBPM TaskCreation at RunTime
                  David Hosier Novice

                  And use code tags instead of quote tags to format code snippets in your post. ;)

                  • 6. Re: JBPM TaskCreation at RunTime
                    ProcessJunky Abs Newbie

                    Hope it is clear now

                    <process-definition xmlns='' name='TestProcess'>
                     <start-state name='start'>
                     <transition name='' to='setup'>< /transition>
                     </start-state>
                     <node name='setup'>
                     <action class='com.sample.SetupProcessActionHandler'/>
                     <transition name='' to='TNODE1'>< /transition>
                     </node>
                    
                     <task-node name='TNODE1' signal='never' create-tasks='false'>
                     <event type='node-enter'>
                     <action class='com.sample.testTask.OnNodeEnter'/>
                     </event>
                     <task name='TASK_EAT'>
                     <event type='task-create'>
                     <action class='com.sample.testTask.OnTaskCreation'/>
                     </event>
                     <event type='task-end'>
                     <action class='com.sample.testTask.OnComplete'/>
                     </event>
                     </task>
                    
                     <task name='TASK_DRINK'>
                     <event type='task-create'>
                     <action class='com.sample.testTask.OnTaskCreation'/>
                     </event>
                     <event type='task-end'>
                     <action class='com.sample.testTask.OnComplete'/>
                     </event>
                     </task>
                    
                     <transition name='' to='TNODE2'>< /transition>
                    </task-node>
                    
                    <task-node name='TNODE2' signal='never' create-tasks='false'>
                     <event type='node-enter'>
                     <action class='com.sample.testTask.OnNodeEnter2'/>
                     </event>
                    
                     <task name='TASK_SLEEP'>
                     <event type='task-create'>
                     <action class='com.sample.testTask.OnTaskCreation'/>
                     </event>
                     <event type='task-end'>
                     <action class='com.sample.testTask.OnComplete'/>
                     </event>
                     </task>
                     <transition name='' to='end'>< /transition>
                    </task-node>
                    
                    <node name='loopback'>
                     <transition name='' to='TNODE1'></transition>
                    </node>
                    
                    <end-state name='end'></end-state>
                    
                    </process-definition>


                    TNODE1 Task Creation Handler


                    public class OnNodeEnter implements ActionHandler {
                    
                     public void execute(ExecutionContext executionContext) throws Exception {
                     System.out.println("OnNodeEnter-->- Start");
                    
                     Token token = executionContext.getToken();
                     TaskMgmtInstance tmi = executionContext.getTaskMgmtInstance();
                     TaskNode taskNode = (TaskNode) executionContext.getNode();
                    
                    
                     Task doEAT = taskNode.getTask("TASK_EAT");
                     tmi.createTaskInstance(doEAT, token);
                    
                     Task doDrink = taskNode.getTask("TASK_DRINK");
                     tmi.createTaskInstance(doDrink, token);
                    
                     System.out.println("OnNodeEnter-->- END");
                     }
                    }


                    TNODE2 Task Creation Handler


                    public class OnNodeEnter2 implements ActionHandler {
                    
                     public void execute(ExecutionContext executionContext) throws Exception {
                     System.out.println("OnNodeEnter2-->- Start");
                    
                     Token token = executionContext.getToken();
                     TaskMgmtInstance tmi = executionContext.getTaskMgmtInstance();
                     TaskNode taskNode = (TaskNode) executionContext.getNode();
                    
                     Task doSleep = taskNode.getTask("TASK_SLEEP");
                     tmi.createTaskInstance(doSleep, token);
                    
                     System.out.println("OnNodeEnter2-->- END");
                     }
                    }


                    ActionHandler on task-create event on all tasks

                    public class OnTaskCreation implements ActionHandler {
                    public void execute(ExecutionContext executionContext) throws Exception {
                    System.out.println("--> TaskInstacne Id ="+executionContext.getTaskInstance().getName());
                    System.out.println("--> TaskInstacne Id ="+executionContext.getTaskInstance().getId());
                    }
                    }



                    Process Exceution and TASK_EAT ending


                    Following test is within transaction and in the end i rollback transaction, so that in case of exception nothing is populated in tables.

                    public void testRunTimeTaskCreation() {
                     try {
                     loadProcessDefinition(processDef);
                    
                     Long processInstanceId = createNewProcessInstance();
                     ProcessInstance processInstance = findProcessInstance(processInstanceId);
                    
                     Collection<TaskInstance> tasks = processInstance.getTaskMgmtInstance().getTaskInstances();
                    
                    // only complete TASK_EAT which get created once process is created
                    
                     for (TaskInstance task : tasks) {
                     System.out.println("----------Task name = "+task.getName());
                     if (task.getName().equals("TASK_EAT")) {
                    
                     assignAndStart(task);
                     task.end(); // end the task and transit to TNODE2, leave TASK_DIRNK as it is (uncompleted)
                     break;
                     }
                     }
                     } catch (Exception e) {
                     e.printStackTrace();
                     fail();
                     }
                    
                     }
                    


                    OnCreation Actionhandler is giving following out put, if above test is run all togather in same transaction.


                    :
                    TaskInstacne Name = TASK_EAT
                    TaskInstacne Id = 700

                    TaskInstacne Name = TASK_DRINK
                    TaskInstacne Id = 701

                    .....
                    TaskInstacne Name = TASK_SLEEP
                    TaskInstacne Id = 0



                    if above test is divided in two test , 1. create process and commit trans then , 2. complete TASK_EAT is perform in different within separate trans output is.



                    :
                    TaskInstacne Name = TASK_EAT
                    TaskInstacne Id = 702

                    TaskInstacne Name = TASK_DRINK
                    TaskInstacne Id = 703
                    .
                    .
                    .
                    TaskInstacne Name = TASK_SLEEP
                    TaskInstacne Id = 704




                    • 7. Re: JBPM TaskCreation at RunTime
                      ProcessJunky Abs Newbie

                      forgot to mentioned transaction is not managed by JBPM, i am taking care of it.

                      • 8. Re: JBPM TaskCreation at RunTime
                        Ronald van Kuijk Master

                        you *have* to save the process in between these. Otherwise nothing is committed to the db. Tasks in different tasknodes are not supposed to be used in some kind of STP system.

                        • 9. Re: JBPM TaskCreation at RunTime
                          ProcessJunky Abs Newbie

                          I agree task should not be used in STP process they are meant to be UserTasks, in real time even in my process it wont be the case. However i am writing my Unit test which deploys, executes and test some flows by completing some task without commiting anything into the database becuase i do not wish my testcases to change the database state..

                          I like my test * Not * to presist or change state of application shared data (database). So if you have transactions it is much simpler and clean by doing rollback in teardown. OR 2nd approch would be to persist and then remove it.
                          it is possible to remove not only process defination, but process instance and other related enteries in other tables. ?

                          Also if TaskInstanceId = 0 then does it means that task is not actually created? i think 0 is defualt value of TaskInstance.Id (if i am not wrong)

                          abs