org.activiti.engine.delegate.event.ActivitiEvent Java Examples

The following examples show how to use org.activiti.engine.delegate.event.ActivitiEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ActivityEventsTest.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
@Deployment(resources = "org/activiti/engine/test/api/event/JobEventsTest.testJobEntityEvents.bpmn20.xml")
public void testActivityTimeOutEvent() {
  ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("testJobEvents");
  Job theJob = managementService.createTimerJobQuery().processInstanceId(processInstance.getId()).singleResult();
  assertNotNull(theJob);

  // Force timer to fire
  Calendar tomorrow = Calendar.getInstance();
  tomorrow.add(Calendar.DAY_OF_YEAR, 1);
  processEngineConfiguration.getClock().setCurrentTime(tomorrow.getTime());
  waitForJobExecutorToProcessAllJobs(2000, 1000);

  // Check timeout has been dispatched
  assertEquals(1, listener.getEventsReceived().size());
  ActivitiEvent activitiEvent = listener.getEventsReceived().get(0);
  assertEquals("ACTIVITY_CANCELLED event expected", ActivitiEventType.ACTIVITY_CANCELLED, activitiEvent.getType());
  ActivitiActivityCancelledEvent cancelledEvent = (ActivitiActivityCancelledEvent) activitiEvent;
  assertTrue("TIMER is the cause of the cancellation", cancelledEvent.getCause() instanceof JobEntity);
}
 
Example #2
Source File: ProcessInstanceEventsTest.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
@Deployment(resources = { "org/activiti/engine/test/api/runtime/oneTaskProcess.bpmn20.xml" })
public void testProcessInstanceCancelledEvents_cancel() throws Exception {
  ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess");
  assertNotNull(processInstance);
  listener.clearEventsReceived();

  runtimeService.deleteProcessInstance(processInstance.getId(), "delete_test");

  List<ActivitiEvent> processCancelledEvents = listener.filterEvents(ActivitiEventType.PROCESS_CANCELLED);
  assertEquals("ActivitiEventType.PROCESS_CANCELLED was expected 1 time.", 1, processCancelledEvents.size());
  ActivitiCancelledEvent processCancelledEvent = (ActivitiCancelledEvent) processCancelledEvents.get(0);
  assertTrue("The cause has to be the same as deleteProcessInstance method call", ActivitiCancelledEvent.class.isAssignableFrom(processCancelledEvent.getClass()));
  assertEquals("The process instance has to be the same as in deleteProcessInstance method call", processInstance.getId(), processCancelledEvent.getProcessInstanceId());
  assertEquals("The execution instance has to be the same as in deleteProcessInstance method call", processInstance.getId(), processCancelledEvent.getExecutionId());
  assertEquals("The cause has to be the same as in deleteProcessInstance method call", "delete_test", processCancelledEvent.getCause());

  List<ActivitiEvent> taskCancelledEvents = listener.filterEvents(ActivitiEventType.ACTIVITY_CANCELLED);
  assertEquals("ActivitiEventType.ACTIVITY_CANCELLED was expected 1 time.", 1, taskCancelledEvents.size());
  ActivitiActivityCancelledEvent activityCancelledEvent = (ActivitiActivityCancelledEvent) taskCancelledEvents.get(0);
  assertTrue("The cause has to be the same as deleteProcessInstance method call", ActivitiActivityCancelledEvent.class.isAssignableFrom(activityCancelledEvent.getClass()));
  assertEquals("The process instance has to be the same as in deleteProcessInstance method call", processInstance.getId(), activityCancelledEvent.getProcessInstanceId());
  assertEquals("The cause has to be the same as in deleteProcessInstance method call", "delete_test", activityCancelledEvent.getCause());

  listener.clearEventsReceived();
}
 
Example #3
Source File: ProcessInstanceEventsTest.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
@Deployment(resources = {
        "org/activiti5/engine/test/bpmn/event/end/TerminateEndEventTest.testTerminateInCallActivity.bpmn",
        "org/activiti5/engine/test/bpmn/event/end/TerminateEndEventTest.subProcessTerminate.bpmn"
})
public void testProcessInstanceTerminatedEvents_callActivity() throws Exception {
  ProcessInstance pi = runtimeService.startProcessInstanceByKey("terminateEndEventExample");

  // should terminate the called process and continue the parent
  long executionEntities = runtimeService.createExecutionQuery().count();
  assertEquals(1, executionEntities);

  Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).taskDefinitionKey("preNormalEnd").singleResult();
  taskService.complete(task.getId());

  assertProcessEnded(pi.getId());
  List<ActivitiEvent> processTerminatedEvents = listener.filterEvents(ActivitiEventType.PROCESS_CANCELLED);
  assertEquals("There should be exactly one ActivitiEventType.PROCESS_TERMINATED event after the task complete.", 1, processTerminatedEvents.size());
  ActivitiProcessCancelledEventImpl processCancelledEvent = (ActivitiProcessCancelledEventImpl) processTerminatedEvents.get(0);
  assertNotEquals(pi.getProcessInstanceId(), processCancelledEvent.getProcessInstanceId());
  assertThat(processCancelledEvent.getProcessDefinitionId(), containsString("terminateEndEventSubprocessExample"));

  List<ActivitiEvent> activityTerminatedEvents = listener.filterEvents(ActivitiEventType.ACTIVITY_CANCELLED);
  assertThat("There is no ActivitiEventType.ACTIVITY_CANCELLED event after the task complete.", activityTerminatedEvents.isEmpty(), is(true));
}
 
