Search in sources :

Example 1 with Command

use of org.kie.api.executor.Command in project jbpm by kiegroup.

the class AbstractAvailableJobsExecutor method executeGivenJob.

public void executeGivenJob(RequestInfo request) {
    Throwable exception = null;
    try {
        AsyncExecutionMarker.markAsync();
        eventSupport.fireBeforeJobExecuted(request, null);
        if (request != null) {
            boolean processReoccurring = false;
            Command cmd = null;
            CommandContext ctx = null;
            ExecutionResults results = null;
            List<CommandCallback> callbacks = null;
            ClassLoader cl = getClassLoader(request.getDeploymentId());
            try {
                logger.debug("Processing Request Id: {}, status {} command {}", request.getId(), request.getStatus(), request.getCommandName());
                byte[] reqData = request.getRequestData();
                if (reqData != null) {
                    ObjectInputStream in = null;
                    try {
                        in = new ClassLoaderObjectInputStream(cl, new ByteArrayInputStream(reqData));
                        ctx = (CommandContext) in.readObject();
                    } catch (IOException e) {
                        logger.warn("Exception while serializing context data", e);
                        return;
                    } finally {
                        if (in != null) {
                            in.close();
                        }
                    }
                }
                if (request.getResponseData() == null) {
                    for (Map.Entry<String, Object> entry : contextData.entrySet()) {
                        ctx.setData(entry.getKey(), entry.getValue());
                    }
                    // add class loader so internally classes can be created with valid (kjar) deployment
                    ctx.setData("ClassLoader", cl);
                    cmd = classCacheManager.findCommand(request.getCommandName(), cl);
                    // increment execution counter directly to cover both success and failure paths
                    request.setExecutions(request.getExecutions() + 1);
                    results = cmd.execute(ctx);
                    if (results == null) {
                        results = new ExecutionResults();
                    }
                    try {
                        ByteArrayOutputStream bout = new ByteArrayOutputStream();
                        ObjectOutputStream out = new ObjectOutputStream(bout);
                        out.writeObject(results);
                        byte[] respData = bout.toByteArray();
                        request.setResponseData(respData);
                    } catch (IOException e) {
                        request.setResponseData(null);
                    }
                    results.setData("CompletedAt", new Date());
                    request.setStatus(STATUS.DONE);
                    executorStoreService.updateRequest(request, null);
                    processReoccurring = true;
                } else {
                    logger.debug("Job was already successfully executed, retrying callbacks only...");
                    byte[] resData = request.getResponseData();
                    if (resData != null) {
                        ObjectInputStream in = null;
                        try {
                            in = new ClassLoaderObjectInputStream(cl, new ByteArrayInputStream(resData));
                            results = (ExecutionResults) in.readObject();
                        } catch (IOException e) {
                            logger.warn("Exception while serializing response data", e);
                            return;
                        } finally {
                            if (in != null) {
                                in.close();
                            }
                        }
                    }
                    request.setStatus(STATUS.DONE);
                    executorStoreService.updateRequest(request, null);
                    processReoccurring = true;
                }
                // callback handling after job execution
                callbacks = classCacheManager.buildCommandCallback(ctx, cl);
                for (CommandCallback handler : callbacks) {
                    handler.onCommandDone(ctx, results);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } catch (Throwable e) {
                exception = e;
                if (callbacks == null) {
                    callbacks = classCacheManager.buildCommandCallback(ctx, cl);
                }
                processReoccurring = handleException(request, e, ctx, callbacks);
            } finally {
                ((ExecutorImpl) executor).clearExecution(request.getId());
                AsyncExecutionMarker.reset();
                handleCompletion(processReoccurring, cmd, ctx);
                eventSupport.fireAfterJobExecuted(request, exception);
            }
        }
    } catch (Exception e) {
        logger.warn("Unexpected error while processin executor's job {}", e.getMessage(), e);
    }
}
Also used : CommandContext(org.kie.api.executor.CommandContext) ExecutionResults(org.kie.api.executor.ExecutionResults) ClassLoaderObjectInputStream(org.apache.commons.io.input.ClassLoaderObjectInputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Date(java.util.Date) IOException(java.io.IOException) AsyncJobException(org.jbpm.executor.AsyncJobException) Command(org.kie.api.executor.Command) ByteArrayInputStream(java.io.ByteArrayInputStream) CommandCallback(org.kie.api.executor.CommandCallback) HashMap(java.util.HashMap) Map(java.util.Map) ObjectInputStream(java.io.ObjectInputStream) ClassLoaderObjectInputStream(org.apache.commons.io.input.ClassLoaderObjectInputStream)

Example 2 with Command

use of org.kie.api.executor.Command in project jbpm by kiegroup.

the class ClassCacheManager method findCommand.

/**
 * Finds command by FQCN and if not found loads the class and store the instance in
 * the cache.
 * @param name - fully qualified class name of the command
 * @return initialized class instance
 */
public Command findCommand(String name, ClassLoader cl) {
    synchronized (commandCache) {
        if (!commandCache.containsKey(name)) {
            try {
                Command commandInstance = (Command) Class.forName(name, true, cl).newInstance();
                commandCache.put(name, commandInstance);
            } catch (Exception ex) {
                throw new IllegalArgumentException("Unknown Command implementation with name '" + name + "'");
            }
        } else {
            Command cmd = commandCache.get(name);
            if (!cmd.getClass().getClassLoader().equals(cl)) {
                commandCache.remove(name);
                try {
                    Command commandInstance = (Command) Class.forName(name, true, cl).newInstance();
                    commandCache.put(name, commandInstance);
                } catch (Exception ex) {
                    throw new IllegalArgumentException("Unknown Command implementation with name '" + name + "'");
                }
            }
        }
    }
    return commandCache.get(name);
}
Also used : Command(org.kie.api.executor.Command)

Aggregations

Command (org.kie.api.executor.Command)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 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ClassLoaderObjectInputStream (org.apache.commons.io.input.ClassLoaderObjectInputStream)1 AsyncJobException (org.jbpm.executor.AsyncJobException)1 CommandCallback (org.kie.api.executor.CommandCallback)1 CommandContext (org.kie.api.executor.CommandContext)1 ExecutionResults (org.kie.api.executor.ExecutionResults)1