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