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;
}
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()));
}
}
}
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);
}
Aggregations