Search in sources :

Example 1 with ExceptionScopeInstance

use of org.jbpm.process.instance.context.exception.ExceptionScopeInstance in project jbpm by kiegroup.

the class WorkItemNodeInstance method internalTrigger.

public void internalTrigger(final NodeInstance from, String type) {
    super.internalTrigger(from, type);
    // if node instance was cancelled, abort
    if (getNodeInstanceContainer().getNodeInstance(getId()) == null) {
        return;
    }
    // TODO this should be included for ruleflow only, not for BPEL
    // if (!Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
    // throw new IllegalArgumentException(
    // "A WorkItemNode only accepts default incoming connections!");
    // }
    WorkItemNode workItemNode = getWorkItemNode();
    createWorkItem(workItemNode);
    if (workItemNode.isWaitForCompletion()) {
        addWorkItemListener();
    }
    String deploymentId = (String) getProcessInstance().getKnowledgeRuntime().getEnvironment().get(EnvironmentName.DEPLOYMENT_ID);
    ((WorkItem) workItem).setDeploymentId(deploymentId);
    ((WorkItem) workItem).setNodeInstanceId(this.getId());
    ((WorkItem) workItem).setNodeId(getNodeId());
    if (isInversionOfControl()) {
        ((ProcessInstance) getProcessInstance()).getKnowledgeRuntime().update(((ProcessInstance) getProcessInstance()).getKnowledgeRuntime().getFactHandle(this), this);
    } else {
        try {
            ((WorkItemManager) ((ProcessInstance) getProcessInstance()).getKnowledgeRuntime().getWorkItemManager()).internalExecuteWorkItem((org.drools.core.process.instance.WorkItem) workItem);
        } catch (WorkItemHandlerNotFoundException wihnfe) {
            getProcessInstance().setState(ProcessInstance.STATE_ABORTED);
            throw wihnfe;
        } catch (Exception e) {
            String exceptionName = e.getClass().getName();
            ExceptionScopeInstance exceptionScopeInstance = (ExceptionScopeInstance) resolveContextInstance(ExceptionScope.EXCEPTION_SCOPE, exceptionName);
            if (exceptionScopeInstance == null) {
                throw new WorkflowRuntimeException(this, getProcessInstance(), "Unable to execute Action: " + e.getMessage(), e);
            }
            // workItemId must be set otherwise cancel activity will not find the right work item
            this.workItemId = workItem.getId();
            exceptionScopeInstance.handleException(exceptionName, e);
        }
    }
    if (!workItemNode.isWaitForCompletion()) {
        triggerCompleted();
    }
    this.workItemId = workItem.getId();
}
Also used : WorkItemNode(org.jbpm.workflow.core.node.WorkItemNode) WorkItemHandlerNotFoundException(org.drools.core.WorkItemHandlerNotFoundException) ProcessInstance(org.jbpm.process.instance.ProcessInstance) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) WorkItem(org.drools.core.process.instance.WorkItem) WorkItemManager(org.drools.core.process.instance.WorkItemManager) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) WorkItemHandlerNotFoundException(org.drools.core.WorkItemHandlerNotFoundException) ExceptionScopeInstance(org.jbpm.process.instance.context.exception.ExceptionScopeInstance)

Example 2 with ExceptionScopeInstance

use of org.jbpm.process.instance.context.exception.ExceptionScopeInstance in project jbpm by kiegroup.

the class FaultNodeInstance method internalTrigger.

public void internalTrigger(final NodeInstance from, String type) {
    if (!org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
        throw new IllegalArgumentException("A FaultNode only accepts default incoming connections!");
    }
    String faultName = getFaultName();
    ExceptionScopeInstance exceptionScopeInstance = getExceptionScopeInstance(faultName);
    NodeInstanceContainer nodeInstanceContainer = (NodeInstanceContainer) getNodeInstanceContainer();
    nodeInstanceContainer.removeNodeInstance(this);
    boolean exceptionHandled = false;
    if (getFaultNode().isTerminateParent()) {
        // handle exception before canceling nodes to allow boundary event to catch the events
        if (exceptionScopeInstance != null) {
            exceptionHandled = true;
            handleException(faultName, exceptionScopeInstance);
        }
        if (nodeInstanceContainer instanceof CompositeNodeInstance) {
            ((CompositeNodeInstance) nodeInstanceContainer).cancel();
        } else if (nodeInstanceContainer instanceof WorkflowProcessInstance) {
            Collection<NodeInstance> nodeInstances = ((WorkflowProcessInstance) nodeInstanceContainer).getNodeInstances();
            for (NodeInstance nodeInstance : nodeInstances) {
                ((org.jbpm.workflow.instance.NodeInstance) nodeInstance).cancel();
            }
        }
    }
    if (exceptionScopeInstance != null) {
        if (!exceptionHandled) {
            handleException(faultName, exceptionScopeInstance);
        }
    } else {
        ((ProcessInstance) getProcessInstance()).setState(ProcessInstance.STATE_ABORTED, faultName, getFaultData());
    }
}
Also used : NodeInstanceContainer(org.jbpm.workflow.instance.NodeInstanceContainer) Collection(java.util.Collection) ProcessInstance(org.jbpm.process.instance.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) ExceptionScopeInstance(org.jbpm.process.instance.context.exception.ExceptionScopeInstance)

