use of org.kie.api.executor.RequestInfo in project jbpm by kiegroup.
the class AsyncWorkItemHandlerTest method testRunProcessWithAsyncHandlerProritizedJobs.
@Test(timeout = 10000)
public void testRunProcessWithAsyncHandlerProritizedJobs() throws Exception {
CountDownAsyncJobListener countDownListener = new CountDownAsyncJobListener(1);
((ExecutorServiceImpl) executorService).addAsyncJobListener(countDownListener);
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-PrioritizedAsyncTasks.bpmn2"), ResourceType.BPMN2).registerableItemsFactory(new DefaultRegisterableItemsFactory() {
@Override
public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
Map<String, WorkItemHandler> handlers = super.getWorkItemHandlers(runtime);
handlers.put("async", new AsyncWorkItemHandler(executorService, "org.jbpm.executor.commands.PrintOutCommand"));
return handlers;
}
}).get();
manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
assertNotNull(manager);
RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
KieSession ksession = runtime.getKieSession();
assertNotNull(ksession);
ProcessInstance processInstance = ksession.startProcess("async-examples.priority-jobs");
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
List<RequestInfo> delayedPrintOuts = executorService.getRequestsByCommand("org.jbpm.executor.test.CustomCommand", new QueryContext());
List<RequestInfo> printOuts = executorService.getRequestsByCommand("org.jbpm.executor.commands.PrintOutCommand", new QueryContext());
assertEquals(1, delayedPrintOuts.size());
assertEquals(1, printOuts.size());
assertEquals(STATUS.QUEUED, delayedPrintOuts.get(0).getStatus());
assertEquals(STATUS.QUEUED, printOuts.get(0).getStatus());
countDownListener.waitTillCompleted();
delayedPrintOuts = executorService.getRequestsByCommand("org.jbpm.executor.test.CustomCommand", new QueryContext());
printOuts = executorService.getRequestsByCommand("org.jbpm.executor.commands.PrintOutCommand", new QueryContext());
assertEquals(1, delayedPrintOuts.size());
assertEquals(1, printOuts.size());
assertEquals(STATUS.DONE, delayedPrintOuts.get(0).getStatus());
assertEquals(STATUS.QUEUED, printOuts.get(0).getStatus());
countDownListener.reset(1);
countDownListener.waitTillCompleted();
delayedPrintOuts = executorService.getRequestsByCommand("org.jbpm.executor.test.CustomCommand", new QueryContext());
printOuts = executorService.getRequestsByCommand("org.jbpm.executor.commands.PrintOutCommand", new QueryContext());
assertEquals(1, delayedPrintOuts.size());
assertEquals(1, printOuts.size());
assertEquals(STATUS.DONE, delayedPrintOuts.get(0).getStatus());
assertEquals(STATUS.DONE, printOuts.get(0).getStatus());
processInstance = runtime.getKieSession().getProcessInstance(processInstance.getId());
assertNull(processInstance);
}
use of org.kie.api.executor.RequestInfo in project jbpm by kiegroup.
the class AsyncWorkItemHandlerTest method testRunProcessWithAsyncHandlerWithBusinessKeyAbort.
@Test
public void testRunProcessWithAsyncHandlerWithBusinessKeyAbort() throws Exception {
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTaskWithBusinessKey.bpmn2"), ResourceType.BPMN2).registerableItemsFactory(new DefaultRegisterableItemsFactory() {
@Override
public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
Map<String, WorkItemHandler> handlers = super.getWorkItemHandlers(runtime);
handlers.put("async", new AsyncWorkItemHandler(executorService, "org.jbpm.executor.commands.PrintOutCommand"));
return handlers;
}
}).get();
manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
assertNotNull(manager);
RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
KieSession ksession = runtime.getKieSession();
assertNotNull(ksession);
String businessKey = UUID.randomUUID().toString();
Map<String, Object> params = new HashMap<String, Object>();
params.put("businessKey", businessKey);
params.put("delay", "5s");
ProcessInstance processInstance = ksession.startProcess("ScriptTask", params);
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
runtime.getKieSession().abortProcessInstance(processInstance.getId());
processInstance = runtime.getKieSession().getProcessInstance(processInstance.getId());
assertNull(processInstance);
List<RequestInfo> jobRequest = executorService.getRequestsByBusinessKey(businessKey, new QueryContext());
assertNotNull(jobRequest);
assertEquals(1, jobRequest.size());
assertEquals(businessKey, jobRequest.get(0).getKey());
assertEquals(STATUS.CANCELLED, jobRequest.get(0).getStatus());
}
use of org.kie.api.executor.RequestInfo in project jbpm by kiegroup.
the class AsyncWorkItemHandler method abortWorkItem.
@Override
public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
String businessKey = buildBusinessKey(workItem);
logger.info("Looking up for not cancelled and not done requests for business key {}", businessKey);
List<RequestInfo> requests = executorService.getRequestsByBusinessKey(businessKey, new QueryContext());
if (requests != null) {
for (RequestInfo request : requests) {
if (request.getStatus() != STATUS.CANCELLED && request.getStatus() != STATUS.DONE && request.getStatus() != STATUS.ERROR && request.getStatus() != STATUS.RUNNING) {
logger.info("About to cancel request with id {} and business key {} request state {}", request.getId(), businessKey, request.getStatus());
executorService.cancelRequest(request.getId());
}
}
}
}
use of org.kie.api.executor.RequestInfo in project jbpm by kiegroup.
the class BasicExecutorBaseTest method simpleExecutionTest.
@Test(timeout = 10000)
public void simpleExecutionTest() 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());
assertEquals(1, executedRequests.get(0).getExecutions());
}
use of org.kie.api.executor.RequestInfo in project jbpm by kiegroup.
the class BasicExecutorBaseTest method testCustomIncrementingRequestRetrySpecialValues.
@Test(timeout = 10000)
public void testCustomIncrementingRequestRetrySpecialValues() throws InterruptedException {
CountDownAsyncJobListener countDownListener = configureListener(2);
CommandContext ctxCMD = new CommandContext();
ctxCMD.setData("businessKey", UUID.randomUUID().toString());
ctxCMD.setData("retryDelay", "-1ms, 1m 80s");
ctxCMD.setData("retries", 2);
executorService.scheduleRequest("org.jbpm.executor.ThrowExceptionCommand", ctxCMD);
countDownListener.waitTillCompleted();
List<ErrorInfo> errors = executorService.getAllErrors(new QueryContext());
// 2 executions in total 1(regular) + 1(retry)
assertEquals(2, errors.size());
long firstError = errors.get(0).getTime().getTime();
long secondError = errors.get(1).getTime().getTime();
// Time difference between first and second shouldn't be bigger than 4 seconds as executor has 3 second interval and
// should start executing second command immediately.
long diff = secondError - firstError;
assertTrue(diff < 4000);
List<RequestInfo> allRequests = executorService.getAllRequests(new QueryContext());
assertEquals(1, allRequests.size());
// Future execution is planned to be started 2 minutes and 20 seconds after last fail.
// Time difference vary because of test thread sleeping for 10 seconds.
diff = allRequests.get(0).getTime().getTime() - Calendar.getInstance().getTimeInMillis();
assertTrue(diff < 140000);
assertTrue(diff > 130000);
executorService.clearAllRequests();
}
Aggregations