Search in sources :

Example 1 with ExecutionErrorHandler

use of org.kie.internal.runtime.error.ExecutionErrorHandler 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

RegistryContext (org.drools.core.command.impl.RegistryContext)1 AsyncJobException (org.jbpm.executor.AsyncJobException)1 ExceptionScopeInstance (org.jbpm.process.instance.context.exception.ExceptionScopeInstance)1 NoOpExecutionErrorHandler (org.jbpm.process.instance.impl.NoOpExecutionErrorHandler)1 AbstractRuntimeManager (org.jbpm.runtime.manager.impl.AbstractRuntimeManager)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 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)1 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)1 NodeInstance (org.kie.api.runtime.process.NodeInstance)1 WorkItem (org.kie.api.runtime.process.WorkItem)1 ExecutionErrorHandler (org.kie.internal.runtime.error.ExecutionErrorHandler)1 ProcessInstanceIdContext (org.kie.internal.runtime.manager.context.ProcessInstanceIdContext)1