4 Replies Latest reply on Jul 8, 2009 10:23 AM by Ronald van Kuijk

    jBPM4: Manually created task can't block the execution

    Roger Yan Newbie

      I have a process definition as shown below:

      [img]http://scude.co.cc/pub_img/fork-process.png[/img]

      Process definition:

      <process name="EventListener" xmlns="http://jbpm.org/4.0/jpdl">
      
       <start g="100,16,48,48">
       <transition to="wait"/>
       </start>
      
       <state g="84,424,80,52" name="park"/>
       <fork g="100,180,48,48" name="fork1">
       <on event="start">
       <event-listener class="cc.scude.PlaceHomeworkListener"/>
       </on>
       <transition name="to submit" to="task1" g="-59,-21"/>
       <transition to="task2"/>
       </fork>
       <task assignee="stu" g="16,260,92,52" name="task1">
       <transition name="next" to="join1" g="-30,-21"/>
       </task>
       <task assignee="stu" g="140,260,92,52" name="task2">
       <transition to="join1"/>
       </task>
       <join g="100,344,48,48" name="join1">
       <transition to="park"/>
       </join>
       <state g="78,96,92,52" name="wait">
       <transition to="fork1"/>
       </state>
      </process>
      


      There are 2 tasks for user 'stu' after the fork node. And I have a EventListener attached to the fork node, which is used to create a task manually.

      public class PlaceHomeworkListener implements EventListener {
       @Override
       public void notify(EventListenerExecution execution) throws Exception {
       Environment env = Environment.getCurrent();
       Context ctx = env.getContext(Context.CONTEXTNAME_PROCESS_ENGINE);
       if (ctx instanceof WireContext) {
       WireContext context = (WireContext) ctx;
       TaskService taskService = context.get(TaskService.class);
      
       Task task = taskService.newTask();
       task.setAssignee("stu");
       task.setName("submit your homework");
       taskService.saveTask(task);
       }
       }
      }
      


      The task was created successfully by the listener and it's appeared in stu's personal task list. But it can't block the execution as tasks should be. When I completed the 2 tasks defined in xml, the execution goes to the next state 'park' whether I complete the task created in listener or not. Unit test code looks as below:
       ProcessInstance processInstance = executionService.startProcessInstanceByKey("EventListener");
       String pid = processInstance.getId();
      
       List<Task> tasks = taskService.findPersonalTasks("stu");
       assertEquals(0, tasks.size());
       Execution execution = processInstance.findActiveExecutionIn("wait");
       executionService.signalExecutionById(execution.getId());
      
       tasks = taskService.findPersonalTasks("stu");
       assertEquals(3, tasks.size()); // SUCCESS
      
       //Complete the first two tasks that were defined xml
       for (int i=0;i<2;i++) {
       Task t = tasks.get(i);
       taskService.completeTask(t.getDbid());
       }
       processInstance = executionService.findProcessInstanceById(pid);
      
       assertFalse(processInstance.isActive("park")); //FAILED
      
       tasks = taskService.findPersonalTasks("stu");
       debug("tasks after task completed:"+tasks.size());
       assertEquals(1, tasks.size());
      


      If I complete only one task, the last assertFalse line will success. It means that the task defined in xml will stop the execution while the task created in listener not.

      How to make the task created in code behaviour the same as that in xml?