use of org.kie.api.executor.CommandContext in project jbpm by kiegroup.
the class BasicExecutorBaseTest method testUpdateRequestDataFromErrorState.
@Test(timeout = 10000)
public void testUpdateRequestDataFromErrorState() throws InterruptedException {
CountDownAsyncJobListener countDownListener = configureListener(1);
CommandContext ctxCMD = new CommandContext();
ctxCMD.setData("businessKey", UUID.randomUUID().toString());
ctxCMD.setData("retries", 0);
Long requestId = executorService.scheduleRequest("org.jbpm.executor.test.MissingDataCommand", ctxCMD);
countDownListener.waitTillCompleted();
List<RequestInfo> inErrorRequests = executorService.getInErrorRequests(new QueryContext());
assertEquals(1, inErrorRequests.size());
Map<String, Object> fixedData = new HashMap<>();
fixedData.put("amount", 200);
executorService.updateRequestData(requestId, fixedData);
countDownListener.reset(1);
((RequeueAware) executorService).requeueById(requestId);
countDownListener.waitTillCompleted();
List<RequestInfo> executedRequests = executorService.getCompletedRequests(new QueryContext());
assertEquals(1, executedRequests.size());
}
use of org.kie.api.executor.CommandContext in project jbpm by kiegroup.
the class ReconfiguredExecutorTest method simpleExcecutionTest.
@Test
public void simpleExcecutionTest() throws InterruptedException {
CountDownAsyncJobListener countDownListener = configureListener(1);
CommandContext ctxCMD = new CommandContext();
ctxCMD.setData("businessKey", UUID.randomUUID().toString());
executorService.scheduleRequest("org.jbpm.executor.commands.PrintOutCommand", ctxCMD);
countDownListener.waitTillCompleted();
List<RequestInfo> inErrorRequests = executorService.getInErrorRequests(new QueryContext());
assertEquals(0, inErrorRequests.size());
List<RequestInfo> queuedRequests = executorService.getQueuedRequests(new QueryContext());
assertEquals(0, queuedRequests.size());
List<RequestInfo> executedRequests = executorService.getCompletedRequests(new QueryContext());
assertEquals(1, executedRequests.size());
}
use of org.kie.api.executor.CommandContext in project jbpm by kiegroup.
the class JmsAvaiableJobExecutorTest method testAsyncAuditProducerPrioritizedJobs.
@Test
public void testAsyncAuditProducerPrioritizedJobs() throws Exception {
CountDownAsyncJobListener countDownListener = configureListener(2);
final List<String> executedJobs = new ArrayList<String>();
((ExecutorServiceImpl) executorService).addAsyncJobListener(new AsynchronousJobListener() {
@Override
public void beforeJobScheduled(AsynchronousJobEvent event) {
}
@Override
public void beforeJobExecuted(AsynchronousJobEvent event) {
}
@Override
public void beforeJobCancelled(AsynchronousJobEvent event) {
}
@Override
public void afterJobScheduled(AsynchronousJobEvent event) {
}
@Override
public void afterJobExecuted(AsynchronousJobEvent event) {
executedJobs.add(event.getJob().getKey());
}
@Override
public void afterJobCancelled(AsynchronousJobEvent event) {
}
});
CommandContext ctxCMD = new CommandContext();
ctxCMD.setData("businessKey", "low priority");
ctxCMD.setData("priority", 2);
CommandContext ctxCMD2 = new CommandContext();
ctxCMD2.setData("businessKey", "high priority");
ctxCMD2.setData("priority", 8);
UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
ut.begin();
executorService.scheduleRequest("org.jbpm.executor.commands.PrintOutCommand", ctxCMD);
executorService.scheduleRequest("org.jbpm.executor.commands.PrintOutCommand", ctxCMD2);
ut.commit();
MessageReceiver receiver = new MessageReceiver();
receiver.receiveAndProcess(queue, countDownListener);
List<RequestInfo> inErrorRequests = executorService.getInErrorRequests(new QueryContext());
assertEquals(0, inErrorRequests.size());
List<RequestInfo> queuedRequests = executorService.getQueuedRequests(new QueryContext());
assertEquals(0, queuedRequests.size());
List<RequestInfo> executedRequests = executorService.getCompletedRequests(new QueryContext());
assertEquals(2, executedRequests.size());
assertEquals(2, executedJobs.size());
assertEquals("high priority", executedJobs.get(0));
assertEquals("low priority", executedJobs.get(1));
}
use of org.kie.api.executor.CommandContext in project jbpm by kiegroup.
the class JmsAvaiableJobExecutorTest method testAsyncAuditProducer.
@Test
public void testAsyncAuditProducer() throws Exception {
CountDownAsyncJobListener countDownListener = configureListener(1);
CommandContext ctxCMD = new CommandContext();
ctxCMD.setData("businessKey", UUID.randomUUID().toString());
UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
ut.begin();
executorService.scheduleRequest("org.jbpm.executor.commands.PrintOutCommand", ctxCMD);
ut.commit();
MessageReceiver receiver = new MessageReceiver();
receiver.receiveAndProcess(queue, countDownListener);
List<RequestInfo> inErrorRequests = executorService.getInErrorRequests(new QueryContext());
assertEquals(0, inErrorRequests.size());
List<RequestInfo> queuedRequests = executorService.getQueuedRequests(new QueryContext());
assertEquals(0, queuedRequests.size());
List<RequestInfo> executedRequests = executorService.getCompletedRequests(new QueryContext());
assertEquals(1, executedRequests.size());
}
use of org.kie.api.executor.CommandContext in project jbpm by kiegroup.
the class ExecutorImpl method updateRequestData.
@Override
public void updateRequestData(Long requestId, Map<String, Object> data) {
logger.debug("About to update request {} data with following {}", requestId, data);
RequestInfo request = (RequestInfo) executorStoreService.findRequest(requestId);
if (request.getStatus().equals(STATUS.CANCELLED) || request.getStatus().equals(STATUS.DONE) || request.getStatus().equals(STATUS.RUNNING)) {
throw new IllegalStateException("Request data can't be updated when request is in status " + request.getStatus());
}
CommandContext ctx = 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);
} finally {
if (in != null) {
in.close();
}
}
}
} catch (Exception e) {
logger.error("Unexpected error when reading request data", e);
throw new RuntimeException(e);
}
if (ctx == null) {
ctx = new CommandContext();
}
WorkItem workItem = (WorkItem) ctx.getData("workItem");
if (workItem != null) {
logger.debug("Updating work item {} parameters with data {}", workItem, data);
for (Entry<String, Object> entry : data.entrySet()) {
workItem.setParameter(entry.getKey(), entry.getValue());
}
} else {
logger.debug("Updating request context with data {}", data);
for (Entry<String, Object> entry : data.entrySet()) {
ctx.setData(entry.getKey(), entry.getValue());
}
}
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(bout);
oout.writeObject(ctx);
request.setRequestData(bout.toByteArray());
} catch (IOException e) {
throw new RuntimeException("Unable to save updated request data", e);
}
executorStoreService.updateRequest(request, null);
logger.debug("Request {} data updated successfully", requestId);
}
Aggregations