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