Search in sources :

Example 1 with CompensateEventSubscriptionEntity

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

the class ScopeUtil method createEventScopeExecution.

/**
   * creates an event scope for the given execution:
   * 
   * create a new event scope execution under the parent of the given 
   * execution and move all event subscriptions to that execution.
   * 
   * this allows us to "remember" the event subscriptions after finishing a 
   * scope
   */
public static void createEventScopeExecution(ExecutionEntity execution) {
    ExecutionEntity eventScope = ScopeUtil.findScopeExecutionForScope(execution, execution.getActivity().getParent());
    List<CompensateEventSubscriptionEntity> eventSubscriptions = execution.getCompensateEventSubscriptions();
    if (!eventSubscriptions.isEmpty()) {
        ExecutionEntity eventScopeExecution = eventScope.createExecution();
        eventScopeExecution.setActive(false);
        eventScopeExecution.setConcurrent(false);
        eventScopeExecution.setEventScope(true);
        eventScopeExecution.setActivity((ActivityImpl) execution.getActivity());
        execution.setConcurrent(false);
        // copy local variables to eventScopeExecution by value. This way, 
        // the eventScopeExecution references a 'snapshot' of the local variables      
        Map<String, Object> variables = execution.getVariablesLocal();
        for (Entry<String, Object> variable : variables.entrySet()) {
            eventScopeExecution.setVariableLocal(variable.getKey(), variable.getValue());
        }
        // set event subscriptions to the event scope execution:
        for (CompensateEventSubscriptionEntity eventSubscriptionEntity : eventSubscriptions) {
            eventSubscriptionEntity = eventSubscriptionEntity.moveUnder(eventScopeExecution);
        }
        CompensateEventSubscriptionEntity eventSubscription = CompensateEventSubscriptionEntity.createAndInsert(eventScope);
        eventSubscription.setActivity(execution.getActivity());
        eventSubscription.setConfiguration(eventScopeExecution.getId());
    }
}
Also used : ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) CompensateEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.CompensateEventSubscriptionEntity)

Example 2 with CompensateEventSubscriptionEntity

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

the class CompensationEventHandler method handleEvent.

public void handleEvent(EventSubscriptionEntity eventSubscription, Object payload, CommandContext commandContext) {
    String configuration = eventSubscription.getConfiguration();
    if (configuration == null) {
        throw new ActivitiException("Compensating execution not set for compensate event subscription with id " + eventSubscription.getId());
    }
    ExecutionEntity compensatingExecution = commandContext.getExecutionEntityManager().findExecutionById(configuration);
    ActivityImpl compensationHandler = eventSubscription.getActivity();
    if ((compensationHandler.getProperty(BpmnParse.PROPERTYNAME_IS_FOR_COMPENSATION) == null || !(Boolean) compensationHandler.getProperty(BpmnParse.PROPERTYNAME_IS_FOR_COMPENSATION)) && compensationHandler.isScope()) {
        // descend into scope:
        List<CompensateEventSubscriptionEntity> eventsForThisScope = compensatingExecution.getCompensateEventSubscriptions();
        ScopeUtil.throwCompensationEvent(eventsForThisScope, compensatingExecution, false);
    } else {
        try {
            if (commandContext.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
                commandContext.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createActivityEvent(ActivitiEventType.ACTIVITY_COMPENSATE, compensationHandler.getId(), (String) compensationHandler.getProperty("name"), compensatingExecution.getId(), compensatingExecution.getProcessInstanceId(), compensatingExecution.getProcessDefinitionId(), (String) compensatingExecution.getActivity().getProperties().get("type"), compensatingExecution.getActivity().getActivityBehavior().getClass().getCanonicalName()));
            }
            compensatingExecution.setActivity(compensationHandler);
            // executing the atomic operation makes sure activity start events are fired
            compensatingExecution.performOperation(AtomicOperation.ACTIVITY_START);
        } catch (Exception e) {
            throw new ActivitiException("Error while handling compensation event " + eventSubscription, e);
        }
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) ActivityImpl(org.activiti.engine.impl.pvm.process.ActivityImpl) CompensateEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.CompensateEventSubscriptionEntity) ActivitiException(org.activiti.engine.ActivitiException)

Example 3 with CompensateEventSubscriptionEntity

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

the class IntermediateThrowCompensationEventActivityBehavior method execute.