Example #4
Source File: CancelCallActivityByMessageTest.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
@Override
public void onEvent(ActivitiEvent event) {
	switch (event.getType()) {
	case ENTITY_CREATED:
		ActivitiEntityEvent entityEvent = (ActivitiEntityEvent) event;
		if (entityEvent.getEntity() instanceof ExecutionEntity) {
			eventsReceived.add(event);
		}
		break;
	case ACTIVITY_STARTED:
	case ACTIVITY_COMPLETED:
	case ACTIVITY_CANCELLED:
	case TASK_CREATED:
	case TASK_COMPLETED:
	case PROCESS_STARTED:
	case PROCESS_COMPLETED:
	case PROCESS_CANCELLED:
		eventsReceived.add(event);
		break;
	default:
		break;
	}
}
 
Example #5
Source File: SkipEventListener.java    From lemon with Apache License 2.0 6 votes vote down vote up
public void onEvent(ActivitiEvent event) {
    if (!(event instanceof ActivitiEntityEventImpl)) {
        return;
    }

    ActivitiEntityEventImpl activitiEntityEventImpl = (ActivitiEntityEventImpl) event;
    Object entity = activitiEntityEventImpl.getEntity();

    if (!(entity instanceof TaskEntity)) {
        return;
    }

    TaskEntity taskEntity = (TaskEntity) entity;

    try {
        switch (event.getType()) {
        case TASK_CREATED:
            logger.debug("create : {}", taskEntity.getId());
            this.onCreate(taskEntity);

            break;
        }
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
    }
}
 
Example #6
Source File: TaskAutoRedirectGlobalEventListener.java    From activiti-in-action-codes with Apache License 2.0 6 votes vote down vote up
@Override
public void onEvent(ActivitiEvent event) {
    ActivitiEntityEvent entityEvent = (ActivitiEntityEvent) event;
    Object entity = entityEvent.getEntity();
    if (entity instanceof TaskEntity) {
        TaskEntity task = (TaskEntity) entity;
        String originUserId = task.getAssignee();
        String newUserId = userMap.get(originUserId);
        if (StringUtils.isNotBlank(newUserId)) {
            task.setAssignee(newUserId);
            TaskService taskService = event.getEngineServices().getTaskService();
            String message = getClass().getName() + "-> 任务[" + task.getName() + "]的办理人[" +
                    originUserId + "]自动转办给了用户[" + newUserId + "]";
            taskService.addComment(task.getId(), task.getProcessInstanceId(), "redirect", message);
        }
    }
}
 
Example #7
Source File: DelegateExpressionActivitiEventListener.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
@Override
public void onEvent(ActivitiEvent event) {
	if(isValidEvent(event)) {
	  Object delegate = DelegateExpressionUtil.resolveDelegateExpression(expression, new NoExecutionVariableScope());
		if (delegate instanceof ActivitiEventListener) {
			// Cache result of isFailOnException() from delegate-instance until next
			// event is received. This prevents us from having to resolve the expression twice when
			// an error occurs.
			failOnException = ((ActivitiEventListener) delegate).isFailOnException();
			
			// Call the delegate
			((ActivitiEventListener) delegate).onEvent(event);
		} else {
			
			// Force failing, since the exception we're about to throw cannot be ignored, because it
			// did not originate from the listener itself
			failOnException = true;
			throw new ActivitiIllegalArgumentException("Delegate expression " + expression
					+ " did not resolve to an implementation of " + ActivitiEventListener.class.getName());
		}
	}
}
 
Example #8
Source File: ProcessInstanceEventsTest.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
@Deployment(resources = { "org/activiti/engine/test/bpmn/event/end/TerminateEndEventTest.testTerminateInCallActivity.bpmn",
    "org/activiti/engine/test/bpmn/event/end/TerminateEndEventTest.subProcessTerminate.bpmn" })
