Search in sources :

Example 16 with MessageEventSubscriptionEntity

use of org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntity in project Activiti by Activiti.

the class StartProcessInstanceByMessageCmd method execute.

public ProcessInstance execute(CommandContext commandContext) {
    if (messageName == null) {
        throw new ActivitiIllegalArgumentException("Cannot start process instance by message: message name is null");
    }
    MessageEventSubscriptionEntity messageEventSubscription = commandContext.getEventSubscriptionEntityManager().findMessageStartEventSubscriptionByName(messageName, tenantId);
    if (messageEventSubscription == null) {
        throw new ActivitiObjectNotFoundException("Cannot start process instance by message: no subscription to message with name '" + messageName + "' found.", MessageEventSubscriptionEntity.class);
    }
    String processDefinitionId = messageEventSubscription.getConfiguration();
    if (processDefinitionId == null) {
        throw new ActivitiException("Cannot start process instance by message: subscription to message with name '" + messageName + "' is not a message start event.");
    }
    DeploymentManager deploymentCache = commandContext.getProcessEngineConfiguration().getDeploymentManager();
    ProcessDefinition processDefinition = deploymentCache.findDeployedProcessDefinitionById(processDefinitionId);
    if (processDefinition == null) {
        throw new ActivitiObjectNotFoundException("No process definition found for id '" + processDefinitionId + "'", ProcessDefinition.class);
    }
    ProcessInstanceHelper processInstanceHelper = commandContext.getProcessEngineConfiguration().getProcessInstanceHelper();
    ProcessInstance processInstance = processInstanceHelper.createAndStartProcessInstanceByMessage(processDefinition, businessKey, messageName, processVariables, transientVariables);
    return processInstance;
}
Also used : ProcessInstanceHelper(org.activiti.engine.impl.util.ProcessInstanceHelper) ActivitiException(org.activiti.engine.ActivitiException) DeploymentManager(org.activiti.engine.impl.persistence.deploy.DeploymentManager) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) MessageEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntity)

Example 17 with MessageEventSubscriptionEntity

use of org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntity in project Activiti by Activiti.

the class ProcessInstanceHelper method startProcessInstance.

public void startProcessInstance(ExecutionEntity processInstance, CommandContext commandContext, Map<String, Object> variables, FlowElement initialFlowElement, Map<String, Object> transientVariables) {
    Process process = ProcessDefinitionUtil.getProcess(processInstance.getProcessDefinitionId());
    createProcessVariables(processInstance, variables, transientVariables, process);
    recordStartProcessInstance(commandContext, initialFlowElement, processInstance);
    // Event sub process handling
    List<MessageEventSubscriptionEntity> messageEventSubscriptions = new LinkedList<>();
    for (FlowElement flowElement : process.getFlowElements()) {
        if (flowElement instanceof EventSubProcess) {
            EventSubProcess eventSubProcess = (EventSubProcess) flowElement;
            for (FlowElement subElement : eventSubProcess.getFlowElements()) {
                if (subElement instanceof StartEvent) {
                    StartEvent startEvent = (StartEvent) subElement;
                    if (CollectionUtil.isNotEmpty(startEvent.getEventDefinitions())) {
                        EventDefinition eventDefinition = startEvent.getEventDefinitions().get(0);
                        if (eventDefinition instanceof MessageEventDefinition) {
                            MessageEventDefinition messageEventDefinition = (MessageEventDefinition) eventDefinition;
                            BpmnModel bpmnModel = ProcessDefinitionUtil.getBpmnModel(processInstance.getProcessDefinitionId());
                            if (bpmnModel.containsMessageId(messageEventDefinition.getMessageRef())) {
                                messageEventDefinition.setMessageRef(bpmnModel.getMessage(messageEventDefinition.getMessageRef()).getName());
                            }
                            ExecutionEntity messageExecution = commandContext.getExecutionEntityManager().createChildExecution(processInstance);
                            messageExecution.setCurrentFlowElement(startEvent);
                            messageExecution.setEventScope(true);
                            String messageName = getMessageName(commandContext, messageEventDefinition, messageExecution);
                            MessageEventSubscriptionEntity subscription = commandContext.getEventSubscriptionEntityManager().insertMessageEvent(messageName, messageExecution);
                            Optional<String> correlationKey = getCorrelationKey(commandContext, messageEventDefinition, messageExecution);
                            correlationKey.ifPresent(subscription::setConfiguration);
                            messageEventSubscriptions.add(subscription);
                        }
                    }
                }
            }
        }
    }
    // There will always be one child execution created
    ExecutionEntity execution = processInstance.getExecutions().get(0);
    execution.setAppVersion(processInstance.getAppVersion());
    commandContext.getAgenda().planContinueProcessOperation(execution);
    if (Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
        ActivitiEventDispatcher eventDispatcher = Context.getProcessEngineConfiguration().getEventDispatcher();
        eventDispatcher.dispatchEvent(ActivitiEventBuilder.createProcessStartedEvent(execution, variables, false));
        for (MessageEventSubscriptionEntity messageEventSubscription : messageEventSubscriptions) {
            commandContext.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createMessageWaitingEvent(messageEventSubscription.getExecution(), messageEventSubscription.getEventName(), messageEventSubscription.getConfiguration()));
        }
    }
}
Also used : Process(org.activiti.bpmn.model.Process) EventSubProcess(org.activiti.bpmn.model.EventSubProcess) EventDefinition(org.activiti.bpmn.model.EventDefinition) MessageEventDefinition(org.activiti.bpmn.model.MessageEventDefinition) LinkedList(java.util.LinkedList) BpmnModel(org.activiti.bpmn.model.BpmnModel) ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) FlowElement(org.activiti.bpmn.model.FlowElement) StartEvent(org.activiti.bpmn.model.StartEvent) MessageEventDefinition(org.activiti.bpmn.model.MessageEventDefinition) EventSubProcess(org.activiti.bpmn.model.EventSubProcess) MessageEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntity) ActivitiEventDispatcher(org.activiti.engine.delegate.event.ActivitiEventDispatcher)

