Search in sources :

Example 1 with ErrorInfo

use of org.jbpm.executor.entities.ErrorInfo in project jBPM5-Developer-Guide by Salaboy.

the class BasicExecutorBaseTest method defaultRequestRetryTest.

/**
 * Test showing the retry mechanism for failing commands.
 * @throws InterruptedException
 */
@Test
public void defaultRequestRetryTest() throws InterruptedException {
    CommandContext ctxCMD = new CommandContext();
    ctxCMD.setData("businessKey", UUID.randomUUID().toString());
    // The command we are registering will cause an exception.
    // Remeber that the default number of reties is 3.
    executor.scheduleRequest("ThrowExceptionCmd", ctxCMD);
    Thread.sleep(12000);
    // After 12 seconds we should have 4 errors: 1 corresponding to the
    // first time the command failed. The other 3 correspond to the 3
    // retries.
    List<RequestInfo> inErrorRequests = executor.getInErrorRequests();
    assertEquals(1, inErrorRequests.size());
    List<ErrorInfo> errors = executor.getAllErrors();
    System.out.println(" >>> Errors: " + errors);
    // Three retries means 4 executions in total 1(regular) + 3(retries)
    assertEquals(4, errors.size());
}
Also used : CommandContext(org.jbpm.executor.api.CommandContext) ErrorInfo(org.jbpm.executor.entities.ErrorInfo) RequestInfo(org.jbpm.executor.entities.RequestInfo) Test(org.junit.Test)

Example 2 with ErrorInfo

use of org.jbpm.executor.entities.ErrorInfo 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 ErrorInfo

use of org.jbpm.executor.entities.ErrorInfo in project jBPM5-Developer-Guide by Salaboy.

the class BasicExecutorBaseTest method executorExceptionTest.

/**
 * Test showing the exception handling mechanism of the Executor Service.
 * @throws InterruptedException
 */
@Test
public void executorExceptionTest() throws InterruptedException {
    CommandContext commandContext = new CommandContext();
    commandContext.setData("businessKey", UUID.randomUUID().toString());
    cachedEntities.put((String) commandContext.getData("businessKey"), new AtomicLong(1));
    // Same callback as the precious test
    commandContext.setData("callbacks", "SimpleIncrementCallback");
    // no retries please.
    commandContext.setData("retries", 0);
    // The command we are registering will cause an exception.
    executor.scheduleRequest("ThrowExceptionCmd", commandContext);
    Thread.sleep(10000);
    // After 10 seconds, we should have a failing request.
    List<RequestInfo> inErrorRequests = executor.getInErrorRequests();
    assertEquals(1, inErrorRequests.size());
    System.out.println("Error: " + inErrorRequests.get(0));
    List<ErrorInfo> errors = executor.getAllErrors();
    System.out.println(" >>> Errors: " + errors);
    assertEquals(1, errors.size());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) CommandContext(org.jbpm.executor.api.CommandContext) ErrorInfo(org.jbpm.executor.entities.ErrorInfo) RequestInfo(org.jbpm.executor.entities.RequestInfo) Test(org.junit.Test)

Example 4 with ErrorInfo

use of org.jbpm.executor.entities.ErrorInfo in project jBPM5-Developer-Guide by Salaboy.

the class ExecutorRunnable method run.