public void testProcessInstanceTerminatedEvents_callActivity() throws Exception {
  ProcessInstance pi = runtimeService.startProcessInstanceByKey("terminateEndEventExample");

  Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).taskDefinitionKey("preNormalEnd").singleResult();
  taskService.complete(task.getId());

  assertProcessEnded(pi.getId());
  List<ActivitiEvent> processTerminatedEvents = listener.filterEvents(ActivitiEventType.PROCESS_CANCELLED);
  assertEquals("There should be exactly one ActivitiEventType.PROCESS_CANCELLED event after the task complete.", 1, processTerminatedEvents.size());
  ActivitiProcessCancelledEventImpl processCancelledEvent = (ActivitiProcessCancelledEventImpl) processTerminatedEvents.get(0);
  assertNotEquals(pi.getProcessInstanceId(), processCancelledEvent.getProcessInstanceId());
  assertThat(processCancelledEvent.getProcessDefinitionId(), containsString("terminateEndEventSubprocessExample"));

}
 
Example #9
Source File: BaseDelegateEventListener.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected boolean isValidEvent(ActivitiEvent event) {
  boolean valid = false;
  if (entityClass != null) {
    if (event instanceof ActivitiEntityEvent) {
      Object entity = ((ActivitiEntityEvent) event).getEntity();
      if (entity != null) {
        valid = entityClass.isAssignableFrom(entity.getClass());
      }
    }
  } else {
    // If no class is specified, all events are valid
    valid = true;
  }
  return valid;
}
 
Example #10
Source File: ActivityEventsTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
/**
 * Test events related to error-events
 */
@Deployment
public void testActivityErrorEvents() throws Exception {
	ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("errorProcess");
	assertNotNull(processInstance);
	
	// Error-handling should have ended the process
	ProcessInstance afterErrorInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstance.getId())
			.singleResult();
	assertNull(afterErrorInstance);
	
	ActivitiErrorEvent errorEvent = null;
	
	for(ActivitiEvent event : listener.getEventsReceived()) {
		if(event instanceof ActivitiErrorEvent) {
			if(errorEvent == null) {
				errorEvent = (ActivitiErrorEvent) event;
			} else {
				fail("Only one ActivityErrorEvent expected");
			}
		}
	}
	
	assertNotNull(errorEvent);
	assertEquals(ActivitiEventType.ACTIVITY_ERROR_RECEIVED, errorEvent.getType());
	assertEquals("catchError", errorEvent.getActivityId());
	assertEquals("123", errorEvent.getErrorCode());
	assertEquals(processInstance.getId(), errorEvent.getProcessInstanceId());
	assertEquals(processInstance.getProcessDefinitionId(), errorEvent.getProcessDefinitionId());
	assertFalse(processInstance.getId().equals(errorEvent.getExecutionId()));
}
 
Example #11
Source File: SignalThrowingEventListener.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Override
public void onEvent(ActivitiEvent event) {
  if (isValidEvent(event)) {

    if (event.getProcessInstanceId() == null && processInstanceScope) {
      throw new ActivitiIllegalArgumentException("Cannot throw process-instance scoped signal, since the dispatched event is not part of an ongoing process instance");
    }

    CommandContext commandContext = Context.getCommandContext();
    EventSubscriptionEntityManager eventSubscriptionEntityManager = commandContext.getEventSubscriptionEntityManager();
    List<SignalEventSubscriptionEntity> subscriptionEntities = null;
    if (processInstanceScope) {
      subscriptionEntities = eventSubscriptionEntityManager.findSignalEventSubscriptionsByProcessInstanceAndEventName(event.getProcessInstanceId(), signalName);
    } else {
      String tenantId = null;
      if (event.getProcessDefinitionId() != null) {
        ProcessDefinition processDefinition = commandContext.getProcessEngineConfiguration().getDeploymentManager().findDeployedProcessDefinitionById(event.getProcessDefinitionId());
        tenantId = processDefinition.getTenantId();
      }
      subscriptionEntities = eventSubscriptionEntityManager.findSignalEventSubscriptionsByEventName(signalName, tenantId);
    }

    for (SignalEventSubscriptionEntity signalEventSubscriptionEntity : subscriptionEntities) {
      eventSubscriptionEntityManager.eventReceived(signalEventSubscriptionEntity, null, false);
    }
  }
}
 
