Search in sources :

Example 21 with EventSubscriptionEntity

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

the class ScopeUtil method createCopyOfSubProcessExecutionForCompensation.

/**
 * Creates a new event scope execution and moves existing event subscriptions to this new execution
 */
public static void createCopyOfSubProcessExecutionForCompensation(ExecutionEntity subProcessExecution) {
    EventSubscriptionEntityManager eventSubscriptionEntityManager = Context.getCommandContext().getEventSubscriptionEntityManager();
    List<EventSubscriptionEntity> eventSubscriptions = eventSubscriptionEntityManager.findEventSubscriptionsByExecutionAndType(subProcessExecution.getId(), "compensate");
    List<CompensateEventSubscriptionEntity> compensateEventSubscriptions = new ArrayList<CompensateEventSubscriptionEntity>();
    for (EventSubscriptionEntity event : eventSubscriptions) {
        if (event instanceof CompensateEventSubscriptionEntity) {
            compensateEventSubscriptions.add((CompensateEventSubscriptionEntity) event);
        }
    }
    if (CollectionUtil.isNotEmpty(compensateEventSubscriptions)) {
        ExecutionEntity processInstanceExecutionEntity = subProcessExecution.getProcessInstance();
        ExecutionEntity eventScopeExecution = Context.getCommandContext().getExecutionEntityManager().createChildExecution(processInstanceExecutionEntity);
        eventScopeExecution.setActive(false);
        eventScopeExecution.setEventScope(true);
        eventScopeExecution.setCurrentFlowElement(subProcessExecution.getCurrentFlowElement());
        // copy local variables to eventScopeExecution by value. This way,
        // the eventScopeExecution references a 'snapshot' of the local variables
        new SubProcessVariableSnapshotter().setVariablesSnapshots(subProcessExecution, eventScopeExecution);
        // set event subscriptions to the event scope execution:
        for (CompensateEventSubscriptionEntity eventSubscriptionEntity : compensateEventSubscriptions) {
            eventSubscriptionEntityManager.delete(eventSubscriptionEntity);
            CompensateEventSubscriptionEntity newSubscription = eventSubscriptionEntityManager.insertCompensationEvent(eventScopeExecution, eventSubscriptionEntity.getActivityId());
            newSubscription.setConfiguration(eventSubscriptionEntity.getConfiguration());
            newSubscription.setCreated(eventSubscriptionEntity.getCreated());
        }
        CompensateEventSubscriptionEntity eventSubscription = eventSubscriptionEntityManager.insertCompensationEvent(processInstanceExecutionEntity, eventScopeExecution.getCurrentFlowElement().getId());
        eventSubscription.setConfiguration(eventScopeExecution.getId());
    }
}
Also used : ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) EventSubscriptionEntityManager(org.activiti.engine.impl.persistence.entity.EventSubscriptionEntityManager) ArrayList(java.util.ArrayList) CompensateEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.CompensateEventSubscriptionEntity) CompensateEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.CompensateEventSubscriptionEntity) EventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity)

Example 22 with EventSubscriptionEntity

use of org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity 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)

Example 23 with EventSubscriptionEntity

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

the class EventSubscriptionManager method removeObsoleteEventSubscriptionsImpl.

protected void removeObsoleteEventSubscriptionsImpl(ProcessDefinitionEntity processDefinition, String eventHandlerType) {
    // remove all subscriptions for the previous version
    EventSubscriptionEntityManager eventSubscriptionEntityManager = Context.getCommandContext().getEventSubscriptionEntityManager();
    List<EventSubscriptionEntity> subscriptionsToDelete = eventSubscriptionEntityManager.findEventSubscriptionsByTypeAndProcessDefinitionId(eventHandlerType, processDefinition.getId(), processDefinition.getTenantId());
    for (EventSubscriptionEntity eventSubscriptionEntity : subscriptionsToDelete) {
        eventSubscriptionEntityManager.delete(eventSubscriptionEntity);
    }
}
Also used : EventSubscriptionEntityManager(org.activiti.engine.impl.persistence.entity.EventSubscriptionEntityManager) MessageEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntity) EventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity) SignalEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.SignalEventSubscriptionEntity)

Example 24 with EventSubscriptionEntity

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

the class IntermediateCatchSignalEventActivityBehavior method deleteSignalEventSubscription.

