Search in sources :

Example 1 with NodeTriggeredCountDownProcessEventListener

use of org.jbpm.test.listener.NodeTriggeredCountDownProcessEventListener in project jbpm by kiegroup.

the class AsyncContinuationSupportTest method testAsyncModeWithSignalProcess.

@Test(timeout = 10000)
public void testAsyncModeWithSignalProcess() throws Exception {
    final NodeTriggeredCountDownProcessEventListener countDownListenerSignalAsync = new NodeTriggeredCountDownProcessEventListener("Signal", 1);
    final NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("EndProcess", 1);
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-WaitForEvent.bpmn2"), ResourceType.BPMN2).addEnvironmentEntry("ExecutorService", executorService).addEnvironmentEntry("AsyncMode", "true").registerableItemsFactory(new DefaultRegisterableItemsFactory() {

        @Override
        public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
            List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
            listeners.add(countDownListener);
            listeners.add(countDownListenerSignalAsync);
            return listeners;
        }
    }).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("WaitForEvent");
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    long processInstanceId = processInstance.getId();
    // wait for the signal not to be triggered in async way before sending signal
    countDownListenerSignalAsync.waitTillCompleted();
    // Send async signal to the process instance
    System.out.println("<<<< Sending signal >>>>>");
    runtime.getKieSession().signalEvent("MySignal", null);
    countDownListener.waitTillCompleted();
    processInstance = runtime.getKieSession().getProcessInstance(processInstanceId);
    assertNull(processInstance);
    List<? extends NodeInstanceLog> logs = runtime.getAuditService().findNodeInstances(processInstanceId);
    assertNotNull(logs);
    assertEquals(8, logs.size());
    waitForAllJobsToComplete();
    List<RequestInfo> completed = executorService.getCompletedRequests(new QueryContext());
    List<RequestInfo> all = executorService.getAllRequests(new QueryContext());
    logger.info("all jobs from db {}", all);
    // there should be 2 completed commands (for script task and end node)
    assertEquals(2, completed.size());
    Set<String> commands = completed.stream().map(RequestInfo::getCommandName).collect(Collectors.toSet());
    assertEquals(1, commands.size());
    assertEquals(AsyncSignalEventCommand.class.getName(), commands.iterator().next());
}
Also used : DefaultRegisterableItemsFactory(org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) NodeTriggeredCountDownProcessEventListener(org.jbpm.test.listener.NodeTriggeredCountDownProcessEventListener) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) QueryContext(org.kie.api.runtime.query.QueryContext) RequestInfo(org.kie.api.executor.RequestInfo) AsyncSignalEventCommand(org.jbpm.process.core.async.AsyncSignalEventCommand) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) NodeTriggeredCountDownProcessEventListener(org.jbpm.test.listener.NodeTriggeredCountDownProcessEventListener) AbstractExecutorBaseTest(org.jbpm.test.util.AbstractExecutorBaseTest) Test(org.junit.Test)

Example 2 with NodeTriggeredCountDownProcessEventListener

use of org.jbpm.test.listener.NodeTriggeredCountDownProcessEventListener in project jbpm by kiegroup.

the class AsyncContinuationSupportTest method testAsyncMIUserTask.

@Test(timeout = 10000)
public void testAsyncMIUserTask() throws Exception {
    final NodeTriggeredCountDownProcessEventListener countDownListener = new NodeTriggeredCountDownProcessEventListener("Hello", 3);
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-MultiInstanceLoopCharacteristicsTask.bpmn2"), ResourceType.BPMN2).addEnvironmentEntry("ExecutorService", executorService).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

        @Override
        public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
            Map<String, WorkItemHandler> handlers = super.getWorkItemHandlers(runtime);
            handlers.put("async", new SystemOutWorkItemHandler());
            return handlers;
        }

        @Override
        public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
            List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
            listeners.add(countDownListener);
            return listeners;
        }
    }).get();
    manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
    assertNotNull(manager);
    RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    ArrayList<String> items = new ArrayList<String>();
    items.add("one");
    items.add("two");
    items.add("three");
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("list", items);
    ProcessInstance processInstance = ksession.startProcess("MultiInstanceLoopCharacteristicsTask", params);
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    long processInstanceId = processInstance.getId();
    countDownListener.waitTillCompleted();
    List<TaskSummary> tasks = runtime.getTaskService().getTasksAssignedAsPotentialOwner("john", "en-UK");
    assertNotNull(tasks);
    assertEquals(3, tasks.size());
    for (TaskSummary task : tasks) {
        runtime.getTaskService().start(task.getId(), "john");
        runtime.getTaskService().complete(task.getId(), "john", null);
    }
    processInstance = runtime.getKieSession().getProcessInstance(processInstance.getId());
    assertNull(processInstance);
    List<? extends NodeInstanceLog> logs = runtime.getAuditService().findNodeInstances(processInstanceId);
    assertNotNull(logs);
    assertEquals(12, logs.size());
}
Also used : DefaultRegisterableItemsFactory(org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) HashMap(java.util.HashMap) NodeTriggeredCountDownProcessEventListener(org.jbpm.test.listener.NodeTriggeredCountDownProcessEventListener) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) ArrayList(java.util.ArrayList) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) NodeTriggeredCountDownProcessEventListener(org.jbpm.test.listener.NodeTriggeredCountDownProcessEventListener) AbstractExecutorBaseTest(org.jbpm.test.util.AbstractExecutorBaseTest) Test(org.junit.Test)

Aggregations

DefaultRegisterableItemsFactory (org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory)2 NodeLeftCountDownProcessEventListener (org.jbpm.test.listener.NodeLeftCountDownProcessEventListener)2 NodeTriggeredCountDownProcessEventListener (org.jbpm.test.listener.NodeTriggeredCountDownProcessEventListener)2 AbstractExecutorBaseTest (org.jbpm.test.util.AbstractExecutorBaseTest)2 Test (org.junit.Test)2 ProcessEventListener (org.kie.api.event.process.ProcessEventListener)2 KieSession (org.kie.api.runtime.KieSession)2 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)2 RuntimeEnvironment (org.kie.api.runtime.manager.RuntimeEnvironment)2 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 AsyncSignalEventCommand (org.jbpm.process.core.async.AsyncSignalEventCommand)1 SystemOutWorkItemHandler (org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler)1 RequestInfo (org.kie.api.executor.RequestInfo)1 WorkItemHandler (org.kie.api.runtime.process.WorkItemHandler)1 QueryContext (org.kie.api.runtime.query.QueryContext)1 TaskSummary (org.kie.api.task.model.TaskSummary)1