11 Replies Latest reply on Oct 6, 2008 5:05 AM by ajanz

    dynamically create tasks

    ajanz

      i got the following code to generate dynamically tasks on a node.

       public Object execute(JbpmContext jbpmContext) throws Exception {
       // TODO Auto-generated method stub
       // TODO Auto-generated method stub
      
       Boolean b;
       try {
       b = Boolean.TRUE;
       ProcessInstance pi = jbpmContext
       .loadProcessInstance(processid);
       for (int i = 0; i < actors.size(); i++) {
       String actor = (String) actors.get(i);
       TaskInstance ti = pi.getTaskMgmtInstance()
       .createTaskInstance();
       ti.setActorId(actor);
       jbpmContext.save(ti);
       }
       jbpmContext.save(pi);
       } catch (Exception e) {
       // TODO: handle exception
       e.printStackTrace();
       b= Boolean.FALSE;
       }
       return b;
       }
      
      


      sometimes i got a nullpointer exception on

      TaskInstance ti = pi.getTaskMgmtInstance()

      the processid is set correct.

      any idea what might be wrong?




        • 1. Re: dynamically create tasks
          salaboy21

          One of the first things that come into my mind is that you probably have an InvalidStateException because you are saving the task in a loop.

          Check your StackTrace, and let us know if you find the problem.

          • 2. Re: dynamically create tasks
            ajanz

            no it was only called once.

            but i must call it because i create n tasks.

            or is there an other way to save it?

            • 3. Re: dynamically create tasks
              kukeltje

              like in another post... please make a unittest with embedded processdefinition that demonstrates (although it is not always here) the problem

              • 4. Re: dynamically create tasks
                ajanz

                do i need to save the taskinstance seperate?

                or would

                jbpmContext.save(pi);

                be enough?

                • 5. Re: dynamically create tasks
                  kukeltje

                   


                  or would

                  jbpmContext.save(pi);

                  be enough?



                  It should be

                  • 6. Re: dynamically create tasks
                    ajanz

                    ok here is the test

                     public void testRuntimeTaskCreation() {
                     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
                     "<process-definition>" +
                     "<start-state name='Start'>" +
                     " <transition to='in Bearbeitung' name='beginnen'></transition>" +
                     "</start-state> " +
                     " <task-node name='in Bearbeitung' signal='never'> " +
                     "<transition to='Ende' name='beenden' ></transition>" +
                     "<transition to='in Bearbeitung' name='nicht zuständig, weitergeleitet'></transition>"+
                     "<transition to='in Bearbeitung' name='beantwortet'></transition> " +
                     "<transition to='in Bearbeitung' name='nicht zuständig, zurück an Initiator'></transition>" +
                     "</task-node>"+
                     "<end-state name='Ende'></end-state> </process-definition>");
                    
                    
                     ProcessInstance processInstance = new ProcessInstance(processDefinition);
                     processInstance.signal("beginnen");
                    
                     TaskMgmtInstance tmi = (TaskMgmtInstance) processInstance.getInstance(TaskMgmtInstance.class);
                     TaskInstance ti = tmi.createTaskInstance();
                     if ( ti !=null) System.out.println("error creating task");
                     else System.out.println("Task created!");
                     }
                    
                    


                    • 7. Re: dynamically create tasks
                      kukeltje

                      You *DO* have to define a task in a task-node to create it runtime and have a create-tasks="false" attribute on the task-node.

                      Otherwise you have to runtime define the task to (can be done, but normally isn't)

                      In short: I'd suggest to use the testcases in the source of jBPM as a reference/examples. So much can be learned from them.

                      • 8. Re: dynamically create tasks
                        kukeltje

                        Have you tried running this test at all? You should use the JbpmContext when creating processinstances... not like you do now... this gives error in my unittest (which uses asserts, not system.outs etc...)

                        • 9. Re: dynamically create tasks
                          kukeltje

                          ah... you do not use persistency at all here.... I did... next time post a full unittest not just one testmethod

                          • 10. Re: dynamically create tasks
                            kukeltje

                            oh... and I never get the nullpointer exception (ran the test 100 times).... not even when there is no task in the task-node. task Instance IS NOT NULL, so your system.out is WRONG.... please be more precise next time

                            • 11. Re: dynamically create tasks
                              ajanz

                              great!!! thank you. now it works

                              - i redefined the task node with createtask=false
                              - add also a task in the node
                              - coded the function exactly as in RuntimeTaskCreationTest