Example #12
Source File: ProcessInstanceEventsTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Deployment(resources = {"org/activiti5/engine/test/api/runtime/oneTaskProcess.bpmn20.xml"})
public void testProcessInstanceCancelledEvents_cancell() throws Exception {
  ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess");
  assertNotNull(processInstance);
  listener.clearEventsReceived();

  runtimeService.deleteProcessInstance(processInstance.getId(), "delete_test");

  List<ActivitiEvent> processCancelledEvents = listener.filterEvents(ActivitiEventType.PROCESS_CANCELLED);
  assertEquals("ActivitiEventType.PROCESS_CANCELLED was expected 1 time.", 1, processCancelledEvents.size());
  ActivitiCancelledEvent processCancelledEvent = (ActivitiCancelledEvent) processCancelledEvents.get(0);
  assertTrue("The cause has to be the same as deleteProcessInstance method call", ActivitiCancelledEvent.class.isAssignableFrom(processCancelledEvent.getClass()));
  assertEquals("The process instance has to be the same as in deleteProcessInstance method call", processInstance.getId(), processCancelledEvent.getProcessInstanceId());
  assertEquals("The execution instance has to be the same as in deleteProcessInstance method call", processInstance.getId(), processCancelledEvent.getExecutionId());
  assertEquals("The cause has to be the same as in deleteProcessInstance method call", "delete_test", processCancelledEvent.getCause());

  List<ActivitiEvent> taskCancelledEvents = listener.filterEvents(ActivitiEventType.ACTIVITY_CANCELLED);
  assertEquals("ActivitiEventType.ACTIVITY_CANCELLED was expected 1 time.", 1, taskCancelledEvents.size());
  ActivitiActivityCancelledEvent activityCancelledEvent = (ActivitiActivityCancelledEvent) taskCancelledEvents.get(0);
  assertTrue("The cause has to be the same as deleteProcessInstance method call", ActivitiActivityCancelledEvent.class.isAssignableFrom(activityCancelledEvent.getClass()));
  assertEquals("The activity id has to be the same as processInstance activity", processInstance.getActivityId(), activityCancelledEvent.getActivityId());
  assertEquals("The process instance has to be the same as in deleteProcessInstance method call", processInstance.getId(), activityCancelledEvent.getProcessInstanceId());
  assertEquals("The execution instance has to be the same as in deleteProcessInstance method call", processInstance.getId(), activityCancelledEvent.getExecutionId());
  assertEquals("The cause has to be the same as in deleteProcessInstance method call", "delete_test", activityCancelledEvent.getCause());

  listener.clearEventsReceived();
}
 
Example #13
Source File: DebugInfoEntityEventListener.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected ExecutionEntity getExecutionEntity(ActivitiEvent event) {
  ActivitiEntityEvent entityEvent = (ActivitiEntityEvent) event;
  Object entity = entityEvent.getEntity();
  if (entity instanceof ExecutionEntity) {
    ExecutionEntity executionEntity = (ExecutionEntity) entity;
    return executionEntity;
  }
  return null;
}
 
Example #14
Source File: TestActivitiEntityEventTaskListener.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Override
public void onEvent(ActivitiEvent event) {
	super.onEvent(event);
	if (event instanceof ActivitiEntityEvent && Task.class.isAssignableFrom(((ActivitiEntityEvent) event).getEntity().getClass())) {
		tasks.add(copy((Task) ((ActivitiEntityEvent) event).getEntity()));
	}
}
 
Example #15
Source File: ActivitiEventSupport.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected void dispatchEvent(ActivitiEvent event, ActivitiEventListener listener) {
  try {
    listener.onEvent(event);
  } catch (Throwable t) {
    if (listener.isFailOnException()) {
      throw new ActivitiException("Exception while executing event-listener", t);
    } else {
      // Ignore the exception and continue notifying remaining listeners. The listener
      // explicitly states that the exception should not bubble up
      LOG.warn("Exception while executing event-listener, which was ignored", t);
    }
  }
}
 
Example #16
Source File: ProcessInstanceEventsTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Deployment(resources = {"org/activiti5/engine/test/api/runtime/nestedSubProcess.bpmn20.xml",
  "org/activiti5/engine/test/api/runtime/subProcess.bpmn20.xml"})
