use of org.jbpm.executor.entities.RequestInfo in project jBPM5-Developer-Guide by Salaboy.
the class ExecutorImpl method scheduleRequest.
public Long scheduleRequest(String commandId, CommandContext ctx) {
if (ctx == null) {
throw new IllegalStateException("A Context Must Be Provided! ");
}
String businessKey = (String) ctx.getData("businessKey");
RequestInfo requestInfo = new RequestInfo();
requestInfo.setCommandName(commandId);
requestInfo.setKey(businessKey);
requestInfo.setStatus(STATUS.QUEUED);
requestInfo.setMessage("Ready to execute");
if (ctx.getData("retries") != null) {
requestInfo.setRetries((Integer) ctx.getData("retries"));
} else {
requestInfo.setRetries(retries);
}
if (ctx != null) {
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(bout);
oout.writeObject(ctx);
requestInfo.setRequestData(bout.toByteArray());
} catch (IOException e) {
e.printStackTrace();
requestInfo.setRequestData(null);
}
}
em.persist(requestInfo);
logger.log(Level.INFO, " >>> Scheduling request for Command: {0} - requestId: {1} with {2} retries", new Object[] { commandId, requestInfo.getId(), requestInfo.getRetries() });
return requestInfo.getId();
}
use of org.jbpm.executor.entities.RequestInfo 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);
}
}
}
use of org.jbpm.executor.entities.RequestInfo 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());
}
use of org.jbpm.executor.entities.RequestInfo in project jBPM5-Developer-Guide by Salaboy.
the class BasicExecutorBaseTest method cancelRequestTest.
/**
* Test showing how a request can be canceled.
* @throws InterruptedException
*/
@Test
public void cancelRequestTest() throws InterruptedException {
CommandContext ctxCMD = new CommandContext();
ctxCMD.setData("businessKey", UUID.randomUUID().toString());
//Schedule a task.
Long requestId = executor.scheduleRequest("PrintOutCmd", ctxCMD);
// cancel the task immediately
executor.cancelRequest(requestId);
//We should see the canceled task now
List<RequestInfo> cancelledRequests = executor.getCancelledRequests();
assertEquals(1, cancelledRequests.size());
}
use of org.jbpm.executor.entities.RequestInfo in project jBPM5-Developer-Guide by Salaboy.
the class BasicExecutorBaseTest method simpleExcecutionTest.
/**
* Tests a simple command request.
* @throws InterruptedException
*/
@Test
public void simpleExcecutionTest() throws InterruptedException {
CommandContext ctxCMD = new CommandContext();
ctxCMD.setData("businessKey", UUID.randomUUID().toString());
//A job is scheduled by using its CDI @Name
executor.scheduleRequest("PrintOutCmd", ctxCMD);
Thread.sleep(10000);
//after 10 seconds we should have no errors, no queued requests and
//one executed request.
List<RequestInfo> inErrorRequests = executor.getInErrorRequests();
assertEquals(0, inErrorRequests.size());
List<RequestInfo> queuedRequests = executor.getQueuedRequests();
assertEquals(0, queuedRequests.size());
List<RequestInfo> executedRequests = executor.getExecutedRequests();
assertEquals(1, executedRequests.size());
}
Aggregations