AnsweredAssumed Answered

Problem using Event Handlers

Question asked by dattu.dave@attuneinfocom.in on Apr 21, 2014
Latest reply on Apr 22, 2014 by frederikheremans1
Event Handlers are introduced In Activiti 5.15

I want to execute custom logic when the TASK_ASSIGNED  event is called
Whenever Task is Assigned to a user i want that Event listener should listen the TASK_ASSIGNED event and i want assignee(User) of task in the event TASK_ASSIGNED

I have created my Event Listener for that as below

public class MyEventListener implements ActivitiEventListener{

   
   @Override
   public boolean isFailOnException() {
      // TODO Auto-generated method stub
      return false;
   }

   @Override
   public void onEvent(ActivitiEvent event) {
      // TODO Auto-generated method stub
      switch(event.getType()){
         
         case TASK_ASSIGNED:
            
            System.out.println("From Event handler TASK_ASSIGNED:");
            TaskService ts = event.getEngineServices().getTaskService();
            System.out.println("No of Task: " + ts.createTaskQuery().processInstanceId(event.getProcessInstanceId()).count());
            List<Task> tasks =  ts.createTaskQuery().processInstanceId(event.getProcessInstanceId()).list();
            for(Task task : tasks)
            {
               System.out.println("Task Name: " + task.getName());
               System.out.println("Task Assignee: " + task.getAssignee());
               System.out.println("Task Description: " + task.getDescription());
            }
         break;
         
         case TASK_COMPLETED:
            System.out.println("From Event handler TASK_COMPLETED:");
            TaskService ts2 = event.getEngineServices().getTaskService();
            System.out.println("No of Task: " + ts2.createTaskQuery().processInstanceId(event.getProcessInstanceId()).count());
            tasks =  ts2.createTaskQuery().processInstanceId(event.getProcessInstanceId()).list();
            for(Task task : tasks)
            {
               System.out.println("Task Name: " + task.getName());
               System.out.println("Task Assignee: " + task.getAssignee());
               System.out.println("Task Description: " + task.getDescription());
            }
         break;
         
            
      }
      
      
   }

}



And I have generated Unit Test for Process


TaskService ts = activitiRule.getTaskService();
      System.out.println("From Junit Test");
      System.out.println("No of Tasks from UnitTest: " +ts.createTaskQuery().processInstanceId(processInstance.getId()).count() );
      List<Task> tasks =  ts.createTaskQuery().processInstanceId(processInstance.getId()).list();
      for(Task task : tasks)
      {
         System.out.println("Task Name: " + task.getName());
         System.out.println("Task Assignee: " + task.getAssignee());
         System.out.println("Task Description: " + task.getDescription());
   
         ts.complete(task.getId());
      }   

OUTPUT of JUnit Test:

From Event handler TASK_ASSIGNED:
No of Task: 0
From Junit Test
No of Tasks from UnitTest: 1
Task Name: Approval
Task Assignee: kermit
Task Description: This is Simple Approval Task
From Event handler TASK_COMPLETED:
No of Task: 1
Task Name: Approval
Task Assignee: kermit
Task Description: This is Simple Approval Task




So when I Run The unit Test I got Result From TASK_COMPLETED and from Junit Test. But, in TASK_ASSIGNED: case I got list of Task = 0

This may be because First Activiti call TASK_ASSIGNED event and then it update Activiti DB so when we query inside TASK_ASSIGNED we will get no of Task =0

But I want assignee in TASK_ASSIGNED event. so, when Task is assigned to user, I want to get that assignee and want to use some Custom logic on that Assignee

Please Help me For this problem

Outcomes