use of org.jbpm.executor.AsynchronousJobEvent 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.AsynchronousJobEvent 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));
}
Aggregations