public void testProcessInstanceCancelledEvents_cancelProcessHierarchy() throws Exception {
  ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("nestedSimpleSubProcess");
  ProcessInstance subProcess = runtimeService.createProcessInstanceQuery().superProcessInstanceId(processInstance.getId()).singleResult();
  assertNotNull(processInstance);
  listener.clearEventsReceived();

  runtimeService.deleteProcessInstance(processInstance.getId(), "delete_test");

  List<ActivitiEvent> processCancelledEvents = listener.filterEvents(ActivitiEventType.PROCESS_CANCELLED);
  assertEquals("ActivitiEventType.PROCESS_CANCELLED was expected 2 times.", 2, processCancelledEvents.size());
  ActivitiCancelledEvent processCancelledEvent = (ActivitiCancelledEvent) processCancelledEvents.get(0);
  assertTrue("The cause has to be the same as deleteProcessInstance method call", ActivitiCancelledEvent.class.isAssignableFrom(processCancelledEvent.getClass()));
  assertEquals("The process instance has to be the same as in deleteProcessInstance method call", subProcess.getId(), processCancelledEvent.getProcessInstanceId());
  assertEquals("The execution instance has to be the same as in deleteProcessInstance method call", subProcess.getId(), processCancelledEvent.getExecutionId());
  assertEquals("The cause has to be the same as in deleteProcessInstance method call", "delete_test", processCancelledEvent.getCause());
  
  processCancelledEvent = (ActivitiCancelledEvent) processCancelledEvents.get(1);
  assertTrue("The cause has to be the same as deleteProcessInstance method call", ActivitiCancelledEvent.class.isAssignableFrom(processCancelledEvent.getClass()));
  assertEquals("The process instance has to be the same as in deleteProcessInstance method call", processInstance.getId(), processCancelledEvent.getProcessInstanceId());
  assertEquals("The execution instance has to be the same as in deleteProcessInstance method call", processInstance.getId(), processCancelledEvent.getExecutionId());
  assertEquals("The cause has to be the same as in deleteProcessInstance method call", "delete_test", processCancelledEvent.getCause());

  assertEquals("No task can be active for deleted process.", 0, this.taskService.createTaskQuery().processInstanceId(processInstance.getId()).count());

  List<ActivitiEvent> taskCancelledEvents = listener.filterEvents(ActivitiEventType.ACTIVITY_CANCELLED);
  assertEquals("ActivitiEventType.ACTIVITY_CANCELLED was expected 1 time.", 1, taskCancelledEvents.size());
  ActivitiActivityCancelledEvent activityCancelledEvent = (ActivitiActivityCancelledEvent) taskCancelledEvents.get(0);
  assertTrue("The cause has to be the same as deleteProcessInstance method call", ActivitiActivityCancelledEvent.class.isAssignableFrom(activityCancelledEvent.getClass()));
  assertEquals("The activity id has to point to the subprocess activity", subProcess.getActivityId(), activityCancelledEvent.getActivityId());
  assertEquals("The process instance has to point to the subprocess", subProcess.getId(), activityCancelledEvent.getProcessInstanceId());
  assertEquals("The execution instance has to point to the subprocess", subProcess.getId(), activityCancelledEvent.getExecutionId());
  assertEquals("The cause has to be the same as in deleteProcessInstance method call", "delete_test", activityCancelledEvent.getCause());


  listener.clearEventsReceived();
}
 
Example #17
Source File: EventLogger.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected EventLoggerEventHandler instantiateEventHandler(ActivitiEvent event,
    Class<? extends EventLoggerEventHandler> eventHandlerClass) {
try {
	EventLoggerEventHandler eventHandler = eventHandlerClass.newInstance();
	eventHandler.setTimeStamp(clock.getCurrentTime());
	eventHandler.setEvent(event);
	eventHandler.setObjectMapper(objectMapper);
	return eventHandler;
} catch (Exception e) {
	logger.warn("Could not instantiate " + eventHandlerClass + ", this is most likely a programmatic error");
}
return null;
}
 
Example #18
Source File: ProcessDefinitionEventsTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
/**
 * equals is not implemented.
 */
private boolean isEqual(ActivitiEntityEvent event1, ActivitiEvent activitiEvent) {
  if (activitiEvent instanceof ActivitiEntityEvent && event1.getType().equals(activitiEvent.getType())) {
    ActivitiEntityEvent activitiEntityEvent = (ActivitiEntityEvent) activitiEvent;
    if (activitiEntityEvent.getEntity().getClass().equals(event1.getEntity().getClass())) {
      return true;
    }
  }
  return false;
}
 
Example #19
Source File: JobEventsTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
private void checkEventCount(int expectedCount, ActivitiEventType eventType) {// count
                                                                              // timer
                                                                              // cancelled
                                                                              // events
  int timerCancelledCount = 0;
  List<ActivitiEvent> eventsReceived = listener.getEventsReceived();
  for (ActivitiEvent eventReceived : eventsReceived) {
    if (eventType.equals(eventReceived.getType())) {
      timerCancelledCount++;
    }
  }
  assertEquals(eventType.name() + " event was expected " + expectedCount + " times.", expectedCount, timerCancelledCount);
}
 
