Search in sources :

Example 1 with AsynchronousJobEvent

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);
}
Also used : ExecutorServiceImpl(org.jbpm.executor.impl.ExecutorServiceImpl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) QueryContext(org.kie.api.runtime.query.QueryContext) AsynchronousJobEvent(org.jbpm.executor.AsynchronousJobEvent) CountDownAsyncJobListener(org.jbpm.executor.test.CountDownAsyncJobListener) ExecutorService(org.kie.api.executor.ExecutorService) AsyncWorkItemHandler(org.jbpm.executor.impl.wih.AsyncWorkItemHandler) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) HashSet(java.util.HashSet) Test(org.junit.Test) BZ(qa.tools.ikeeper.annotation.BZ)

Example 2 with AsynchronousJobEvent

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));
}
Also used : UserTransaction(javax.transaction.UserTransaction) AsynchronousJobListener(org.jbpm.executor.AsynchronousJobListener) ExecutorServiceImpl(org.jbpm.executor.impl.ExecutorServiceImpl) CommandContext(org.kie.api.executor.CommandContext) ArrayList(java.util.ArrayList) QueryContext(org.kie.api.runtime.query.QueryContext) RequestInfo(org.kie.api.executor.RequestInfo) AsynchronousJobEvent(org.jbpm.executor.AsynchronousJobEvent) CountDownAsyncJobListener(org.jbpm.executor.test.CountDownAsyncJobListener) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)2 AsynchronousJobEvent (org.jbpm.executor.AsynchronousJobEvent)2 ExecutorServiceImpl (org.jbpm.executor.impl.ExecutorServiceImpl)2 CountDownAsyncJobListener (org.jbpm.executor.test.CountDownAsyncJobListener)2 Test (org.junit.Test)2 QueryContext (org.kie.api.runtime.query.QueryContext)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 UserTransaction (javax.transaction.UserTransaction)1 AsynchronousJobListener (org.jbpm.executor.AsynchronousJobListener)1 AsyncWorkItemHandler (org.jbpm.executor.impl.wih.AsyncWorkItemHandler)1 CommandContext (org.kie.api.executor.CommandContext)1 ExecutorService (org.kie.api.executor.ExecutorService)1 RequestInfo (org.kie.api.executor.RequestInfo)1 KieSession (org.kie.api.runtime.KieSession)1 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)1 BZ (qa.tools.ikeeper.annotation.BZ)1