@Transactional
public void run() {
    logger.log(Level.INFO, " >>> Executor Thread {0} Waking Up!!!", this.toString());
    List<?> resultList = em.createQuery("Select r from RequestInfo as r where r.status ='QUEUED' or r.status = 'RETRYING' ORDER BY r.time DESC").getResultList();
    logger.log(Level.INFO, " >>> Pending Requests = {0}", resultList.size());
    if (resultList.size() > 0) {
        RequestInfo r = null;
        Throwable exception = null;
        try {
            r = (RequestInfo) resultList.get(0);
            r.setStatus(STATUS.RUNNING);
            em.merge(r);
            logger.log(Level.INFO, " >> Processing Request Id: {0}", r.getId());
            logger.log(Level.INFO, " >> Request Status ={0}", r.getStatus());
            logger.log(Level.INFO, " >> Command Name to execute = {0}", r.getCommandName());
            Command cmd = this.findCommand(r.getCommandName());
            CommandContext ctx = null;
            byte[] reqData = r.getRequestData();
            if (reqData != null) {
                try {
                    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(reqData));
                    ctx = (CommandContext) in.readObject();
                } catch (IOException e) {
                    ctx = null;
                    e.printStackTrace();
                }
            }
            ExecutionResults results = cmd.execute(ctx);
            if (ctx != null && ctx.getData("callbacks") != null) {
                logger.log(Level.INFO, " ### Callback: {0}", ctx.getData("callbacks"));
                String[] callbacksArray = ((String) ctx.getData("callbacks")).split(",");
                ;
                List<String> callbacks = (List<String>) Arrays.asList(callbacksArray);
                for (String callbackName : callbacks) {
                    CommandCallback handler = this.findCommandCallback(callbackName);
                    handler.onCommandDone(ctx, results);
                }
            } else {
                logger.info(" ### Callbacks: NULL");
            }
            if (results != null) {
                try {
                    ByteArrayOutputStream bout = new ByteArrayOutputStream();
                    ObjectOutputStream out = new ObjectOutputStream(bout);
                    out.writeObject(results);
                    byte[] respData = bout.toByteArray();
                    r.setResponseData(respData);
                } catch (IOException e) {
                    r.setResponseData(null);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            exception = e;
        }
        if (exception != null) {
            logger.log(Level.SEVERE, "{0} >>> Before - Error Handling!!!{1}", new Object[] { System.currentTimeMillis(), exception.getMessage() });
            ErrorInfo errorInfo = new ErrorInfo(exception.getMessage(), ExceptionUtils.getFullStackTrace(exception.fillInStackTrace()));
            errorInfo.setRequestInfo(r);
            r.getErrorInfo().add(errorInfo);
            logger.log(Level.SEVERE, " >>> Error Number: {0}", r.getErrorInfo().size());
            if (r.getRetries() > 0) {
                r.setStatus(STATUS.RETRYING);
                r.setRetries(r.getRetries() - 1);
                r.setExecutions(r.getExecutions() + 1);
                logger.log(Level.SEVERE, " >>> Retrying ({0}) still available!", r.getRetries());
            } else {
                logger.severe(" >>> Error no retries left!");
                r.setStatus(STATUS.ERROR);
                r.setExecutions(r.getExecutions() + 1);
            }
            em.merge(r);
            logger.severe(" >>> After - Error Handling!!!");
        } else {
            r.setStatus(STATUS.DONE);
            em.merge(r);
        }
    }
}
Also used : CommandContext(org.jbpm.executor.api.CommandContext) ExecutionResults(org.jbpm.executor.api.ExecutionResults) ErrorInfo(org.jbpm.executor.entities.ErrorInfo) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RequestInfo(org.jbpm.executor.entities.RequestInfo) ObjectOutputStream(java.io.ObjectOutputStream) IOException(java.io.IOException) Command(org.jbpm.executor.api.Command) ByteArrayInputStream(java.io.ByteArrayInputStream) List(java.util.List) CommandCallback(org.jbpm.executor.api.CommandCallback) ObjectInputStream(java.io.ObjectInputStream) Transactional(org.jboss.seam.transaction.Transactional)

Aggregations

ErrorInfo (org.jbpm.executor.entities.ErrorInfo)4 CommandContext (org.jbpm.executor.api.CommandContext)3 RequestInfo (org.jbpm.executor.entities.RequestInfo)3 List (java.util.List)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Date (java.util.Date)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Transactional (org.jboss.seam.transaction.Transactional)1 AsyncJobException (org.jbpm.executor.AsyncJobException)1 Command (org.jbpm.executor.api.Command)1 CommandCallback (org.jbpm.executor.api.CommandCallback)1 ExecutionResults (org.jbpm.executor.api.ExecutionResults)1 CommandCallback (org.kie.api.executor.CommandCallback)1