Search in sources :

Example 1 with ActivityStateManager

use of org.broadleafcommerce.core.workflow.state.ActivityStateManager in project BroadleafCommerce by BroadleafCommerce.

the class SequenceProcessor method doActivities.

@Override
public <P extends ProcessContext<U>> P doActivities(T seedData) throws WorkflowException {
    if (LOG.isDebugEnabled()) {
        LOG.debug(getBeanName() + " processor is running..");
    }
    ActivityStateManager activityStateManager = getBeanFactory().getBean(ActivityStateManager.class, "blActivityStateManager");
    if (activityStateManager == null) {
        throw new IllegalStateException("Unable to find an instance of ActivityStateManager registered under bean id blActivityStateManager");
    }
    ProcessContext<U> context = null;
    RollbackStateLocal rollbackStateLocal = new RollbackStateLocal();
    rollbackStateLocal.setThreadId(String.valueOf(Thread.currentThread().getId()));
    rollbackStateLocal.setWorkflowId(getBeanName());
    RollbackStateLocal.setRollbackStateLocal(rollbackStateLocal);
    try {
        // retrieve injected by Spring
        List<Activity<ProcessContext<U>>> activities = getActivities();
        // retrieve a new instance of the Workflow ProcessContext
        context = createContext(seedData);
        for (Activity<ProcessContext<U>> activity : activities) {
            if (activity.shouldExecute(context)) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("running activity:" + activity.getBeanName() + " using arguments:" + context);
                }
                try {
                    context = activity.execute(context);
                } catch (Throwable activityException) {
                    RollbackFailureException rollbackFailure = null;
                    if (getAutoRollbackOnError()) {
                        LOG.info(String.format("Exception ocurred in %s, executing rollback handlers", rollbackStateLocal.getWorkflowId()));
                        try {
                            ActivityStateManagerImpl.getStateManager().rollbackAllState();
                        } catch (Throwable rollbackException) {
                            LOG.fatal(String.format("There was an exception rolling back %s", rollbackStateLocal.getWorkflowId()), rollbackException);
                            if (rollbackException instanceof RollbackFailureException) {
                                rollbackFailure = (RollbackFailureException) rollbackException;
                            } else {
                                rollbackFailure = new RollbackFailureException(rollbackException);
                            }
                            LOG.error(String.format("The original cause of the rollback for %s was", rollbackStateLocal.getWorkflowId()), activityException);
                            rollbackFailure.setOriginalWorkflowException(activityException);
                            throw rollbackFailure;
                        }
                    }
                    ErrorHandler errorHandler = activity.getErrorHandler();
                    if (errorHandler == null) {
                        getDefaultErrorHandler().handleError(context, activityException);
                        break;
                    } else {
                        errorHandler.handleError(context, activityException);
                    }
                }
                // ensure its ok to continue the process
                if (processShouldStop(context, activity)) {
                    break;
                }
                // register the RollbackHandler
                if (activity.getRollbackHandler() != null && activity.getAutomaticallyRegisterRollbackHandler()) {
                    ActivityStateManagerImpl.getStateManager().registerState(activity, context, activity.getRollbackRegion(), activity.getRollbackHandler(), activity.getStateConfiguration());
                }
            } else {
                LOG.debug("Not executing activity: " + activity.getBeanName() + " based on the context: " + context);
            }
        }
    } finally {
        rollbackStateLocal = RollbackStateLocal.getRollbackStateLocal();
        if (rollbackStateLocal != null && rollbackStateLocal.getWorkflowId().equals(getBeanName())) {
            activityStateManager.clearAllState();
        }
    }
    LOG.debug(getBeanName() + " processor is done.");
    return (P) context;
}
Also used : RollbackFailureException(org.broadleafcommerce.core.workflow.state.RollbackFailureException) ActivityStateManager(org.broadleafcommerce.core.workflow.state.ActivityStateManager) RollbackStateLocal(org.broadleafcommerce.core.workflow.state.RollbackStateLocal)

Aggregations

ActivityStateManager (org.broadleafcommerce.core.workflow.state.ActivityStateManager)1 RollbackFailureException (org.broadleafcommerce.core.workflow.state.RollbackFailureException)1 RollbackStateLocal (org.broadleafcommerce.core.workflow.state.RollbackStateLocal)1