Search in sources :

Example 1 with AsyncJobException

use of org.jbpm.executor.AsyncJobException 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)

Example 2 with AsyncJobException

use of org.jbpm.executor.AsyncJobException in project jbpm by kiegroup.

the class AbstractAvailableJobsExecutor method handleException.

@SuppressWarnings("unchecked")
protected boolean handleException(RequestInfo request, Throwable e, CommandContext ctx, List<CommandCallback> callbacks) {
    logger.warn("Error during command {} error message {}", request.getCommandName(), e.getMessage(), e);
    ErrorInfo errorInfo = new ErrorInfo(e.getMessage(), ExceptionUtils.getStackTrace(e.fillInStackTrace()));
    errorInfo.setRequestInfo(request);
    ((List<ErrorInfo>) request.getErrorInfo()).add(errorInfo);
    logger.debug("Error Number: {}", request.getErrorInfo().size());
    if (request.getRetries() > 0) {
        request.setStatus(STATUS.RETRYING);
        request.setRetries(request.getRetries() - 1);
        // calculate next retry time
        List<Long> retryDelay = (List<Long>) ctx.getData("retryDelay");
        if (retryDelay != null) {
            long retryAdd = 0l;
            try {
                // need to decrement it as executions are directly incremented upon execution
                retryAdd = retryDelay.get(request.getExecutions() - 1);
            } catch (IndexOutOfBoundsException ex) {
                // in case there is no element matching given execution, use last one
                retryAdd = retryDelay.get(retryDelay.size() - 1);
            }
            request.setTime(new Date(System.currentTimeMillis() + retryAdd));
            logger.info("Retrying request ( with id {}) - delay configured, next retry at {}", request.getId(), request.getTime());
        }
        logger.debug("Retrying ({}) still available!", request.getRetries());
        executorStoreService.updateRequest(request, ((ExecutorImpl) executor).scheduleExecution(request, request.getTime()));
        return false;
    } else {
        logger.debug("Error no retries left!");
        request.setStatus(STATUS.ERROR);
        executorStoreService.updateRequest(request, null);
        AsyncJobException wrappedException = new AsyncJobException(request.getId(), request.getCommandName(), e);
        if (callbacks != null) {
            for (CommandCallback handler : callbacks) {
                handler.onCommandError(ctx, wrappedException);
            }
        }
        return true;
    }
}
Also used : ErrorInfo(org.jbpm.executor.entities.ErrorInfo) AsyncJobException(org.jbpm.executor.AsyncJobException) List(java.util.List) CommandCallback(org.kie.api.executor.CommandCallback) Date(java.util.Date)

Example 3 with AsyncJobException

use of org.jbpm.executor.AsyncJobException in project jbpm by kiegroup.

the class JobExecutionErrorFilter method filter.

@Override
public ExecutionError filter(ExecutionErrorContext errorContext) {
    AsyncJobException exception = extract(errorContext.getCause(), AsyncJobException.class);
    String stacktrace = getStackTrace(exception);
    NodeInstance nodeInstance = errorContext.getLastExecutedNode();
    return ExecutionError.builder().type(TYPE).initActivityId(getInitActivityId(errorContext)).deploymentId(((ProcessInstanceImpl) nodeInstance.getProcessInstance()).getDeploymentId()).processInstanceId(nodeInstance.getProcessInstance().getId()).processId(nodeInstance.getProcessInstance().getProcessId()).activityId(nodeInstance.getId()).activityName(nodeName(nodeInstance)).jobId(exception.getJobId()).message(exception.getMessage()).error(stacktrace).errorDate(new Date()).build();
}
Also used : ProcessInstanceImpl(org.jbpm.process.instance.impl.ProcessInstanceImpl) AsyncJobException(org.jbpm.executor.AsyncJobException) NodeInstance(org.kie.api.runtime.process.NodeInstance) Date(java.util.Date)

Aggregations

AsyncJobException (org.jbpm.executor.AsyncJobException)3 Date (java.util.Date)2 NodeInstance (org.kie.api.runtime.process.NodeInstance)2 List (java.util.List)1 RegistryContext (org.drools.core.command.impl.RegistryContext)1 ErrorInfo (org.jbpm.executor.entities.ErrorInfo)1 ExceptionScopeInstance (org.jbpm.process.instance.context.exception.ExceptionScopeInstance)1 NoOpExecutionErrorHandler (org.jbpm.process.instance.impl.NoOpExecutionErrorHandler)1 ProcessInstanceImpl (org.jbpm.process.instance.impl.ProcessInstanceImpl)1 AbstractRuntimeManager (org.jbpm.runtime.manager.impl.AbstractRuntimeManager)1 WorkflowProcessInstance (org.jbpm.workflow.instance.WorkflowProcessInstance)1 WorkItemNodeInstance (org.jbpm.workflow.instance.node.WorkItemNodeInstance)1 CommandCallback (org.kie.api.executor.CommandCallback)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 WorkItem (org.kie.api.runtime.process.WorkItem)1 ExecutionErrorHandler (org.kie.internal.runtime.error.ExecutionErrorHandler)1