Java Code Examples for org.activiti.engine.impl.interceptor.CommandContext#getEventSubscriptionEntityManager()

The following examples show how to use org.activiti.engine.impl.interceptor.CommandContext#getEventSubscriptionEntityManager() . 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: MessageEventReceivedCmd.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
protected Void execute(CommandContext commandContext, ExecutionEntity execution) {
  if (messageName == null) {
    throw new ActivitiIllegalArgumentException("messageName cannot be null");
  }
  
  if (Activiti5Util.isActiviti5ProcessDefinitionId(commandContext, execution.getProcessDefinitionId())) {
    Activiti5CompatibilityHandler activiti5CompatibilityHandler = Activiti5Util.getActiviti5CompatibilityHandler(); 
    activiti5CompatibilityHandler.messageEventReceived(messageName, executionId, payload, async);
    return null;
  }

  EventSubscriptionEntityManager eventSubscriptionEntityManager = commandContext.getEventSubscriptionEntityManager();
  List<EventSubscriptionEntity> eventSubscriptions = eventSubscriptionEntityManager.
      findEventSubscriptionsByNameAndExecution(MessageEventHandler.EVENT_HANDLER_TYPE, messageName, executionId);

  if (eventSubscriptions.isEmpty()) {
    throw new ActivitiException("Execution with id '" + executionId + "' does not have a subscription to a message event with name '" + messageName + "'");
  }

  // there can be only one:
  EventSubscriptionEntity eventSubscriptionEntity = eventSubscriptions.get(0);
  eventSubscriptionEntityManager.eventReceived(eventSubscriptionEntity, payload, async);

  return null;
}
 
Example 2
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 3
Source File: IntermediateThrowSignalEventActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
public void execute(DelegateExecution execution) {

    CommandContext commandContext = Context.getCommandContext();

    String eventSubscriptionName = null;
    if (signalEventName != null) {
      eventSubscriptionName = signalEventName;
    } else {
      Expression expressionObject = commandContext.getProcessEngineConfiguration().getExpressionManager().createExpression(signalExpression);
      eventSubscriptionName = expressionObject.getValue(execution).toString();
    }

    EventSubscriptionEntityManager eventSubscriptionEntityManager = commandContext.getEventSubscriptionEntityManager();
    List<SignalEventSubscriptionEntity> subscriptionEntities = null;
    if (processInstanceScope) {
      subscriptionEntities = eventSubscriptionEntityManager
          .findSignalEventSubscriptionsByProcessInstanceAndEventName(execution.getProcessInstanceId(), eventSubscriptionName);
    } else {
      subscriptionEntities = eventSubscriptionEntityManager
          .findSignalEventSubscriptionsByEventName(eventSubscriptionName, execution.getTenantId());
    }

    for (SignalEventSubscriptionEntity signalEventSubscriptionEntity : subscriptionEntities) {
      Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
          ActivitiEventBuilder.createSignalEvent(ActivitiEventType.ACTIVITY_SIGNALED, signalEventSubscriptionEntity.getActivityId(), eventSubscriptionName,
              null, signalEventSubscriptionEntity.getExecutionId(), signalEventSubscriptionEntity.getProcessInstanceId(),
              signalEventSubscriptionEntity.getProcessDefinitionId()));

      eventSubscriptionEntityManager.eventReceived(signalEventSubscriptionEntity, null, signalEventDefinition.isAsync());
    }

    Context.getAgenda().planTakeOutgoingSequenceFlowsOperation((ExecutionEntity) execution, true);
  }
 