Example 3 with ExceptionScopeInstance

use of org.jbpm.process.instance.context.exception.ExceptionScopeInstance in project jbpm by kiegroup.

the class NodeInstanceImpl method executeAction.

/**
 * This method is used in both instances of the {@link ExtendedNodeInstanceImpl}
 * and {@link ActionNodeInstance} instances in order to handle
 * exceptions thrown when executing actions.
 *
 * @param action An {@link Action} instance.
 */
protected void executeAction(Action action) {
    ProcessContext context = new ProcessContext(getProcessInstance().getKnowledgeRuntime());
    context.setNodeInstance(this);
    try {
        action.execute(context);
    } catch (Exception e) {
        String exceptionName = e.getClass().getName();
        ExceptionScopeInstance exceptionScopeInstance = (ExceptionScopeInstance) resolveContextInstance(ExceptionScope.EXCEPTION_SCOPE, exceptionName);
        if (exceptionScopeInstance == null) {
            throw new WorkflowRuntimeException(this, getProcessInstance(), "Unable to execute Action: " + e.getMessage(), e);
        }
        exceptionScopeInstance.handleException(exceptionName, e);
    }
}
Also used : WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) ProcessContext(org.drools.core.spi.ProcessContext) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) ExceptionScopeInstance(org.jbpm.process.instance.context.exception.ExceptionScopeInstance)

Example 4 with ExceptionScopeInstance

use of org.jbpm.process.instance.context.exception.ExceptionScopeInstance in project jbpm by kiegroup.

the class SubProcessNodeInstance method processInstanceCompleted.

public void processInstanceCompleted(ProcessInstance processInstance) {
    removeEventListeners();
    handleOutMappings(processInstance);
    if (processInstance.getState() == ProcessInstance.STATE_ABORTED) {
        String faultName = processInstance.getOutcome() == null ? "" : processInstance.getOutcome();
        // handle exception as sub process failed with error code
        ExceptionScopeInstance exceptionScopeInstance = (ExceptionScopeInstance) resolveContextInstance(ExceptionScope.EXCEPTION_SCOPE, faultName);
        if (exceptionScopeInstance != null) {
            exceptionScopeInstance.handleException(faultName, processInstance.getFaultData());
            if (getSubProcessNode() != null && !getSubProcessNode().isIndependent() && getSubProcessNode().isAbortParent()) {
                cancel();
            }
            return;
        } else if (getSubProcessNode() != null && !getSubProcessNode().isIndependent() && getSubProcessNode().isAbortParent()) {
            ((ProcessInstance) getProcessInstance()).setState(ProcessInstance.STATE_ABORTED, faultName);
            return;
        }
    }
    // handle dynamic subprocess
    if (getNode() == null) {
        setMetaData("NodeType", "SubProcessNode");
    }
    // if there were no exception proceed normally
    triggerCompleted();
}
Also used : ExceptionScopeInstance(org.jbpm.process.instance.context.exception.ExceptionScopeInstance)

Example 5 with ExceptionScopeInstance

use of org.jbpm.process.instance.context.exception.ExceptionScopeInstance in project jbpm by kiegroup.

the class AsyncWorkItemHandlerCmdCallback method onCommandError.

