Search in sources :

Example 6 with AsyncWorkItemHandler

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);
}
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 7 with AsyncWorkItemHandler

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);
}
Also used : AsyncWorkItemHandler(org.jbpm.executor.impl.wih.AsyncWorkItemHandler) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) TrackingProcessEventListener(org.jbpm.test.listener.TrackingProcessEventListener) AsyncWorkItemHandler(org.jbpm.executor.impl.wih.AsyncWorkItemHandler) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test) BZ(qa.tools.ikeeper.annotation.BZ)

Example 8 with AsyncWorkItemHandler

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());
}
Also used : NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) HashMap(java.util.HashMap) AsyncWorkItemHandler(org.jbpm.executor.impl.wih.AsyncWorkItemHandler) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) QueryContext(org.kie.api.runtime.query.QueryContext) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) Test(org.junit.Test)

Example 9 with AsyncWorkItemHandler

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

Example 10 with AsyncWorkItemHandler

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());
}
Also used : CountDownAsyncJobListener(org.jbpm.test.listener.CountDownAsyncJobListener) ExecutorServiceImpl(org.jbpm.executor.impl.ExecutorServiceImpl) HashMap(java.util.HashMap) AsyncWorkItemHandler(org.jbpm.executor.impl.wih.AsyncWorkItemHandler) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) QueryContext(org.kie.api.runtime.query.QueryContext) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) Test(org.junit.Test)

Aggregations

AsyncWorkItemHandler (org.jbpm.executor.impl.wih.AsyncWorkItemHandler)10 HashMap (java.util.HashMap)8 Test (org.junit.Test)8 KieSession (org.kie.api.runtime.KieSession)8 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)7 WorkItemManager (org.kie.api.runtime.process.WorkItemManager)7 QueryContext (org.kie.api.runtime.query.QueryContext)6 ExecutorServiceImpl (org.jbpm.executor.impl.ExecutorServiceImpl)5 CountDownAsyncJobListener (org.jbpm.test.listener.CountDownAsyncJobListener)4 BZ (qa.tools.ikeeper.annotation.BZ)4 NodeLeftCountDownProcessEventListener (org.jbpm.test.listener.NodeLeftCountDownProcessEventListener)3 ExecutorService (org.kie.api.executor.ExecutorService)2 WorkItemHandler (org.kie.api.runtime.process.WorkItemHandler)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 AsynchronousJobEvent (org.jbpm.executor.AsynchronousJobEvent)1 CountDownAsyncJobListener (org.jbpm.executor.test.CountDownAsyncJobListener)1 VariableInstanceLog (org.jbpm.process.audit.VariableInstanceLog)1 TrackingProcessEventListener (org.jbpm.test.listener.TrackingProcessEventListener)1 ErrorInfo (org.kie.api.executor.ErrorInfo)1