Example 18 with MessageEventSubscriptionEntity

use of org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntity in project Activiti by Activiti.

the class EventSubscriptionManager method insertMessageEvent.

protected void insertMessageEvent(MessageEventDefinition messageEventDefinition, StartEvent startEvent, ProcessDefinitionEntity processDefinition, BpmnModel bpmnModel) {
    CommandContext commandContext = Context.getCommandContext();
    if (bpmnModel.containsMessageId(messageEventDefinition.getMessageRef())) {
        Message message = bpmnModel.getMessage(messageEventDefinition.getMessageRef());
        messageEventDefinition.setMessageRef(message.getName());
    }
    // look for subscriptions for the same name in db:
    List<EventSubscriptionEntity> subscriptionsForSameMessageName = commandContext.getEventSubscriptionEntityManager().findEventSubscriptionsByName(MessageEventHandler.EVENT_HANDLER_TYPE, messageEventDefinition.getMessageRef(), processDefinition.getTenantId());
    for (EventSubscriptionEntity eventSubscriptionEntity : subscriptionsForSameMessageName) {
        // throw exception only if there's already a subscription as start event
        if (eventSubscriptionEntity.getProcessInstanceId() == null || eventSubscriptionEntity.getProcessInstanceId().isEmpty()) {
            // the event subscription has no instance-id, so it's a message start event
            throw new ActivitiException("Cannot deploy process definition '" + processDefinition.getResourceName() + "': there already is a message event subscription for the message with name '" + messageEventDefinition.getMessageRef() + "'.");
        }
    }
    MessageEventSubscriptionEntity newSubscription = commandContext.getEventSubscriptionEntityManager().createMessageEventSubscription();
    newSubscription.setEventName(messageEventDefinition.getMessageRef());
    newSubscription.setActivityId(startEvent.getId());
    newSubscription.setConfiguration(processDefinition.getId());
    newSubscription.setProcessDefinitionId(processDefinition.getId());
    if (processDefinition.getTenantId() != null) {
        newSubscription.setTenantId(processDefinition.getTenantId());
    }
    commandContext.getEventSubscriptionEntityManager().insert(newSubscription);
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) Message(org.activiti.bpmn.model.Message) MessageEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntity) MessageEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntity) EventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity) SignalEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.SignalEventSubscriptionEntity)

Aggregations

MessageEventSubscriptionEntity (org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntity)18 EventSubscriptionEntity (org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity)11 CommandContext (org.activiti.engine.impl.interceptor.CommandContext)9 SignalEventSubscriptionEntity (org.activiti.engine.impl.persistence.entity.SignalEventSubscriptionEntity)7 ExecutionEntity (org.activiti.engine.impl.persistence.entity.ExecutionEntity)6 EventSubscriptionEntityManager (org.activiti.engine.impl.persistence.entity.EventSubscriptionEntityManager)4 ActivitiException (org.activiti.engine.ActivitiException)3 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)3 StartEvent (org.activiti.bpmn.model.StartEvent)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 MessageSubscriptionImpl (org.activiti.api.runtime.model.impl.MessageSubscriptionImpl)1 BoundaryEvent (org.activiti.bpmn.model.BoundaryEvent)1 BpmnModel (org.activiti.bpmn.model.BpmnModel)1 EventDefinition (org.activiti.bpmn.model.EventDefinition)1 EventSubProcess (org.activiti.bpmn.model.EventSubProcess)1 FlowElement (org.activiti.bpmn.model.FlowElement)1