@Override
public void execute(ActivityExecution execution) throws Exception {
    final String activityRef = compensateEventDefinition.getActivityRef();
    ExecutionEntity scopeExecution = ScopeUtil.findScopeExecutionForScope((ExecutionEntity) execution, (ActivityImpl) execution.getActivity());
    List<CompensateEventSubscriptionEntity> eventSubscriptions;
    if (activityRef != null) {
        eventSubscriptions = scopeExecution.getCompensateEventSubscriptions(activityRef);
    } else {
        eventSubscriptions = scopeExecution.getCompensateEventSubscriptions();
    }
    if (eventSubscriptions.isEmpty()) {
        leave(execution);
    } else {
        // TODO: implement async (waitForCompletion=false in bpmn)
        ScopeUtil.throwCompensationEvent(eventSubscriptions, execution, false);
    }
}
Also used : ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) CompensateEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.CompensateEventSubscriptionEntity)

Example 4 with CompensateEventSubscriptionEntity

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

the class AbstractBpmnActivityBehavior method createCompensateEventSubscription.

protected void createCompensateEventSubscription(ActivityExecution execution) {
    String compensationHandlerId = (String) execution.getActivity().getProperty(BpmnParse.PROPERTYNAME_COMPENSATION_HANDLER_ID);
    ExecutionEntity executionEntity = (ExecutionEntity) execution;
    ActivityImpl compensationHandlder = executionEntity.getProcessDefinition().findActivity(compensationHandlerId);
    PvmScope scopeActivitiy = compensationHandlder.getParent();
    ExecutionEntity scopeExecution = ScopeUtil.findScopeExecutionForScope(executionEntity, scopeActivitiy);
    CompensateEventSubscriptionEntity compensateEventSubscriptionEntity = CompensateEventSubscriptionEntity.createAndInsert(scopeExecution);
    compensateEventSubscriptionEntity.setActivity(compensationHandlder);
}
Also used : PvmScope(org.activiti.engine.impl.pvm.PvmScope) ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) ActivityImpl(org.activiti.engine.impl.pvm.process.ActivityImpl) CompensateEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.CompensateEventSubscriptionEntity)

Example 5 with CompensateEventSubscriptionEntity

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

the class ScopeUtil method throwCompensationEvent.

/**
   * we create a separate execution for each compensation handler invocation. 
   */
public static void throwCompensationEvent(List<CompensateEventSubscriptionEntity> eventSubscriptions, ActivityExecution execution, boolean async) {
    // first spawn the compensating executions
    for (EventSubscriptionEntity eventSubscription : eventSubscriptions) {
        ExecutionEntity compensatingExecution = null;
        // and holds snapshot data).
        if (eventSubscription.getConfiguration() != null) {
            compensatingExecution = Context.getCommandContext().getExecutionEntityManager().findExecutionById(eventSubscription.getConfiguration());
            // move the compensating execution under this execution:
            compensatingExecution.setParent((InterpretableExecution) execution);
            compensatingExecution.setEventScope(false);
        } else {
            compensatingExecution = (ExecutionEntity) execution.createExecution();
            eventSubscription.setConfiguration(compensatingExecution.getId());
        }
        compensatingExecution.setConcurrent(true);
    }
    // signal compensation events in reverse order of their 'created' timestamp
    Collections.sort(eventSubscriptions, new Comparator<EventSubscriptionEntity>() {

        public int compare(EventSubscriptionEntity o1, EventSubscriptionEntity o2) {
            return o2.getCreated().compareTo(o1.getCreated());
        }
    });
    for (CompensateEventSubscriptionEntity compensateEventSubscriptionEntity : eventSubscriptions) {
        compensateEventSubscriptionEntity.eventReceived(null, async);
    }
}
Also used : ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) CompensateEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.CompensateEventSubscriptionEntity) CompensateEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.CompensateEventSubscriptionEntity) EventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity)

Aggregations

CompensateEventSubscriptionEntity (org.activiti.engine.impl.persistence.entity.CompensateEventSubscriptionEntity)5 ExecutionEntity (org.activiti.engine.impl.persistence.entity.ExecutionEntity)5 ActivityImpl (org.activiti.engine.impl.pvm.process.ActivityImpl)2 ActivitiException (org.activiti.engine.ActivitiException)1 EventSubscriptionEntity (org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity)1 PvmScope (org.activiti.engine.impl.pvm.PvmScope)1