Example 4
Source File: IntermediateThrowCompensationEventActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(DelegateExecution execution) {
  ThrowEvent throwEvent = (ThrowEvent) execution.getCurrentFlowElement();
  
  /*
   * From the BPMN 2.0 spec:
   * 
   * The Activity to be compensated MAY be supplied.
   *  
   * If an Activity is not supplied, then the compensation is broadcast to all completed Activities in 
   * the current Sub- Process (if present), or the entire Process instance (if at the global level). This “throws” the compensation.
   */
  final String activityRef = compensateEventDefinition.getActivityRef();
  
  CommandContext commandContext = Context.getCommandContext();
  EventSubscriptionEntityManager eventSubscriptionEntityManager = commandContext.getEventSubscriptionEntityManager();
  
  List<CompensateEventSubscriptionEntity> eventSubscriptions = new ArrayList<CompensateEventSubscriptionEntity>();
  if (StringUtils.isNotEmpty(activityRef)) {
    
    // If an activity ref is provided, only that activity is compensated
    eventSubscriptions.addAll(eventSubscriptionEntityManager
        .findCompensateEventSubscriptionsByProcessInstanceIdAndActivityId(execution.getProcessInstanceId(), activityRef));
    
  } else {
    
    // If no activity ref is provided, it is broadcast to the current sub process / process instance
    Process process = ProcessDefinitionUtil.getProcess(execution.getProcessDefinitionId());
    
    FlowElementsContainer flowElementsContainer = null;
    if (throwEvent.getSubProcess() == null) {
      flowElementsContainer = process;
    } else {
      flowElementsContainer = throwEvent.getSubProcess();
    }
    
    for (FlowElement flowElement : flowElementsContainer.getFlowElements()) {
      if (flowElement instanceof Activity) {
        eventSubscriptions.addAll(eventSubscriptionEntityManager
            .findCompensateEventSubscriptionsByProcessInstanceIdAndActivityId(execution.getProcessInstanceId(), flowElement.getId()));
      }
    }
    
  }
  
  if (eventSubscriptions.isEmpty()) {
    leave(execution);
  } else {
    // TODO: implement async (waitForCompletion=false in bpmn)
    ScopeUtil.throwCompensationEvent(eventSubscriptions, execution, false);
    leave(execution);
  }
}
 
Example 5
Source File: BoundaryCancelEventActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
@Override
public void trigger(DelegateExecution execution, String triggerName, Object triggerData) {
  BoundaryEvent boundaryEvent = (BoundaryEvent) execution.getCurrentFlowElement();
  
  CommandContext commandContext = Context.getCommandContext();
  ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();
  
  ExecutionEntity subProcessExecution = null;
  // TODO: this can be optimized. A full search in the all executions shouldn't be needed
  List<ExecutionEntity> processInstanceExecutions = executionEntityManager.findChildExecutionsByProcessInstanceId(execution.getProcessInstanceId());
  for (ExecutionEntity childExecution : processInstanceExecutions) {
    if (childExecution.getCurrentFlowElement() != null 
        && childExecution.getCurrentFlowElement().getId().equals(boundaryEvent.getAttachedToRefId())) {
      subProcessExecution = childExecution;
      break;
    }
  }
  
  if (subProcessExecution == null) {
    throw new ActivitiException("No execution found for sub process of boundary cancel event " + boundaryEvent.getId());
  }
  
  EventSubscriptionEntityManager eventSubscriptionEntityManager = commandContext.getEventSubscriptionEntityManager();
  List<CompensateEventSubscriptionEntity> eventSubscriptions = eventSubscriptionEntityManager.findCompensateEventSubscriptionsByExecutionId(subProcessExecution.getParentId());

  if (eventSubscriptions.isEmpty()) {
    leave(execution);
  } else {
    
    String deleteReason = DeleteReason.BOUNDARY_EVENT_INTERRUPTING + "(" + boundaryEvent.getId() + ")";
    
    // cancel boundary is always sync
    ScopeUtil.throwCompensationEvent(eventSubscriptions, execution, false);
    executionEntityManager.deleteExecutionAndRelatedData(subProcessExecution, deleteReason, false);
    if (subProcessExecution.getCurrentFlowElement() instanceof Activity) {
      Activity activity = (Activity) subProcessExecution.getCurrentFlowElement();
      if (activity.getLoopCharacteristics() != null) {
        ExecutionEntity miExecution = subProcessExecution.getParent();
        List<ExecutionEntity> miChildExecutions = executionEntityManager.findChildExecutionsByParentExecutionId(miExecution.getId());
        for (ExecutionEntity miChildExecution : miChildExecutions) {
          if (subProcessExecution.getId().equals(miChildExecution.getId()) == false && activity.getId().equals(miChildExecution.getCurrentActivityId())) {
            executionEntityManager.deleteExecutionAndRelatedData(miChildExecution, deleteReason, false);
          }
        }
      }
    }
    leave(execution);
  }
}
 
Example 6
Source File: ProcessEventJobHandler.java    From activiti6-boot2 with Apache License 2.0 3 votes vote down vote up
public void execute(JobEntity job, String configuration, ExecutionEntity execution, CommandContext commandContext) {
  
  EventSubscriptionEntityManager eventSubscriptionEntityManager = commandContext.getEventSubscriptionEntityManager();
  
  // lookup subscription:
  EventSubscriptionEntity eventSubscriptionEntity = eventSubscriptionEntityManager.findById(configuration);

  // if event subscription is null, ignore
  if (eventSubscriptionEntity != null) {
    eventSubscriptionEntityManager.eventReceived(eventSubscriptionEntity, null, false);
  }

}