Example #20
Source File: ProcessInstanceEventsTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Deployment(resources = {
        "org/activiti5/engine/test/bpmn/event/end/TerminateEndEventTest.testTerminateInParentProcess.bpmn",
        "org/activiti5/engine/test/api/runtime/oneTaskProcess.bpmn20.xml"
})
public void testProcessInstanceTerminatedEvents_terminateInParentProcess() throws Exception {
  ProcessInstance pi = runtimeService.startProcessInstanceByKey("terminateParentProcess");

  // should terminate the called process and continue the parent
  Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).taskDefinitionKey("preTerminateEnd").singleResult();
  taskService.complete(task.getId());

  assertProcessEnded(pi.getId());
  List<ActivitiEvent> processTerminatedEvents = listener.filterEvents(ActivitiEventType.PROCESS_CANCELLED);
  assertEquals("There should be exactly one ActivitiEventType.PROCESS_TERMINATED event after the task complete.", 1, processTerminatedEvents.size());
  ActivitiProcessCancelledEventImpl processCancelledEvent = (ActivitiProcessCancelledEventImpl) processTerminatedEvents.get(0);
  assertThat(processCancelledEvent.getProcessInstanceId(), is(pi.getProcessInstanceId()));
  assertThat(processCancelledEvent.getProcessDefinitionId(), containsString("terminateParentProcess"));

  List<ActivitiEvent> activityTerminatedEvents = listener.filterEvents(ActivitiEventType.ACTIVITY_CANCELLED);
  assertThat("3 activities must be cancelled.", activityTerminatedEvents.size(), is(3));
  ActivitiActivityCancelledEventImpl activityEvent = (ActivitiActivityCancelledEventImpl) activityTerminatedEvents.get(0);
  assertThat("The user task must be terminated in the called sub process.", activityEvent.getActivityId(), is("theTask"));
  assertThat("The cause must be terminate end event", ((ActivityImpl) activityEvent.getCause()).getId(), is("EndEvent_3"));
  activityEvent = (ActivitiActivityCancelledEventImpl) activityTerminatedEvents.get(1);
  assertThat("The call activity must be terminated", activityEvent.getActivityId(), is("CallActivity_1"));
  assertThat("The cause must be terminate end event", ((ActivityImpl) activityEvent.getCause()).getId(), is("EndEvent_3"));
  activityEvent = (ActivitiActivityCancelledEventImpl) activityTerminatedEvents.get(2);
  assertThat("The gateway must be terminated", activityEvent.getActivityId(), is("ParallelGateway_1"));
  assertThat("The cause must be terminate end event", ((ActivityImpl) activityEvent.getCause()).getId(), is("EndEvent_3"));
}
 
Example #21
Source File: ProcessInstanceEventsTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
public List<ActivitiEvent> filterEvents(ActivitiEventType eventType) {// count timer cancelled events
  List<ActivitiEvent> filteredEvents = new ArrayList<ActivitiEvent>();
  List<ActivitiEvent> eventsReceived = listener.getEventsReceived();
  for (ActivitiEvent eventReceived : eventsReceived) {
    if (eventType.equals(eventReceived.getType())) {
      filteredEvents.add(eventReceived);
    }
  }
  return filteredEvents;
}
 
Example #22
Source File: Activiti6ExecutionTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Test
@Deployment
public void testSubProcessEvents() {
  SubProcessEventListener listener = new SubProcessEventListener();
  processEngineConfiguration.getEventDispatcher().addEventListener(listener);
  
  ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("subProcessEvents");
  
  Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
  taskService.complete(task.getId());
  
  Execution subProcessExecution = runtimeService.createExecutionQuery().processInstanceId(processInstance.getId()).activityId("subProcess").singleResult();
  
  task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
  taskService.complete(task.getId());
  
  task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
  taskService.complete(task.getId());
  
  assertProcessEnded(processInstance.getId());
  
  // Verify Events
  List<ActivitiEvent> events = listener.getEventsReceived();
  assertEquals(2, events.size());
  
  ActivitiActivityEvent event = (ActivitiActivityEvent) events.get(0);
  assertEquals("subProcess", event.getActivityType());
  assertEquals(subProcessExecution.getId(), event.getExecutionId());
  
  event = (ActivitiActivityEvent) events.get(1);
  assertEquals("subProcess", event.getActivityType());
  assertEquals(subProcessExecution.getId(), event.getExecutionId());
  
  processEngineConfiguration.getEventDispatcher().removeEventListener(listener);
}
 
Example #23
Source File: ProcessInstanceEventsTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Deployment(resources = {
        "org/activiti5/engine/test/bpmn/event/error/BoundaryErrorEventTest.testCatchErrorOnCallActivity-parent.bpmn20.xml",
        "org/activiti5/engine/test/bpmn/event/error/BoundaryErrorEventTest.subprocess.bpmn20.xml"
})
public void testProcessCompletedEvents_callActivityErrorEndEvent() throws Exception {
  ProcessInstance pi = runtimeService.startProcessInstanceByKey("catchErrorOnCallActivity");

  Task task = taskService.createTaskQuery().singleResult();
  assertEquals("Task in subprocess", task.getName());
  List<ProcessInstance> subProcesses = runtimeService.createProcessInstanceQuery().superProcessInstanceId(pi.getId()).list();
  assertEquals(1, subProcesses.size());

  // Completing the task will reach the end error event,
  // which is caught on the call activity boundary
  taskService.complete(task.getId());

  List<ActivitiEvent> processCompletedEvents = listener.filterEvents(ActivitiEventType.PROCESS_COMPLETED_WITH_ERROR_END_EVENT);
  assertEquals("There should be exactly one ActivitiEventType.PROCESS_COMPLETED_WITH_ERROR_END_EVENT event after the task complete.", 1, processCompletedEvents.size());
  ActivitiEntityEvent processCompletedEvent = (ActivitiEntityEvent) processCompletedEvents.get(0);
  assertEquals(subProcesses.get(0).getId(), processCompletedEvent.getExecutionId());

  task = taskService.createTaskQuery().singleResult();
  assertEquals("Escalated Task", task.getName());

  // Completing the task will end the process instance
  taskService.complete(task.getId());
  assertProcessEnded(pi.getId());
}
 