protected ExecutionEntity deleteSignalEventSubscription(DelegateExecution execution) {
    ExecutionEntity executionEntity = (ExecutionEntity) execution;
    String eventName = null;
    if (signal != null) {
        eventName = signal.getName();
    } else {
        eventName = signalEventDefinition.getSignalRef();
    }
    EventSubscriptionEntityManager eventSubscriptionEntityManager = Context.getCommandContext().getEventSubscriptionEntityManager();
    List<EventSubscriptionEntity> eventSubscriptions = executionEntity.getEventSubscriptions();
    for (EventSubscriptionEntity eventSubscription : eventSubscriptions) {
        if (eventSubscription instanceof SignalEventSubscriptionEntity && eventSubscription.getEventName().equals(eventName)) {
            eventSubscriptionEntityManager.delete(eventSubscription);
        }
    }
    return executionEntity;
}
Also used : ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) EventSubscriptionEntityManager(org.activiti.engine.impl.persistence.entity.EventSubscriptionEntityManager) EventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity) SignalEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.SignalEventSubscriptionEntity) SignalEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.SignalEventSubscriptionEntity)

Example 25 with EventSubscriptionEntity

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

the class EventBasedGatewayTest method testCatchSignalAndMessageAndTimer.

@Deployment
public void testCatchSignalAndMessageAndTimer() {
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("catchSignal");
    assertThat(createEventSubscriptionQuery().count()).isEqualTo(2);
    EventSubscriptionQueryImpl messageEventSubscriptionQuery = createEventSubscriptionQuery().eventType("message");
    assertThat(messageEventSubscriptionQuery.count()).isEqualTo(1);
    assertThat(createEventSubscriptionQuery().eventType("signal").count()).isEqualTo(1);
    assertThat(runtimeService.createProcessInstanceQuery().count()).isEqualTo(1);
    assertThat(managementService.createTimerJobQuery().count()).isEqualTo(1);
    Execution execution = runtimeService.createExecutionQuery().messageEventSubscriptionName("newInvoice").singleResult();
    assertThat(execution).isNotNull();
    execution = runtimeService.createExecutionQuery().signalEventSubscriptionName("alert").singleResult();
    assertThat(execution).isNotNull();
    processEngineConfiguration.getClock().setCurrentTime(new Date(processEngineConfiguration.getClock().getCurrentTime().getTime() + 10000));
    EventSubscriptionEntity messageEventSubscription = messageEventSubscriptionQuery.singleResult();
    runtimeService.messageEventReceived(messageEventSubscription.getEventName(), messageEventSubscription.getExecutionId());
    assertThat(createEventSubscriptionQuery().count()).isEqualTo(0);
    assertThat(runtimeService.createProcessInstanceQuery().count()).isEqualTo(1);
    assertThat(managementService.createTimerJobQuery().count()).isEqualTo(0);
    assertThat(managementService.createJobQuery().count()).isEqualTo(0);
    Task task = taskService.createTaskQuery().taskName("afterMessage").singleResult();
    assertThat(task).isNotNull();
    taskService.complete(task.getId());
    assertHistoricActivitiesDeleteReason(processInstance, DeleteReason.EVENT_BASED_GATEWAY_CANCEL, "signalEvent");
    assertHistoricActivitiesDeleteReason(processInstance, DeleteReason.EVENT_BASED_GATEWAY_CANCEL, "timerEvent");
}
Also used : Task(org.activiti.engine.task.Task) Execution(org.activiti.engine.runtime.Execution) EventSubscriptionQueryImpl(org.activiti.engine.impl.EventSubscriptionQueryImpl) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) Date(java.util.Date) EventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity) Deployment(org.activiti.engine.test.Deployment)

Aggregations

EventSubscriptionEntity (org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity)33 MessageEventSubscriptionEntity (org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntity)15 SignalEventSubscriptionEntity (org.activiti.engine.impl.persistence.entity.SignalEventSubscriptionEntity)13 EventSubscriptionEntityManager (org.activiti.engine.impl.persistence.entity.EventSubscriptionEntityManager)11 CommandContext (org.activiti.engine.impl.interceptor.CommandContext)9 ExecutionEntity (org.activiti.engine.impl.persistence.entity.ExecutionEntity)9 Task (org.activiti.engine.task.Task)6 Deployment (org.activiti.engine.test.Deployment)6 EventSubscriptionQueryImpl (org.activiti.engine.impl.EventSubscriptionQueryImpl)5 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)5 CompensateEventSubscriptionEntity (org.activiti.engine.impl.persistence.entity.CompensateEventSubscriptionEntity)4 Execution (org.activiti.engine.runtime.Execution)4 ArrayList (java.util.ArrayList)3 BoundaryEvent (org.activiti.bpmn.model.BoundaryEvent)3 ActivitiException (org.activiti.engine.ActivitiException)3 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)3 ProcessEngineConfigurationImpl (org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl)2 CommandExecutor (org.activiti.engine.impl.interceptor.CommandExecutor)2 ExecutionEntityManager (org.activiti.engine.impl.persistence.entity.ExecutionEntityManager)2 Request (cern.modesti.request.Request)1