use of org.jbpm.executor.impl.wih.AsyncWorkItemHandler in project jbpm by kiegroup.
the class ParallelAsyncJobsTest method testRunBasicAsync.
/**
* The tests verifies that async. jobs will be executed in parallel.
*
* JobExecutor configuration:
* - Thread pool size = 4
* - Pending task scan interval = 1 second
*
* The process in this test will create 4 long running tasks (8 seconds) one after another. Then
* the test will sleep for 4 seconds giving the JobExecutor time to pick up at least 2 tasks.
*
* The JobExecutor should be able to pick up all the tasks because one task takes 8 seconds to
* complete and the scan interval is 1 second. On the other hand a task should not complete in
* the 4 seconds so pending task count should not be lower than 3 if parallelism does not work.
*/
@Test(timeout = 30000)
@BZ("1146829")
public void testRunBasicAsync() throws Exception {
ExecutorService executorService = getExecutorService();
final Set<String> threadExeuctingJobs = new HashSet<>();
CountDownAsyncJobListener countDownListener = new CountDownAsyncJobListener(4) {
@Override
public void afterJobExecuted(AsynchronousJobEvent event) {
super.afterJobExecuted(event);
threadExeuctingJobs.add(Thread.currentThread().getName());
}
};
((ExecutorServiceImpl) executorService).addAsyncJobListener(countDownListener);
KieSession ks = createKSession(PARENT, SUBPROCESS);
ks.getWorkItemManager().registerWorkItemHandler("async", new AsyncWorkItemHandler(executorService, "org.jbpm.test.command.LongRunningCommand"));
List<String> exceptions = new ArrayList<String>();
exceptions.add("ADRESS_EXCEPTION");
exceptions.add("ID_EXCEPTION");
exceptions.add("PHONE_EXCEPTION");
Map<String, Object> pm = new HashMap<String, Object>();
pm.put("exceptions", exceptions);
ProcessInstance pi = ks.startProcess(PARENT_ID, pm);
// assert that the main process was completed as tasks are executed asynchronously
assertProcessInstanceCompleted(pi.getId());
countDownListener.waitTillCompleted();
// assert that all jobs have where completed.
Assertions.assertThat(executorService.getCompletedRequests(new QueryContext())).as("All async jobs should have been executed").hasSize(4);
Assertions.assertThat(threadExeuctingJobs).as("There should be 4 distinct threads as jobs where executed in parallel").hasSize(4);
}
use of org.jbpm.executor.impl.wih.AsyncWorkItemHandler in project jbpm by kiegroup.
the class AsyncWIHOnOracleTest method testAsyncWIHExecutedMoreThanOnceOnOracle.
@Test
@BZ("1234592")
public void testAsyncWIHExecutedMoreThanOnceOnOracle() throws Exception {
KieSession ksession = createKSession(PROCESS);
TrackingProcessEventListener tpel = new TrackingProcessEventListener();
ksession.addEventListener(tpel);
WorkItemHandler wih = new AsyncWorkItemHandler(getExecutorService(), CounterCommand.class.getName());
ksession.getWorkItemManager().registerWorkItemHandler("async", wih);
ksession.startProcess(PROCESS_ID);
boolean completed = tpel.waitForProcessToComplete(10000);
Assertions.assertThat(completed).as("The process should have finished in 10s").isTrue();
Assertions.assertThat(CounterCommand.getCounter()).as("The job has not been executed").isNotEqualTo(0);
Assertions.assertThat(CounterCommand.getCounter()).as("The job has been executed multiple times").isEqualTo(1);
}
use of org.jbpm.executor.impl.wih.AsyncWorkItemHandler in project jbpm by kiegroup.
the class AsyncTaskTest method testTaskErrorHandling.
@Test(timeout = 10000)
public void testTaskErrorHandling() throws Exception {
NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("Task 1", 1);
addProcessEventListener(countDownListener);
KieSession ksession = createKSession(ASYNC_EXECUTOR);
WorkItemManager wim = ksession.getWorkItemManager();
wim.registerWorkItemHandler("async", new AsyncWorkItemHandler(getExecutorService()));
Map<String, Object> pm = new HashMap<String, Object>();
pm.put("command", USER_FAILING_COMMAND);
ProcessInstance pi = ksession.startProcess(ASYNC_EXECUTOR_ID, pm);
assertNodeTriggered(pi.getId(), "Start", "Hello", "Task 1");
assertNodeNotTriggered(pi.getId(), "Output");
assertNodeNotTriggered(pi.getId(), "Runtime Error Handling");
assertNodeNotTriggered(pi.getId(), "Illegal Argument Error Handling");
// Wait for the 4 retries to fail
countDownListener.waitTillCompleted();
ProcessInstance processInstance = ksession.getProcessInstance(pi.getId());
assertNull(processInstance);
assertNodeTriggered(pi.getId(), "Runtime Error Handling", "RuntimeErrorEnd");
assertNodeNotTriggered(pi.getId(), "Output");
assertNodeNotTriggered(pi.getId(), "Illegal Argument Error Handling");
Assertions.assertThat(getExecutorService().getInErrorRequests(new QueryContext())).hasSize(1);
Assertions.assertThat(getExecutorService().getInErrorRequests(new QueryContext()).get(0).getErrorInfo().get(0).getMessage()).isEqualTo("Internal Error");
assertProcessInstanceCompleted(pi.getId());
}
use of org.jbpm.executor.impl.wih.AsyncWorkItemHandler in project jbpm by kiegroup.
the class AsyncTaskTest method testTaskComplete.
@Test(timeout = 10000)
@BZ("1121027")
public void testTaskComplete() throws Exception {
NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("Process async", 1);
CountDownAsyncJobListener countDownJobListener = new CountDownAsyncJobListener(1);
try {
((ExecutorServiceImpl) getExecutorService()).addAsyncJobListener(countDownJobListener);
addProcessEventListener(countDownListener);
KieSession ksession = createKSession(ASYNC_DATA_EXECUTOR);
WorkItemManager wim = ksession.getWorkItemManager();
wim.registerWorkItemHandler("async", new AsyncWorkItemHandler(getExecutorService()));
Map<String, Object> pm = new HashMap<String, Object>();
pm.put("command", USER_COMMAND);
ProcessInstance pi = ksession.startProcess(ASYNC_DATA_EXECUTOR_ID, pm);
assertNodeTriggered(pi.getId(), "StartProcess", "Set user info", "Process async");
// Wait for the job to complete
countDownListener.waitTillCompleted();
ProcessInstance processInstance = ksession.getProcessInstance(pi.getId());
assertNull(processInstance);
assertNodeTriggered(pi.getId(), "Output", "EndProcess");
countDownJobListener.waitTillCompleted();
Assertions.assertThat(getExecutorService().getCompletedRequests(new QueryContext())).hasSize(1);
assertProcessInstanceCompleted(pi.getId());
} finally {
((ExecutorServiceImpl) getExecutorService()).removeAsyncJobListener(countDownJobListener);
}
}
use of org.jbpm.executor.impl.wih.AsyncWorkItemHandler in project jbpm by kiegroup.
the class AsyncTaskTest method testTaskFail.
@Test(timeout = 10000)
public void testTaskFail() throws Exception {
CountDownAsyncJobListener countDownListener = new CountDownAsyncJobListener(4);
((ExecutorServiceImpl) getExecutorService()).addAsyncJobListener(countDownListener);
KieSession ksession = createKSession(ASYNC_DATA_EXECUTOR);
WorkItemManager wim = ksession.getWorkItemManager();
wim.registerWorkItemHandler("async", new AsyncWorkItemHandler(getExecutorService()));
Map<String, Object> pm = new HashMap<String, Object>();
pm.put("command", USER_FAILING_COMMAND);
ProcessInstance pi = ksession.startProcess(ASYNC_DATA_EXECUTOR_ID, pm);
assertNodeTriggered(pi.getId(), "StartProcess", "Set user info", "Process async");
assertNodeNotTriggered(pi.getId(), "Output");
// Wait for the 4 retries to fail
countDownListener.waitTillCompleted();
ProcessInstance processInstance = ksession.getProcessInstance(pi.getId());
assertNotNull(processInstance);
assertNodeNotTriggered(pi.getId(), "Output");
Assertions.assertThat(getExecutorService().getInErrorRequests(new QueryContext())).hasSize(1);
Assertions.assertThat(getExecutorService().getInErrorRequests(new QueryContext()).get(0).getErrorInfo()).hasSize(4);
Assertions.assertThat(getExecutorService().getInErrorRequests(new QueryContext()).get(0).getErrorInfo().get(0).getMessage()).isEqualTo("Internal Error");
assertProcessInstanceActive(pi.getId());
ksession.abortProcessInstance(pi.getId());
assertProcessInstanceAborted(pi.getId());
}
Aggregations