Example #24
Source File: ProcessInstanceEventsTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Deployment(resources = "org/activiti/engine/test/bpmn/event/end/TerminateEndEventTest.testProcessTerminate.bpmn")
public void testProcessInstanceTerminatedEvents() throws Exception {
  ProcessInstance pi = runtimeService.startProcessInstanceByKey("terminateEndEventExample");

  long executionEntities = runtimeService.createExecutionQuery().processInstanceId(pi.getId()).count();
  assertEquals(3, executionEntities);

  Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).taskDefinitionKey("preTerminateTask").singleResult();
  taskService.complete(task.getId());

  List<ActivitiEvent> processTerminatedEvents = listener.filterEvents(ActivitiEventType.PROCESS_CANCELLED);
  assertEquals("There should be exactly one ActivitiEventType.PROCESS_CANCELLED event after the task complete.", 1, processTerminatedEvents.size());
  ActivitiProcessCancelledEventImpl activitiEvent = (ActivitiProcessCancelledEventImpl) processTerminatedEvents.get(0);
  assertThat(activitiEvent.getProcessInstanceId(), is(pi.getProcessInstanceId()));

  List<ActivitiEvent> activityTerminatedEvents = listener.filterEvents(ActivitiEventType.ACTIVITY_CANCELLED);
  assertThat("There should be exactly two ActivitiEventType.ACTIVITY_CANCELLED event after the task complete.", activityTerminatedEvents.size(), is(2));

  for (ActivitiEvent event : activityTerminatedEvents) {
    
    ActivitiActivityCancelledEventImpl activityEvent = (ActivitiActivityCancelledEventImpl) event;
    if (activityEvent.getActivityId().equals("preNormalTerminateTask")) {
      assertThat("The user task must be terminated", activityEvent.getActivityId(), is("preNormalTerminateTask"));
      assertThat("The cause must be terminate end event", ((FlowNode) activityEvent.getCause()).getId(), is("EndEvent_2"));
    } else if (activityEvent.getActivityId().equals("EndEvent_2")) {
      assertThat("The end event must be terminated", activityEvent.getActivityId(), is("EndEvent_2"));
      assertThat("The cause must be terminate end event", ((FlowNode) activityEvent.getCause()).getId(), is("EndEvent_2"));
    }
    
  }
  
}
 
Example #25
Source File: ProcessInstanceEventsTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Deployment(resources = { "org/activiti/engine/test/api/runtime/oneTaskProcess.bpmn20.xml" })
public void testProcessInstanceTerminatedEvents_complete() throws Exception {
  ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess");
  assertNotNull(processInstance);

  Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
  taskService.complete(task.getId());

  List<ActivitiEvent> processTerminatedEvents = listener.filterEvents(ActivitiEventType.PROCESS_CANCELLED);
  assertEquals("There should be no ActivitiEventType.PROCESS_TERMINATED event after process complete.", 0, processTerminatedEvents.size());
}
 
Example #26
Source File: ProcessDefinitionEventsTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected void assertSequence(ActivitiEntityEvent before, ActivitiEntityEvent after) {
  int beforeIndex = 0;
  int afterIndex = 0;
  for (int index = 0; index < listener.getEventsReceived().size(); index++) {
    ActivitiEvent activitiEvent = listener.getEventsReceived().get(index);

    if (isEqual(before, activitiEvent))
      beforeIndex = index;
    if (isEqual(after, activitiEvent))
      afterIndex = index;
  }
  assertTrue(beforeIndex < afterIndex);
}
 
Example #27
Source File: TestHistoricActivityEventListener.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Override
public void onEvent(ActivitiEvent event) {
  if (event.getType().equals(ActivitiEventType.HISTORIC_PROCESS_INSTANCE_CREATED)
      || event.getType().equals(ActivitiEventType.HISTORIC_PROCESS_INSTANCE_ENDED)
      || event.getType().equals(ActivitiEventType.HISTORIC_ACTIVITY_INSTANCE_CREATED)
      || event.getType().equals(ActivitiEventType.HISTORIC_ACTIVITY_INSTANCE_ENDED)) {
        eventsReceived.add(event);
      }
}
 