@Override
public void onCommandError(CommandContext ctx, final Throwable exception) {
    final Long processInstanceId = (Long) ctx.getData("processInstanceId");
    final WorkItem workItem = (WorkItem) ctx.getData("workItem");
    // find the right runtime to do the complete
    RuntimeManager manager = getRuntimeManager(ctx);
    RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get(processInstanceId));
    final ExecutionErrorHandler errorHandler = getExecutionErrorHandler(manager);
    try {
        boolean isErrorHandled = engine.getKieSession().execute(new ExecutableCommand<Boolean>() {

            private static final long serialVersionUID = 1L;

            @Override
            public Boolean execute(Context context) {
                KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
                WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstanceId);
                NodeInstance nodeInstance = getNodeInstance(workItem, processInstance);
                Throwable actualException = exception;
                if (actualException instanceof AsyncJobException) {
                    actualException = exception.getCause();
                }
                String exceptionName = actualException.getClass().getName();
                ExceptionScopeInstance exceptionScopeInstance = (ExceptionScopeInstance) ((org.jbpm.workflow.instance.NodeInstance) nodeInstance).resolveContextInstance(ExceptionScope.EXCEPTION_SCOPE, exceptionName);
                if (exceptionScopeInstance != null) {
                    logger.debug("Handling job error '{}' via process error handling", actualException.getMessage());
                    exceptionScopeInstance.handleException(exceptionName, actualException);
                    return true;
                } else {
                    logger.debug("No process level error handling for '{}' letting it to be handled by execution errors", exception.getMessage());
                    errorHandler.processing(nodeInstance);
                    return false;
                }
            }
        });
        if (!isErrorHandled) {
            logger.debug("Error '{}' was not handled on process level, handling it via execution errors mechanism", exception.getMessage());
            errorHandler.handle(exception);
        }
    } catch (Exception e) {
        logger.error("Error when handling callback from executor", e);
    } finally {
        manager.disposeRuntimeEngine(engine);
        closeErrorHandler(manager);
    }
}
Also used : ProcessInstanceIdContext(org.kie.internal.runtime.manager.context.ProcessInstanceIdContext) CommandContext(org.kie.api.executor.CommandContext) RegistryContext(org.drools.core.command.impl.RegistryContext) Context(org.kie.api.runtime.Context) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) AbstractRuntimeManager(org.jbpm.runtime.manager.impl.AbstractRuntimeManager) WorkItem(org.kie.api.runtime.process.WorkItem) AsyncJobException(org.jbpm.executor.AsyncJobException) ExecutionErrorHandler(org.kie.internal.runtime.error.ExecutionErrorHandler) NoOpExecutionErrorHandler(org.jbpm.process.instance.impl.NoOpExecutionErrorHandler) AsyncJobException(org.jbpm.executor.AsyncJobException) KieSession(org.kie.api.runtime.KieSession) NodeInstance(org.kie.api.runtime.process.NodeInstance) WorkItemNodeInstance(org.jbpm.workflow.instance.node.WorkItemNodeInstance) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance) ExceptionScopeInstance(org.jbpm.process.instance.context.exception.ExceptionScopeInstance)

Aggregations

ExceptionScopeInstance (org.jbpm.process.instance.context.exception.ExceptionScopeInstance)5 ProcessInstance (org.jbpm.process.instance.ProcessInstance)2 WorkflowRuntimeException (org.jbpm.workflow.instance.WorkflowRuntimeException)2 NodeInstance (org.kie.api.runtime.process.NodeInstance)2 Collection (java.util.Collection)1 WorkItemHandlerNotFoundException (org.drools.core.WorkItemHandlerNotFoundException)1 RegistryContext (org.drools.core.command.impl.RegistryContext)1 WorkItem (org.drools.core.process.instance.WorkItem)1 WorkItemManager (org.drools.core.process.instance.WorkItemManager)1 ProcessContext (org.drools.core.spi.ProcessContext)1 AsyncJobException (org.jbpm.executor.AsyncJobException)1 NoOpExecutionErrorHandler (org.jbpm.process.instance.impl.NoOpExecutionErrorHandler)1 AbstractRuntimeManager (org.jbpm.runtime.manager.impl.AbstractRuntimeManager)1 WorkItemNode (org.jbpm.workflow.core.node.WorkItemNode)1 NodeInstanceContainer (org.jbpm.workflow.instance.NodeInstanceContainer)1 WorkflowProcessInstance (org.jbpm.workflow.instance.WorkflowProcessInstance)1 WorkItemNodeInstance (org.jbpm.workflow.instance.node.WorkItemNodeInstance)1 CommandContext (org.kie.api.executor.CommandContext)1 Context (org.kie.api.runtime.Context)1 KieSession (org.kie.api.runtime.KieSession)1