cancel
Showing results for 
Search instead for 
Did you mean: 

Problem using Event Handlers

dattu_dave
Champ in-the-making
Champ in-the-making
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
1 REPLY 1

frederikherema1
Star Contributor
Star Contributor
You cannot rely on the DB to be consistent at the time the event-handlers are fired. Not only is the transaction not comitted, but some entities may have not been updated/flushed to the DB. Any entities that are related to the fired event can be accessed inside the event payload. This represents the current state, which *can* be different from the DB.

In your case, using the ActivitiEvent, casting to ActivitiEntityEvent and extracting the Task should get you to the assignee. There is a lot of documentation around the different types of events, maybe a good point to start…