Example #28
Source File: JobEventsTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected void checkEventContext(ActivitiEvent event, Job entity, boolean scopeExecutionExpected) {
	assertEquals(entity.getProcessInstanceId(), event.getProcessInstanceId());
	assertEquals(entity.getProcessDefinitionId(), event.getProcessDefinitionId());
	if (scopeExecutionExpected) {
		assertEquals(entity.getExecutionId(), event.getExecutionId());
	} else {
		assertEquals(entity.getProcessInstanceId(), event.getExecutionId());
	}
	
	assertTrue(event instanceof ActivitiEntityEvent);
	ActivitiEntityEvent entityEvent = (ActivitiEntityEvent) event;
	assertTrue(entityEvent.getEntity() instanceof Job);
	assertEquals(entity.getId(), ((Job) entityEvent.getEntity()).getId());
}
 
Example #29
Source File: JobEventsTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
private List<ActivitiEvent> filterEvents(ActivitiEventType eventType) {
  List<ActivitiEvent> eventsReceived = listener.getEventsReceived();
  List<ActivitiEvent> filteredEvents = new ArrayList<>();
  for (ActivitiEvent eventReceived : eventsReceived) {
    if (eventType.equals(eventReceived.getType())) {
      filteredEvents.add(eventReceived);
    }
  }
  return filteredEvents;
}
 
Example #30
Source File: ProcessInstanceEventsTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Deployment(resources = { "org/activiti/engine/test/api/runtime/nestedSubProcess.bpmn20.xml", "org/activiti/engine/test/api/runtime/subProcess.bpmn20.xml" })
public void testProcessInstanceCancelledEvents_cancelProcessHierarchy() throws Exception {
  ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("nestedSimpleSubProcess");
  ProcessInstance subProcess = runtimeService.createProcessInstanceQuery().superProcessInstanceId(processInstance.getId()).singleResult();
  assertNotNull(processInstance);
  listener.clearEventsReceived();

  runtimeService.deleteProcessInstance(processInstance.getId(), "delete_test");

  List<ActivitiEvent> processCancelledEvents = listener.filterEvents(ActivitiEventType.PROCESS_CANCELLED);
  assertEquals("ActivitiEventType.PROCESS_CANCELLED was expected 2 times.", 2, processCancelledEvents.size());
  ActivitiCancelledEvent processCancelledEvent = (ActivitiCancelledEvent) processCancelledEvents.get(0);
  assertTrue("The cause has to be the same as deleteProcessInstance method call", ActivitiCancelledEvent.class.isAssignableFrom(processCancelledEvent.getClass()));
  assertEquals("The process instance has to be the same as in deleteProcessInstance method call", subProcess.getId(), processCancelledEvent.getProcessInstanceId());
  assertEquals("The execution instance has to be the same as in deleteProcessInstance method call", subProcess.getId(), processCancelledEvent.getExecutionId());
  assertEquals("The cause has to be the same as in deleteProcessInstance method call", "delete_test", processCancelledEvent.getCause());
  
  processCancelledEvent = (ActivitiCancelledEvent) processCancelledEvents.get(1);
  assertTrue("The cause has to be the same as deleteProcessInstance method call", ActivitiCancelledEvent.class.isAssignableFrom(processCancelledEvent.getClass()));
  assertEquals("The process instance has to be the same as in deleteProcessInstance method call", processInstance.getId(), processCancelledEvent.getProcessInstanceId());
  assertEquals("The execution instance has to be the same as in deleteProcessInstance method call", processInstance.getId(), processCancelledEvent.getExecutionId());
  assertEquals("The cause has to be the same as in deleteProcessInstance method call", "delete_test", processCancelledEvent.getCause());

  assertEquals("No task can be active for deleted process.", 0, this.taskService.createTaskQuery().processInstanceId(processInstance.getId()).count());

  List<ActivitiEvent> taskCancelledEvents = listener.filterEvents(ActivitiEventType.ACTIVITY_CANCELLED);
  assertEquals("ActivitiEventType.ACTIVITY_CANCELLED was expected 1 time.", 1, taskCancelledEvents.size());
  ActivitiActivityCancelledEvent activityCancelledEvent = (ActivitiActivityCancelledEvent) taskCancelledEvents.get(0);
  assertTrue("The cause has to be the same as deleteProcessInstance method call", ActivitiActivityCancelledEvent.class.isAssignableFrom(activityCancelledEvent.getClass()));
  assertEquals("The process instance has to point to the subprocess", subProcess.getId(), activityCancelledEvent.getProcessInstanceId());
  assertEquals("The cause has to be the same as in deleteProcessInstance method call", "delete_test", activityCancelledEvent.getCause());

  listener.clearEventsReceived();
}